Created
January 30, 2017 13:26
-
-
Save FredrikAugust/cf239a4656dae37958d9870e9d1bb67f to your computer and use it in GitHub Desktop.
Euclidean distance in Ruby (2-dimensional)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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