Method: Geometry::Vector#angle

Defined in:
lib/geometry/vector/vector.rb

#angle(other) ⇒ Object

Return the angle (in radians) between self and the passed in vector.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/geometry/vector/vector.rb', line 99

def angle(other)

  # Two options here:
  #
  # 1.  Math.atan2(other.cross_length(self), dot(other)) 
  #
  #     This is stable but slower (x 1.5)
  #
  # 2.  Math.acos(dot(other) / (r * other.r)) 
  #
  #     This is faster but unstable around 0 and pi where the gradient of acos approaches 
  #     infinity. An alternative way to view this is that the gradient of cos approaches
  #     zero and small differences in angle can be indistinguishable at some number of 
  #     decimal places.
  #
  
  # Math.acos(dot(other) / (r * other.r)) 
  Math.atan2(other.cross_length(self), dot(other)) 
end