Welcome to the final lesson of Distance on the Coordinate Plane! Over the first three lessons we built a reliable method for finding the straight-line distance between any two points: measure the horizontal and vertical legs, then apply the Pythagorean theorem. That four-step process works every time, but writing out each step can feel a bit lengthy once you have done it a dozen times. In this lesson we will package those steps into a single compact expression called the distance formula and see that it is simply the Pythagorean theorem rewritten in the language of coordinates.
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal
In Lesson 3 we followed the same sequence for every pair of points: find ∣x2​−x1​∣, find ∣y2​−y1​∣, square both and add, then take the square root. Because the pattern never changes, we can collapse it into one algebraic statement.
There is one simplification that makes this possible. As we noted at the end of Lesson 3, squaring a number removes any negative sign automatically. For instance, (−4)2 and 42 both equal 16. That means we no longer need absolute values around the coordinate differences — we can write (x2​−x1​)2 instead of ∣x2​−x1​∣2 and get the same result either way. This small insight is the key that lets us merge all four steps into a single formula.
Here is the distance between any two points (x1​,y1​) and (x2​,y2​):
d=(x2​−x1​)2+(y2​−y1​)2​
Every piece of this formula maps directly to the right triangle we have been building throughout the course:
Formula piece
Triangle part
(x2​−x1​)
Horizontal change; its absolute value is the horizontal leg a
(y2​−y1​)
Vertical change; its absolute value is the vertical leg b
(x2​−x1​)2
a2 (squared horizontal leg)
(y2​−y1​)2
b2 (squared vertical leg)
(x2​−x1​)2+(y2​−y1​)2
a2+b2=c2
The entire square root
c (the hypotenuse, i.e., the distance)
The unsquared differences (x2​−x1​) and (y2​−y1​) can be positive or negative depending on which point you label first, but their squares are always nonnegative — and equal to the squared leg lengths a2 and b2. That is what lets the formula stay clean without absolute-value bars.
Reading the formula from the inside out, it says: subtract coordinates to get two legs, square them, add, and take the square root. That is exactly the Pythagorean theorem a2+b2=c2, solved for c, with a and b replaced by coordinate differences. The distance formula is not a new idea — it is a repackaging of what you already know.
Let's apply the formula to a fresh pair of points: (2,1) and (8,9). We start by assigning labels:
The straight-line distance is 10 units. You might notice that 6, 8, and 10 form a scaled-up version of the 3-4-5 triple (each value doubled). Whenever the numbers under the radical add to a perfect square, the answer comes out as a whole number.
The most important step in the whole process is the substitution itself — plugging each coordinate into the correct spot. A common mistake is swapping an x-value for a y-value. A simple way to avoid this: write down the assignments (x1​=…, y1​=…, and so on) before touching the formula, just like we did above.
In Lesson 3 we found the distance between (−2,3) and (4,−1) using the triangle method and got 213​. Let's confirm that the formula produces the same result.
The result matches exactly. Notice that (−4)2 became 16, not −16. Squaring took care of the negative sign, just as we predicted — and this is precisely why the formula works in every quadrant without needing absolute values.
At this point you have two ways to find the distance between any two coordinate points:
The triangle method — draw or imagine the right triangle, find each leg with absolute-value subtraction, then apply a2+b2=c2.
The distance formula — substitute coordinates directly into d=(x2​−x1​)2+(y2​−y1​)2​.
Both approaches perform the same arithmetic in the same order. The triangle method makes each step visible, which is great for building understanding. The distance formula compresses those steps into one line, which is faster once you are comfortable with it. You are free to use whichever feels more natural; they will always produce the same answer because they are the same math.
Imagine you are a delivery dispatcher checking the straight-line distance between two addresses plotted as coordinates on a city grid. You could sketch a right triangle on the map and measure the legs, or you could punch the coordinates straight into the formula. Either way, you get the same number — and that number tells you the shortest possible route a drone could fly between those two stops.
One final note: it does not matter which point you call (x1​,y1​) and which you call (x2​,y2​). Reversing the labels only changes the sign inside each set of parentheses, and squaring eliminates that sign anyway.
In this lesson we condensed the four-step triangle method into the distance formula, d=(x2​−x1​)2+(y2​−y1​)2​. We saw that each piece of the formula corresponds to a specific part of the right triangle, and we confirmed that both approaches always produce the same result. The key takeaway is straightforward: the distance formula is the Pythagorean theorem, rewritten for coordinates.
Now it is time to put the formula into practice! In the upcoming exercises you will match formula pieces to triangle parts, fill in substitution blanks, compute distances from scratch, and even map out a delivery route on a city grid. Let's see how quickly the formula becomes second nature.