Concept units

Distance between two points

Basic

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 )

The distance is the long side of the triangle the two points make
A B
A = (1, 2)   B = (4, 6)
√(3² + 4²) = √25 = 5
Try this. Drag point B. The blue side is the change in x, the orange side the change in y, and the green line the distance. B starts at (4, 6), exactly 5 from A. Goal: find the other two spots on this grid that are also exactly 5 from A, with whole-number coordinates, not level with A and not straight above it.
Worked example

How far is A = (1, 2) from B = (4, 6)?

  1. Subtract: B − A = (4 − 1, 6 − 2) = (3, 4).
  2. Square each component: 32 = 9 and 42 = 16.
  3. Add: 9 + 16 = 25.
  4. 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.

A shortcut when you only compare

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 is one kind of distance

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

  1. 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.

  2. 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.

  3. 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.

Where this shows up

Units that build on this one