Module: Vector2d::Calculations
- Included in:
- Vector2d
- Defined in:
- lib/vector2d/calculations.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#*(other) ⇒ Object
Multiplies vectors.
-
#+(other) ⇒ Object
Adds vectors.
-
#-(other) ⇒ Object
Subtracts vectors.
-
#/(other) ⇒ Object
Divides vectors.
-
#angle_between(other) ⇒ Object
Angle in radians between this vector and another vector.
-
#cross_product(other) ⇒ Object
Cross product of this vector and another vector.
-
#distance(other) ⇒ Object
Calculates the distance between two vectors.
-
#dot_product(other) ⇒ Object
Dot product of this vector and another vector.
-
#squared_distance(other) ⇒ Object
Calculate squared distance between vectors.
Instance Method Details
#*(other) ⇒ Object
Multiplies vectors.
Vector2d(1, 2) * Vector2d(2, 3) # => Vector2d(2, 6)
Vector2d(1, 2) * 2 # => Vector2d(2, 4)
44 45 46 |
# File 'lib/vector2d/calculations.rb', line 44 def *(other) calculate_each(:*, other) end |
#+(other) ⇒ Object
Adds vectors.
Vector2d(1, 2) + Vector2d(2, 3) # => Vector2d(3, 5)
Vector2d(1, 2) + 2 # => Vector2d(3, 4)
62 63 64 |
# File 'lib/vector2d/calculations.rb', line 62 def +(other) calculate_each(:+, other) end |
#-(other) ⇒ Object
Subtracts vectors.
Vector2d(2, 3) - Vector2d(2, 1) # => Vector2d(0, 2)
Vector2d(4, 3) - 1 # => Vector2d(3, 2)
71 72 73 |
# File 'lib/vector2d/calculations.rb', line 71 def -(other) calculate_each(:-, other) end |
#/(other) ⇒ Object
Divides vectors.
Vector2d(4, 2) / Vector2d(2, 1) # => Vector2d(2, 2)
Vector2d(4, 2) / 2 # => Vector2d(2, 1)
53 54 55 |
# File 'lib/vector2d/calculations.rb', line 53 def /(other) calculate_each(:/, other) end |
#angle_between(other) ⇒ Object
Angle in radians between this vector and another vector.
v1 = Vector2d(2, 3)
v2 = Vector2d(4, 5)
v1.angle_between(v2) # => 0.0867..
126 127 128 129 |
# File 'lib/vector2d/calculations.rb', line 126 def angle_between(other) v, = coerce(other) self.class.angle_between(self, v) end |
#cross_product(other) ⇒ Object
Cross product of this vector and another vector.
v1 = Vector2d(2, 1)
v2 = Vector2d(2, 3)
v1.cross_product(v2) # => 4
115 116 117 118 |
# File 'lib/vector2d/calculations.rb', line 115 def cross_product(other) v, = coerce(other) self.class.cross_product(self, v) end |
#distance(other) ⇒ Object
Calculates the distance between two vectors.
v1 = Vector2d(2, 3)
v2 = Vector2d(5, 6)
v1.distance(v2) # => 1.4142..
81 82 83 |
# File 'lib/vector2d/calculations.rb', line 81 def distance(other) Math.sqrt(squared_distance(other)) end |
#dot_product(other) ⇒ Object
Dot product of this vector and another vector.
v1 = Vector2d(2, 1)
v2 = Vector2d(2, 3)
v1.dot_product(v2) # => 10
104 105 106 107 |
# File 'lib/vector2d/calculations.rb', line 104 def dot_product(other) v, = coerce(other) self.class.dot_product(self, v) end |
#squared_distance(other) ⇒ Object
Calculate squared distance between vectors.
v1 = Vector2d(2, 3)
v2 = Vector2d(5, 6)
v1.distance_squared(v2) # => 18
91 92 93 94 95 96 |
# File 'lib/vector2d/calculations.rb', line 91 def squared_distance(other) v, = coerce(other) dx = v.x - x dy = v.y - y (dx * dx) + (dy * dy) end |