Distance between two points
Read these first if they are new to you
The idea in one paragraph
To find how far apart two points are, subtract one from the other to get the arrow between them. The arrow's components are the sides of a right triangle, and the distance is the long side. Pythagoras' theorem gives it: square each component, add the squares, and take the square root. The same recipe works in 3D, with one more square to add.
The arrow between two points is the long side of a right triangle
Say point A is at (1, 2) and point B is at (4, 6). The arrow from A to B is B − A = (3, 4), as the vectors unit shows. Walk 3 squares right, then 4 squares up, and you reach B. Those two walks are the short sides of a right triangle. The straight line from A to B is its long side, the hypotenuse.
Pythagoras' theorem says that in a right triangle, the square of the long side equals the sum of the squares of the two short sides. So the distance between the points is:
d = √( (xB − xA)2 + (yB − yA)2 )
How far is A = (1, 2) from B = (4, 6)?
- Subtract: B − A = (4 − 1, 6 − 2) = (3, 4).
- Square each component: 32 = 9 and 42 = 16.
- Add: 9 + 16 = 25.
- Take the square root: √25 = 5.
Your turn. How far is (−1, 2) from (2, −2)?
Answer
The arrow is (3, −4). The squares are 9 and 16, which add to 25. The distance is 5. A negative component gives a positive square, so the direction of the arrow does not matter.
In 3D, add one more square
A point in a room has a height z as well. The distance between two such points uses Pythagoras twice: once across the floor, and once from the floor up. The result is one more square under the root:
d = √( Δx2 + Δy2 + Δz2 )
Here Δx (read "delta x") means the change in x between the two points. For example, from (0, 0, 0) to (2, 3, 6) the changes are 2, 3 and 6. The squares are 4, 9 and 36, which add to 49, so the distance is 7. The distance from a point to the origin is called the length of that vector.
To find which of several points is closest, you can compare the squared distances and skip the square root. A larger distance always has a larger square, so the order stays the same. Programs that search for the nearest point use this to save work.
This page measures straight-line distance, which mathematicians call Euclidean distance. Other measures exist. "City-block" distance adds the steps along each axis, like walking a street grid: 3 + 4 = 7 in the example above. When a program says "distance" without a qualifier, it usually means the Euclidean one.
distance.py: np.linalg.norm computes the length of a vector
import numpy as np
a = np.array([1, 2])
b = np.array([4, 6])
d = b - a # [3 4]
print(np.sqrt(np.sum(d ** 2))) # Pythagoras by hand
print(np.linalg.norm(b - a)) # the same, in one call
p = np.array([0, 0, 0])
q = np.array([2, 3, 6])
print(np.linalg.norm(q - p)) # works in 3D too
5.0 5.0 7.0
Check yourself
-
How far is (1, 1, 1) from (3, 3, 2)?
Answer
The changes are 2, 2 and 1. The squares add to 4 + 4 + 1 = 9, so the distance is 3.
-
Point P has squared distance 50 from the origin, and point Q has squared distance 41. Which one is closer, without taking any square root?
Answer
Q. A smaller squared distance always means a smaller distance.
-
Is the distance from A to B ever different from the distance from B to A?
Answer
No. Swapping the points flips the sign of every component of the arrow, and squaring removes the sign.
Go further
Optional background, for readers who want more than this page.
-
Drag a triangle and watch the squares on its sides; shows why the theorem holds.
-
The same formula for any number of components. A later section, "Squared Euclidean distance", covers the squared form.
Where this shows up
Units that build on this one
- Normalizing data Basic
- Nearest-neighbor search Intermediate
- Farthest-point sampling Intermediate
- Matching two sets: the assignment problem Intermediate
- 3D images made of voxels Intermediate
- Loss: one number for how wrong Intermediate
- The dot product: how much two vectors agree Basic