Files
news.gitea.happylizard.me/bin/gems/facets-3.1.0/lib/standard/facets/math/distance.rb
T
pizza2d1 4cee170d66
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 9s
Add bin and edit workflow
2026-09-16 13:11:16 -06:00

20 lines
544 B
Ruby

module Math
# 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)
#
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
end