Module: Vector2d::Calculations::ClassMethods
- Included in:
- Vector2d
- Defined in:
- lib/vector2d/calculations.rb
Instance Method Summary collapse
-
#angle_between(vector1, vector2) ⇒ Object
Calculates angle between two vectors in radians.
-
#cross_product(vector1, vector2) ⇒ Object
Calculates cross product of two vectors.
-
#dot_product(vector1, vector2) ⇒ Object
Calculates dot product of two vectors.
Instance Method Details
#angle_between(vector1, vector2) ⇒ Object
Calculates angle between two vectors in radians.
v1 = Vector2d(2, 3)
v2 = Vector2d(4, 5)
Vector2d.angle_between(v1, v2) # => 0.0867..
32 33 34 35 36 |
# File 'lib/vector2d/calculations.rb', line 32 def angle_between(vector1, vector2) one = vector1.normalized? ? vector1 : vector1.normalize two = vector2.normalized? ? vector2 : vector2.normalize Math.acos(dot_product(one, two)) end |
#cross_product(vector1, vector2) ⇒ Object
Calculates cross product of two vectors.
v1 = Vector2d(2, 1)
v2 = Vector2d(2, 3)
Vector2d.cross_product(v1, v2) # => 4
12 13 14 |
# File 'lib/vector2d/calculations.rb', line 12 def cross_product(vector1, vector2) (vector1.x * vector2.y) - (vector1.y * vector2.x) end |
#dot_product(vector1, vector2) ⇒ Object
Calculates dot product of two vectors.
v1 = Vector2d(2, 1)
v2 = Vector2d(2, 3)
Vector2d.dot_product(v1, v2) # => 10
22 23 24 |
# File 'lib/vector2d/calculations.rb', line 22 def dot_product(vector1, vector2) (vector1.x * vector2.x) + (vector1.y * vector2.y) end |