Method: Math.distance
- Defined in:
- lib/standard/facets/math/distance.rb
.distance(p, q) ⇒ Object
Calculates the Euclidean Distance between points p and q.
‘p`, `q` is assumed to described coordinates in N-dimensions, e. g.:
Math.distance([1, 1], [2, 2]) # 2D coordinates
Math.distance([1, 1, 1], [2, 2, 2]) # 3D coordinates
If N is 1, then ‘::distance` may also be invoked like so:
Math.distance(1, 1)
14 15 16 17 |
# File 'lib/standard/facets/math/distance.rb', line 14 def self.distance(p, q) p, q = [p].flatten, [q].flatten sqrt(p.zip(q).inject(0){ |sum, coord| sum + (coord.first - coord.last)**2 }) end |