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
44 45 46 |
# File 'lib/vector2d/calculations.rb', line 44 def *(other) calculate_each(:*, other) end |
#+(other) ⇒ Object
62 63 64 |
# File 'lib/vector2d/calculations.rb', line 62 def +(other) calculate_each(:+, other) end |
#-(other) ⇒ Object
71 72 73 |
# File 'lib/vector2d/calculations.rb', line 71 def -(other) calculate_each(:-, other) end |
#/(other) ⇒ Object
53 54 55 |
# File 'lib/vector2d/calculations.rb', line 53 def /(other) calculate_each(:/, other) end |
#angle_between(other) ⇒ Object
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
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
81 82 83 |
# File 'lib/vector2d/calculations.rb', line 81 def distance(other) Math.sqrt(squared_distance(other)) end |
#dot_product(other) ⇒ Object
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
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 |