Skip to content

Latest commit

 

History

History
30 lines (23 loc) · 785 Bytes

geometry_basics_distance_between_points_in_2d.md

File metadata and controls

30 lines (23 loc) · 785 Bytes

Description

This series of katas will introduce you to basics of doing geometry with computers.

Point objects have attributes x and y.

Write a function calculating distance between Point a and Point b.

Input coordinates fit in range −50 ⩽ 𝑥,𝑦 ⩽ 50. Tests compare expected result and actual answer with tolerance of 1e-6.

def distance_between_points(a, b)
  return 0 # your code here
end

My Solution

def distance_between_points(a, b)
  Math.sqrt((a.x - b.x)**2 + (a.y - b.y)**2)
end

Better/Alternative solution from Codewars

def distance_between_points(a, b)
  Math.hypot(b.x - a.x, b.y - a.y)
end