Sub topic 12
The Distance Between Any Two Points
What this chapter assumes
You should be able to plot and read points in all four quadrants. You should be able to find a horizontal or vertical distance by subtracting coordinates. You should know the Pythagorean theorem: in a right triangle with legs a and b and hypotenuse c, a² + b² = c². You should also be comfortable with square roots.
The earlier chapter on horizontal and vertical distances handled segments that run along a grid line. This chapter removes that restriction and handles a segment at any angle.
From axis distances to a slanted segment
Take two points, P = (x1, y1) and Q = (x2, y2). If they share a y-coordinate, the segment is horizontal and its length is the difference of the x-coordinates. If they share an x-coordinate, the segment is vertical and its length is the difference of the y-coordinates. Most pairs of points share neither, so the segment between them is slanted.
You can still use the two easy distances. From P, move horizontally until you are directly above or below Q. This gives a third point R = (x2, y1). The three points P, R and Q form a right triangle:
- Segment PR is horizontal, so its length is
|x2 - x1|. - Segment RQ is vertical, so its length is
|y2 - y1|. - The right angle is at R, because a horizontal line and a vertical line are perpendicular.
- The slanted segment PQ is the hypotenuse, and its length is the distance you want.
Deriving the formula
Apply the Pythagorean theorem to triangle PRQ. Call the horizontal leg dx = x2 - x1 and the vertical leg dy = y2 - y1. Let d be the length of the hypotenuse:
d² = dx² + dy²
d = sqrt(dx² + dy²)
d = sqrt((x2 - x1)² + (y2 - y1)²)This is the distance formula. It does not depend on the picture, so it works in every quadrant and for every slope.
A worked example
Find the distance between A = (1, 2) and B = (4, 6).
- Horizontal change:
dx = 4 - 1 = 3. - Vertical change:
dy = 6 - 2 = 4. - Square and add:
3² + 4² = 9 + 16 = 25. - Take the square root:
d = sqrt(25) = 5.
The legs 3 and 4 and the hypotenuse 5 form a Pythagorean triple. Triples such as (3, 4, 5), (5, 12, 13) and (6, 8, 10) are convenient for checking your work, because the root comes out as a whole number.
A worked example with negative coordinates
Find the distance between C = (-3, 4) and D = (2, -8).
dx = 2 - (-3) = 5.dy = -8 - 4 = -12.dx² + dy² = 25 + 144 = 169.d = sqrt(169) = 13.
The vertical change is negative because D is below C. The sign disappears when you square it, so it causes no trouble.
Why the order and the signs do not matter
Two properties follow from squaring.
- Order does not matter. Swapping P and Q changes
dxto-dxanddyto-dy. The squares are unchanged, so the distance from P to Q equals the distance from Q to P. - Signs do not matter. A negative difference gives the same square as the matching positive one. You never need absolute value bars inside the formula.
The table shows several pairs worked the same way.
| Point P | Point Q | dx | dy | dx² + dy² | d |
|---|---|---|---|---|---|
| (0, 0) | (3, 4) | 3 | 4 | 25 | 5 |
| (1, 2) | (4, 6) | 3 | 4 | 25 | 5 |
| (-3, 4) | (2, -8) | 5 | -12 | 169 | 13 |
| (-2, -1) | (4, 7) | 6 | 8 | 100 | 10 |
| (2, 5) | (2, -1) | 0 | -6 | 36 | 6 |
The last row is a vertical segment. When dx = 0, the formula reduces to sqrt(dy²) = |dy|, which is the vertical-distance rule from the earlier chapter. The same happens for a horizontal segment. The distance formula therefore contains the axis-distance rules as special cases, and you do not need to choose between them.
If P and Q are the same point, both differences are 0 and the distance is 0. This is the only case where the distance is 0.
Implementing the distance in Python
The formula translates directly into code. The function below keeps the squared distance in its own function, because the next section uses it.
import math
def dist_sq(p, q):
return (q[0] - p[0]) ** 2 + (q[1] - p[1]) ** 2
def distance(p, q):
return math.sqrt(dist_sq(p, q))
print(distance((1, 2), (4, 6)))
print(distance((-3, 4), (2, -8)))
print(distance((-2, -1), (4, 7)))
print(distance((0, 0), (1, 1)))
print(math.dist((1, 2), (4, 6)))The output is:
5.0
13.0
10.0
1.4142135623730951
5.0The standard library function math.dist computes the same quantity, and it accepts points with any number of coordinates. Writing your own version once is useful because it shows what the library call does.
The last distance, from (0, 0) to (1, 1), is sqrt(2). It is irrational, so Python prints an approximation. In written work, leave the answer as sqrt(2) when an exact value is wanted, or round it (about 1.41) when a measurement is wanted.
Comparing distances without a square root
Sometimes you only need to know which of two distances is larger. The square root is increasing, so d1 < d2 exactly when d1² < d2². You can compare squared distances and skip the root. This is exact when the coordinates are integers, whereas rounded square roots can hide small differences.
def dist_sq(p, q):
return (q[0] - p[0]) ** 2 + (q[1] - p[1]) ** 2
origin = (0, 0)
a = (3, 4)
b = (5, 1)
print(dist_sq(origin, a))
print(dist_sq(origin, b))
print(dist_sq(origin, a) < dist_sq(origin, b))The output is 25, then 26, then True. Point a is at distance 5 and point b is at distance sqrt(26), about 5.10, so a is closer to the origin.
The same idea tests for a right angle. A triangle is a right triangle if the squared side lengths satisfy a² + b² = c² for its longest side c. With vertices (0, 0), (4, 0) and (0, 3), the squared sides are 16, 9 and 25. Since 16 + 9 = 25, the angle at (0, 0) is a right angle. This is the converse of the Pythagorean theorem.
Common errors
- Forgetting to take the square root.
dx² + dy²is the squared distance. For the points (0, 0) and (3, 4) it is 25, and the distance is 5. - Adding before squaring. The expression
(dx + dy)²is notdx² + dy². Fordx = 3anddy = 4, the first is 49 and the second is 25. - Taking the root of each term.
sqrt(dx² + dy²)is not|dx| + |dy|. For (0, 0) to (3, 4), the second gives 7 instead of 5. The sum of the two legs is always at least the straight-line distance, and it is only equal when the segment is horizontal or vertical. - Mixing coordinates from different points. Subtract x from x and y from y. Writing
x2 - y1is a common slip when copying values. - Sign mistakes with negatives. Subtracting a negative number adds. For
x1 = -3andx2 = 2,dx = 2 - (-3) = 5, not-1. Put the negative value in parentheses before you subtract. - Reading distance from a distorted plot. If the plotted axes use different scales on screen, a segment can look longer or shorter than it is. Compute the distance from the coordinates. In matplotlib,
ax.set_aspect("equal")makes the picture match the numbers.
Where the formula leads
Distance is the tool for measuring figures on the plane. Once you can find the length of any segment, you can find the perimeter of a polygon by adding the lengths of its sides. You can also check whether a triangle is isosceles by comparing two squared sides. The next chapters on midpoints and polygons build on this step. The midpoint formula averages coordinates, and the distance formula is the measurement you apply to the two halves it creates.
Exercises
Exercise
Find the distance between P = (-2, 3) and Q = (4, -5). Give the exact value.
Show answer
dx = 4 - (-2) = 6 and dy = -5 - 3 = -8. Then dx² + dy² = 36 + 64 = 100, so d = sqrt(100) = 10.
Exercise
A triangle has vertices (0, 0), (6, 0) and (0, 8). Find the length of each side and the perimeter.
Show answer
The side from (0, 0) to (6, 0) is horizontal with length 6. The side from (0, 0) to (0, 8) is vertical with length 8. The third side has dx = -6 and dy = 8, so d = sqrt(36 + 64) = sqrt(100) = 10. The perimeter is 6 + 8 + 10 = 24.
Exercise
Without computing any square roots, decide which point, (2, 3) or (-1, -3), is closer to the origin, and state the distance to (2, 3) as an exact value.
Show answer
The squared distances are 2² + 3² = 13 and (-1)² + (-3)² = 1 + 9 = 10. Since 10 < 13, the point (-1, -3) is closer. The distance to (2, 3) is sqrt(13), which is about 3.61.