Class: Orbit::Vector
- Inherits:
-
Object
- Object
- Orbit::Vector
- Defined in:
- lib/orbit/vector.rb
Instance Attribute Summary collapse
-
#m_w ⇒ Object
Returns the value of attribute m_w.
-
#m_x ⇒ Object
Returns the value of attribute m_x.
-
#m_y ⇒ Object
Returns the value of attribute m_y.
-
#m_z ⇒ Object
Returns the value of attribute m_z.
Instance Method Summary collapse
-
#angle(vec) ⇒ Object
##################################/ Calculate the angle between this vector and another.
-
#dot(vec) ⇒ Object
##################################/ Return the dot product.
-
#initialize(x = nil, y = nil, z = nil, w = nil) ⇒ Vector
constructor
A new instance of Vector.
-
#magnitude ⇒ Object
##################################/.
-
#mul(factor) ⇒ Object
##################################/ Multiply each component in the vector by ‘factor’.
-
#sub(vec) ⇒ Object
##################################/ Subtract a vector from this one.
Constructor Details
#initialize(x = nil, y = nil, z = nil, w = nil) ⇒ Vector
Returns a new instance of Vector.
9 10 11 12 13 14 |
# File 'lib/orbit/vector.rb', line 9 def initialize( x = nil, y = nil, z = nil, w = nil ) @m_x = x @m_y = y @m_z = z @m_w = w end |
Instance Attribute Details
#m_w ⇒ Object
Returns the value of attribute m_w.
7 8 9 |
# File 'lib/orbit/vector.rb', line 7 def m_w @m_w end |
#m_x ⇒ Object
Returns the value of attribute m_x.
4 5 6 |
# File 'lib/orbit/vector.rb', line 4 def m_x @m_x end |
#m_y ⇒ Object
Returns the value of attribute m_y.
5 6 7 |
# File 'lib/orbit/vector.rb', line 5 def m_y @m_y end |
#m_z ⇒ Object
Returns the value of attribute m_z.
6 7 8 |
# File 'lib/orbit/vector.rb', line 6 def m_z @m_z end |
Instance Method Details
#angle(vec) ⇒ Object
##################################/ Calculate the angle between this vector and another
38 39 40 |
# File 'lib/orbit/vector.rb', line 38 def angle(vec) return Math.acos(dot(vec) / (magnitude() * vec.magnitude())) end |
#dot(vec) ⇒ Object
##################################/ Return the dot product
50 51 52 |
# File 'lib/orbit/vector.rb', line 50 def dot(vec) return (@m_x * vec.m_x) + (@m_y * vec.m_y) + (@m_z * vec.m_z) end |
#magnitude ⇒ Object
##################################/
43 44 45 |
# File 'lib/orbit/vector.rb', line 43 def magnitude return Math.sqrt((@m_x * @m_x) + (@m_y * @m_y) + (@m_z * @m_z)) end |
#mul(factor) ⇒ Object
##################################/ Multiply each component in the vector by ‘factor’.
18 19 20 21 22 23 24 25 |
# File 'lib/orbit/vector.rb', line 18 def mul(factor) #puts "m_x: #{@m_x}, factor: #{factor}" @m_x = @m_x * factor @m_y *= factor @m_z *= factor @m_w *= (factor).abs if @m_w end |
#sub(vec) ⇒ Object
##################################/ Subtract a vector from this one.
29 30 31 32 33 34 |
# File 'lib/orbit/vector.rb', line 29 def sub(vec) @m_x -= vec.m_x @m_y -= vec.m_y @m_z -= vec.m_z @m_w -= vec.m_w end |