Skip to content

Instantly share code, notes, and snippets.

@FredrikAugust
Created January 30, 2017 13:26
Show Gist options
  • Save FredrikAugust/cf239a4656dae37958d9870e9d1bb67f to your computer and use it in GitHub Desktop.
Save FredrikAugust/cf239a4656dae37958d9870e9d1bb67f to your computer and use it in GitHub Desktop.
Euclidean distance in Ruby (2-dimensional)
# Euclidean distance in ruby (2-dimensional)
# The points have to be in the format [x, y]
def euclidean_distance(point1, point2)
# First; group the x's and y's, then sum the squared difference in x's and y's
Math.sqrt(point1.zip(point2).reduce(0) { |sum, p| sum + (p[0] - p[1]) ** 2 })
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment