Class: RubyLabs::SphereLab::Vector
- Inherits:
-
Object
- Object
- RubyLabs::SphereLab::Vector
- Defined in:
- lib/spherelab.rb
Overview
Vector
A Vector is a 3-tuple of (x,y,z) coordinates. Operators defined for vector objects (where v
is a vector and a
is a scalar) are:
v == v
v + v
v - v
v * a
v.add(v) (equivalent to v += v)
v.sub(v) (equivalent to v -= v)
v.scale(a) (equivalent to v *= a)
v.norm
Arithmetic methods are invoked for a vector v1
when Ruby evaluates an expression of the form v1
<op> v2
where <op> is , -, or *. They create a a new vector containing the result of the operation. and - do element-wise addition or and subtraction, * is a vector-scalar multiplication.
The add, sub, and scale methods modify a vector, e.g. a call to v1.add(v2)
will update v1
by adding the corresponding components in v2
.
Instance Attribute Summary collapse
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Instance Method Summary collapse
-
#*(a) ⇒ Object
Create a new vector that is the product of this vector and scalar
a
. -
#+(v) ⇒ Object
Create a new vector that is the sum of this vector and vector
v
. -
#-(v) ⇒ Object
Create a new vector that is the difference between this vector and vector
v
. -
#==(v) ⇒ Object
Compare this vector with another vector
v
. -
#add(v) ⇒ Object
Add the components of vector
v
to this vector. -
#angle(v) ⇒ Object
Compute the angle between this vector and vector
v
. -
#coords ⇒ Object
Return a vector of three Floats corresponding to the
x
,y
, andz
components of this vector. -
#coords=(a) ⇒ Object
Set the three components of this vector to the values in the array
a
, which should be an array of three Floats (it can have more values, but only the first three are used). -
#initialize(*args) ⇒ Vector
constructor
Make a new vector with the specified
x
,y
, andz
components. -
#inspect ⇒ Object
Create a string that displays the
x
,y
, andz
coordinates of the vector. -
#norm ⇒ Object
Compute the magnitude of this vector, which is the Euclidean norm defined by sqrt(x**2 + y**2 + z**2).
-
#scale(a) ⇒ Object
Multiply each component of this vector by scalar
a
. -
#sub(v) ⇒ Object
Subtract the components of vector
v
from this vector.
Constructor Details
#initialize(*args) ⇒ Vector
Make a new vector with the specified x
, y
, and z
components.
84 85 86 |
# File 'lib/spherelab.rb', line 84 def initialize(*args) @x, @y, @z = args end |
Instance Attribute Details
#x ⇒ Object
Returns the value of attribute x.
80 81 82 |
# File 'lib/spherelab.rb', line 80 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
80 81 82 |
# File 'lib/spherelab.rb', line 80 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
80 81 82 |
# File 'lib/spherelab.rb', line 80 def z @z end |
Instance Method Details
#*(a) ⇒ Object
Create a new vector that is the product of this vector and scalar a
.
116 117 118 |
# File 'lib/spherelab.rb', line 116 def *(a) Vector.new(@x * a, @y * a, @z * a) end |
#+(v) ⇒ Object
Create a new vector that is the sum of this vector and vector v
.
104 105 106 |
# File 'lib/spherelab.rb', line 104 def +(v) Vector.new(@x + v.x, @y + v.y, @z + v.z) end |
#-(v) ⇒ Object
Create a new vector that is the difference between this vector and vector v
.
110 111 112 |
# File 'lib/spherelab.rb', line 110 def -(v) Vector.new(@x - v.x, @y - v.y, @z - v.z) end |
#==(v) ⇒ Object
Compare this vector with another vector v
. Two vectors are the same if all three components are the same.
98 99 100 |
# File 'lib/spherelab.rb', line 98 def ==(v) return (@x == v.x) && (@y == v.y) && (@z == v.z) end |
#add(v) ⇒ Object
Add the components of vector v
to this vector.
122 123 124 125 126 127 |
# File 'lib/spherelab.rb', line 122 def add(v) @x += v.x @y += v.y @z += v.z self end |
#angle(v) ⇒ Object
Compute the angle between this vector and vector v
. This method is only used when drawing 2D vectors, so the z dimension is ignored.
156 157 158 |
# File 'lib/spherelab.rb', line 156 def angle(v) acos((@x * v.x + @y * v.y) / (norm * v.norm)) end |
#coords ⇒ Object
Return a vector of three Floats corresponding to the x
, y
, and z
components of this vector.
163 164 165 |
# File 'lib/spherelab.rb', line 163 def coords [@x, @y, @z] end |
#coords=(a) ⇒ Object
Set the three components of this vector to the values in the array a
, which should be an array of three Floats (it can have more values, but only the first three are used).
171 172 173 174 175 |
# File 'lib/spherelab.rb', line 171 def coords=(a) @x = a[0] @y = a[1] @z = a[2] end |
#inspect ⇒ Object
Create a string that displays the x
, y
, and z
coordinates of the vector.
90 91 92 93 |
# File 'lib/spherelab.rb', line 90 def inspect pos = [@x,@y,@z].map { |x| sprintf "%.5g", x } return "(" + pos.join(",") + ")" end |
#norm ⇒ Object
Compute the magnitude of this vector, which is the Euclidean norm defined by sqrt(x**2 + y**2 + z**2)
149 150 151 |
# File 'lib/spherelab.rb', line 149 def norm sqrt(@x*@x + @y*@y + @z*@z) end |
#scale(a) ⇒ Object
Multiply each component of this vector by scalar a
.
140 141 142 143 144 |
# File 'lib/spherelab.rb', line 140 def scale(a) @x *= a @y *= a @z *= a end |
#sub(v) ⇒ Object
Subtract the components of vector v
from this vector.
131 132 133 134 135 136 |
# File 'lib/spherelab.rb', line 131 def sub(v) @x -= v.x @y -= v.y @z -= v.z self end |