Distance Between Two Points

How far apart are two points on a grid? Suppose the first is at (x_1, y_1) and the second at (x_2, y_2). The horizontal gap between them is x_2 - x_1, and the vertical gap is y_2 - y_1.

Those two gaps are the legs of a right-angled triangle, and the straight-line distance between the points is its hypotenuse. So the distance is just Pythagoras' theorem applied to the grid:

d = \sqrt{(x_2-x_1)^2 + (y_2-y_1)^2} For points (x_1, y_1) and (x_2, y_2):

Seeing it on a grid

Take A = (1, 1) and B = (5, 4). The horizontal gap is 5 - 1 = 4 and the vertical gap is 4 - 1 = 3. Step through the figure: the two gaps form a right triangle, so the distance is \sqrt{4^2 + 3^2} = \sqrt{25} = 5.