The distance formula¶
Level: 101 · for anyone starting precalculus
One line: The distance formula is the Pythagorean theorem with the legs read off the coordinates, and its squares do a second job: they throw away the signs, which is why the order of the points never matters and no absolute value is needed.
The idea¶
Two points, P₁ = (x₁, y₁) and P₂ = (x₂, y₂). How far apart are they? Add a third point, P₃ = (x₂, y₁): it has P₂'s x and P₁'s y, so it sits level with P₁ and directly below (or above) P₂. The three points make a right triangle with the right angle at P₃. Its horizontal leg is |x₂ − x₁| long and its vertical leg |y₂ − y₁|, because a horizontal distance is a difference of x-coordinates and a vertical one a difference of y-coordinates. Pythagoras does the rest:
d(P₁, P₂) = √((x₂ − x₁)² + (y₂ − y₁)²)
In words: subtract the x's and square, subtract the y's and square, add, take the square root.
The book's Example 1 is exactly this: from (1, 3) to (5, 6), the corner is (5, 3), the legs are 4 and 3, and the distance is 5. Written out as a proof, ten lines each with its reason and the horizontal and vertical pairs as cases of their own, the same argument is the worked example of what a proof is.
This page follows the opening of section 1.1, The Distance and Midpoint Formulas, in Michael Sullivan's Precalculus, which sends the reader back to Appendix A.2 for the Pythagorean theorem first.
Symbol by symbol¶
| Piece | Say it | What it means |
|---|---|---|
| P₁, P₂ | "P one, P two" | two points, named with subscripts so that their coordinates can be x₁, y₁ and x₂, y₂. The subscript is a label, not a power. |
| d(P₁, P₂) | "the distance from P one to P two" | a number, never negative. The notation reads like a function of two points, and it is one. |
| x₂ − x₁ | "x two minus x one" | the horizontal change, which may be negative (moving left). |
| (x₂ − x₁)² | its square, which is never negative. | |
| √ | "square root" | the non-negative root: d is a distance. |
What the squares are doing¶
Two things at once. They are Pythagoras, the squares on the legs. And they erase direction: x₂ − x₁ and x₁ − x₂ are negatives of each other, and squaring cannot tell them apart. Three consequences follow, and each is a small theorem:
- Order does not matter. d(P₁, P₂) = d(P₂, P₁), so you can call either point "1".
- No absolute values needed, though the legs are |x₂ − x₁| and |y₂ − y₁|: |t|² = t² for every t.
- Distance from the origin. Put P₁ = (0, 0): the distance of a point (x, y) from the origin is √(x² + y²), the formula with nothing to subtract. It is the length of the arrow from the origin to the point, called the length of the vector (x, y) in linear algebra and the absolute value |x + yi| in the complex numbers; exercise 3 below works it. Axler states this special case on its own line, the other books leave it to the reader.
- Horizontal and vertical segments are not special cases. If y₁ = y₂, the formula gives √((x₂ − x₁)²) = |x₂ − x₁|, the absolute value, because √(t²) = |t|, not t. The one-dimensional distance on a number line, |b − a|, is this formula with one coordinate.
Exact answers¶
d² is always exact when the coordinates are whole numbers or fractions. d is exact only when d² is a perfect square, which the book arranges in its examples and life does not. Otherwise the answer is left as a square root in simplest form, √20 = √(4 · 5) = 2√5, and a decimal only at the end. Better still: to compare distances, compare their squares. Squaring keeps order among non-negative numbers, so d(A, B) < d(A, C) exactly when d(A, B)² < d(A, C)², and no square root is ever taken. This is how the book's next example decides whether three points make a right triangle: by the converse of the Pythagorean theorem, using squared distances only.
What the program prints¶
Verified output of distance_formula.py — regenerated by tools/run_examples.py, never hand-typed.
1. EXAMPLE 1: THE CORNER POINT MAKES A RIGHT TRIANGLE
P1 = (1, 3), P2 = (5, 6); the corner P3 = (x2, y1) = (5, 3).
Horizontal leg P1P3: |5 - 1| = 4. Vertical leg P3P2: |6 - 3| = 3.
d^2 = 4^2 + 3^2 = 25, d = 5.
2. THE ORDER OF THE POINTS DOES NOT MATTER
from (1, 3) to (5, 6): x2 - x1 = 4, y2 - y1 = 3, squares 16 + 9 = 25
from (5, 6) to (1, 3): x2 - x1 = -4, y2 - y1 = -3, squares 16 + 9 = 25
Swapping the points flips both signs, and squaring cannot see a sign.
So (x2 - x1)^2 = (x1 - x2)^2 and no absolute value is needed.
3. HORIZONTAL AND VERTICAL: THE FORMULA STILL WORKS
(-2, 4) to (5, 4): d = sqrt(49) = 7
(3, -1) to (3, -7): d = sqrt(36) = 6
One leg is 0, so d = sqrt(t^2) for a single difference t, and sqrt(t^2) = |t|,
not t: going down from y = -1 to y = -7, t = -6, and the distance is 6.
4. MOST DISTANCES ARE NOT WHOLE NUMBERS
(-4, 5) to (3, 2) d^2 = 58 d = sqrt(58) = 7.6158
(0, 0) to (1, 1) d^2 = 2 d = sqrt(2) = 1.4142
(1/2, 0) to (0, 1/2) d^2 = 1/2 d = 1/2 sqrt(2) = 0.7071
(-1, -2) to (2, 2) d^2 = 25 d = 5
d^2 is always exact. d is exact only when d^2 is a perfect square;
otherwise the book asks for the radical in simplest form, and so does this.
5. THE CONVERSE AT WORK: IS A = (-2, 1), B = (2, 3), C = (3, 1) A RIGHT TRIANGLE?
d(A,B)^2 = 20, d(B,C)^2 = 5, d(A,C)^2 = 25
20 + 5 = 25 = d(A,C)^2, so yes: the right angle is at B,
opposite the longest side AC. Area = (1/2) sqrt(20) sqrt(5) = (1/2) sqrt(100) = 5.
Squared distances are enough to decide; no square root was taken until the area.
6. THREE POINTS ON A LINE: THE TWO SHORT DISTANCES ADD UP
P = (0, 0), Q = (3, 4), R = (6, 8): d(P,Q) = 5, d(Q,R) = 5, d(P,R) = 10
Move Q to (3, 5): d(P,Q) + d(Q,R) = 10.0736 > 10.0000 = d(P,R).
A detour is always longer: the triangle inequality, with equality only on the line.
Section 6 is the triangle inequality, seen from the coordinates: the straight path from P to R is never longer than a detour through Q, and it is equal only when Q lies on the segment. It gives a test for three points being on one line that needs no slope.
To compute a distance of your own, give two points after the program name; fractions are fine:
python3 08_Analytic_Geometry/distance_formula/examples/distance_formula.py 1,3 5,6
python3 08_Analytic_Geometry/distance_formula/examples/distance_formula.py -1/2,3 4,-1
Questions¶
1. Find the distance between (−4, 5) and (3, 2).
Answer
(3 − (−4))² + (2 − 5)² = 49 + 9 = 58, so d = √58 ≈ 7.62. 58 = 2 · 29 has no square factor, so √58 is already simplest.2. Someone computes the distance from (2, 7) to (5, 3) as √((2 − 5)² + (7 − 3)²). The other person uses (5 − 2) and (3 − 7). Who is right?
Answer
Both, and they get the same √(9 + 16) = 5: each difference only changes sign, and the square erases the sign. Even a mixed order, (5 − 2) with (7 − 3), would survive here, but only because of the squares. Keep one order anyway: slope, two sections on, divides one difference by the other, and a mixed order there flips the sign of the answer.3. Find the distance between (0, 0) and (a, b).
Answer
√(a² + b²). This is the distance of a point from the origin, and in [the complex numbers](../../03_Complex_Numbers/multiplication_rotates/README.md) it is the absolute value |a + bi|.4. Why is √((x₂ − x₁)²) equal to |x₂ − x₁| and not x₂ − x₁?
Answer
The square root sign means the non-negative root. If x₂ − x₁ = −6, its square is 36 and √36 = 6 = |−6|. √(t²) = |t| for every real t.5. Is (x₂ − x₁)² + (y₂ − y₁)² the same as (x₂ − x₁ + y₂ − y₁)²? Test on (0, 0) and (3, 4).
Answer
No: 9 + 16 = 25 against (3 + 4)² = 49. Squaring does not split over a sum. Each difference is squared on its own.6. Is √(a² + b²) = a + b? Test with a = 3, b = 4.
Answer
No: √25 = 5, not 7. The square root does not split over a sum either. This is the most common algebra error in the whole chapter.7. Do A = (−2, 1), B = (2, 3), C = (3, 1) form a right triangle? Where is the right angle?
Answer
Squared distances: AB² = 16 + 4 = 20, BC² = 1 + 4 = 5, AC² = 25 + 0 = 25. The longest is AC², and 20 + 5 = 25, so yes. The right angle is at B, opposite the longest side AC. No square root was needed.8. Which point is closer to the origin, (5, −1) or (−3, 4)?
Answer
Compare squares: 25 + 1 = 26 against 9 + 16 = 25. (−3, 4) is closer, by a hair. √ was never needed.9. Find all points on the x-axis at distance 5 from (1, 3).
Answer
A point on the x-axis is (x, 0). (x − 1)² + (0 − 3)² = 25 gives (x − 1)² = 16, so x − 1 = ±4: x = 5 or x = −3. Two points, (5, 0) and (−3, 0), as a circle of radius 5 around (1, 3) would cut the axis twice.10. Are (0, 0), (3, 4) and (6, 8) on one line? Use distances only.
Answer
d(first, second) = 5, d(second, third) = 5, d(first, third) = 10. The two short ones add up to the long one exactly, so the middle point is on the segment: yes. If the sum were bigger, the three would make a real triangle.11. The distance formula uses the Pythagorean theorem. Where, exactly, is the right angle?
Answer
At P₃ = (x₂, y₁), where a horizontal line through P₁ meets a vertical line through P₂. The axes are perpendicular, so horizontal and vertical lines are too. On a graph whose axes are not at right angles, the formula would be wrong.12. What does the formula give when P₁ = P₂? What does that say about the distance being zero?
Answer
0. And conversely, a sum of two squares is 0 only when both are 0, so d = 0 only when the points are the same point.How to practise¶
- Do the corner first. For the first ten problems, write P₃ = (x₂, y₁) and the two legs before using the formula. After that, skip it, but you will know why it works.
- Signs in brackets. Write (3 − (−4)), with the brackets, every time a coordinate is negative. Most wrong answers are a lost minus sign.
- Squares to compare, roots to report. Decide "which is closer" and "is it right-angled" with d²; take the root only when a length is asked for.
- Check with the program, and do the cards,
traptag first.
Cross-references¶
The ideas behind the formula, beside it, and built on it, each linked to where it lives here.
| Where it sits | Term | Here |
|---|---|---|
| Underneath | Pythagorean theorem, right triangle, hypotenuse | the theorem, right triangle, hypotenuse; the page the Pythagorean theorem and its converse |
| Underneath | Rectangular coordinates, ordered pair, perpendicular axes | rectangular coordinates, ordered pair; the page rectangular coordinates |
| Underneath | Distance on a number line, |b − a|, signed distance | signed distance |
| Underneath | Absolute value, |t|² = t², √(t²) = |t| | absolute value; the lemma in what a proof is, and question 4 above |
| Underneath | Principal square root, simplest radical form | the section exact answers above |
| Itself | Distance formula, Euclidean distance, d(P₁, P₂) | distance formula |
| Itself | Metric: non-negative, zero only for one point, symmetric, triangle inequality | metric, triangle inequality; the other three are questions 2, 12 and section 2 of the program |
| Itself | Squared distance, comparing without the root | exact answers above |
| Beside | Modulus of a complex number, |z| | complex multiplication, geometrically; the page multiplication rotates |
| Beside | Polar coordinates, r = √(x² + y²) | polar form |
| Beside | Law of cosines, the general triangle | law of cosines; which comes first, Pythagoras or the law of cosines |
| Beside | Length of a vector, Euclidean norm, |v| = √(v·v) | Euclidean norm; the page distance in n dimensions |
| Built on it | Midpoint formula | midpoint; the page the midpoint formula |
| Built on it | Circle, standard form (x − h)² + (y − k)² = r² | circle, unit circle; the page circles |
| Built on it | Converse of Pythagoras, right-angle test by squared distances | converse, Pythagorean triple; question 7 above |
| Built on it | Collinearity by distances, perpendicular bisector, locus | collinear; question 10 above; lines and slope for perpendicular lines |
| Built on it | Distance in three and n dimensions, metric | the page distance in n dimensions; metric |
| Beside | Other distances: taxicab, Chebyshev, Hamming | taxicab distance, Chebyshev distance, Hamming distance; the page distance is the four properties |
| The proof | Theorem, proof, given, lemma, proof by cases, counterexample | theorem, proof, proof by cases, counterexample; the page what a proof is |
Flashcards¶
The page as a deck of Anki cards: distance_formula.txt. Import with File → Import. Tags: formula, why, problems, trap.
Where this goes next¶
The midpoint of a segment is the rest of section 1.1, and this formula is how its answer is checked. Then the graph of an equation, lines, and circles, and a circle is this formula held fixed: all points (x, y) at distance r from a center (h, k), so (x − h)² + (y − k)² = r².
Po polsku, w skrócie¶
Wzór na odległość dwóch punktów to twierdzenie Pitagorasa zapisane we współrzędnych. Trzeci punkt, (x₂, y₁), leży na tej samej wysokości co pierwszy i dokładnie pod drugim, więc trzy punkty tworzą trójkąt prostokątny. Przyprostokątne to różnice współrzędnych, |x₂ − x₁| i |y₂ − y₁|, a odległość to przeciwprostokątna: d = √((x₂ − x₁)² + (y₂ − y₁)²).
Kwadraty robią tu dwie rzeczy: są Pitagorasem i kasują znaki. Dlatego kolejność punktów nie ma znaczenia i nie potrzeba wartości bezwzględnej; √(t²) = |t|, a nie t. Najczęstsze błędy: zgubiony minus przy ujemnej współrzędnej i „pierwiastek z sumy to suma pierwiastków", co jest nieprawdą: √(9 + 16) = 5, nie 7. Żeby porównać odległości, wystarczy porównać ich kwadraty i pierwiastka w ogóle nie liczyć. Program sprawdza to wszystko na dokładnych liczbach i liczy odległość dowolnych dwóch punktów podanych w wierszu poleceń.
Auf Deutsch: Stichwörter¶
Der Abstand zweier Punkte ist Pythagoras auf den Koordinatendifferenzen; Vorzeichen und Reihenfolge fallen unter dem Quadrat weg, und Abstände vergleicht man quadriert.
Stichwörter: Abstandsformel, Satz des Pythagoras, Koordinatendifferenz, Betrag, Quadratwurzel, Abstand vom Ursprung, Vergleich quadrierter Abstände.
See also¶
- The Pythagorean theorem and its converse — the theorem this formula is, and the converse used in question 7
- Rectangular coordinates — the plane, and why a horizontal distance is a difference of x-coordinates
- Distance in n dimensions, and the length of a vector — the same formula with a third square, and √(v·v)
- Distance is the four properties — what else counts as a distance, and why geometry picked this one
- What a proof is — this formula's argument as ten lines with reasons, which assumption the right angle costs, and why the program's checks are evidence rather than proof
- Multiplication rotates — the distance from the origin as the length of a complex number
- Precalculus: a reading guide — where section 1.1 sits in the course
- Euclidean distance ↗ — Wikipedia, with the same formula in any number of dimensions
- Odległość ↗ — Wikipedia po polsku
- Michael Sullivan, Precalculus (Pearson), section 1.1 The Distance and Midpoint Formulas; this page follows its first objective
- Ron Larson, Precalculus (Cengage), section 1.1, pages 4–5: the derivation with a reason beside every line, the converse of the Pythagorean theorem used to verify a right triangle (Example 4), and the special cases left to Exercise 62
- James Stewart, Lothar Redlin and Saleem Watson, Precalculus: Mathematics for Calculus (Cengage), section 1.9, pages 93–94: the same formula derived from the number-line distance |b − a|, with the absolute values shown to drop under the square; compared with Sullivan in the reading guide