Skip to content

Instantly share code, notes, and snippets.

@kmcd
Created April 22, 2009 11:02
Show Gist options
  • Save kmcd/99722 to your computer and use it in GitHub Desktop.
Save kmcd/99722 to your computer and use it in GitHub Desktop.
def euclidean_distance(p1,p2)
sum_of_squares = 0
p1.each_with_index do |p1_coord,index|
sum_of_squares += (p1_coord - p2[index]) ** 2
end
Math.sqrt( sum_of_squares )
end
require 'test/unit'
class EuclideanDistanceTest < Test::Unit::TestCase
def test_distance_between_2_points_with_2_dimensions
assert_in_delta 2.828, euclidean_distance([1,2], [3,4]),0.001
end
def test_distance_between_2_points_with_3_dimensions
assert_in_delta 5.196, euclidean_distance([1,2,3], [4,5,6]),0.001
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment