Module: Vector2d::Calculations

Includes:
Contracts
Included in:
Vector2d
Defined in:
lib/vector2d/calculations.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Object



52
53
54
# File 'lib/vector2d/calculations.rb', line 52

def *(other)
  calculate_each(:*, other)
end

#+(other) ⇒ Object



72
73
74
# File 'lib/vector2d/calculations.rb', line 72

def +(other)
  calculate_each(:+, other)
end

#-(other) ⇒ Object



82
83
84
# File 'lib/vector2d/calculations.rb', line 82

def -(other)
  calculate_each(:-, other)
end

#/(other) ⇒ Object



62
63
64
# File 'lib/vector2d/calculations.rb', line 62

def /(other)
  calculate_each(:/, other)
end

#angle_between(other) ⇒ Object



142
143
144
145
# File 'lib/vector2d/calculations.rb', line 142

def angle_between(other)
  v, _ = coerce(other)
  self.class.angle_between(self, v)
end

#cross_product(other) ⇒ Object



130
131
132
133
# File 'lib/vector2d/calculations.rb', line 130

def cross_product(other)
  v, _ = coerce(other)
  self.class.cross_product(self, v)
end

#distance(other) ⇒ Object



93
94
95
# File 'lib/vector2d/calculations.rb', line 93

def distance(other)
  Math.sqrt(squared_distance(other))
end

#dot_product(other) ⇒ Object



118
119
120
121
# File 'lib/vector2d/calculations.rb', line 118

def dot_product(other)
  v, _ = coerce(other)
  self.class.dot_product(self, v)
end

#squared_distance(other) ⇒ Object



104
105
106
107
108
109
# File 'lib/vector2d/calculations.rb', line 104

def squared_distance(other)
  v, _ = coerce(other)
  dx = v.x - x
  dy = v.y - y
  dx * dx + dy * dy
end