Welcome back to Distance on the Coordinate Plane! This is the second lesson of the course, and you are building momentum. In Lesson 1, you learned how to find horizontal and vertical distances between points that line up along a single axis. That skill is about to become the foundation for something bigger.
In this lesson, we will take two points that do not line up horizontally or vertically, and we will build a right triangle that connects them. This construction is the key step that lets us use the Pythagorean theorem to find the straight-line distance between any two points on the plane. Let's get started.
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal
Finding the distance between two points is straightforward when they share an x-coordinate or a y-coordinate — you just subtract and take the absolute value. But what happens when neither coordinate matches?
Think about two street intersections in a city grid that are neither on the same avenue nor on the same street. To walk there on the grid, you would go some blocks east and then some blocks north. A bird, however, could fly in a straight diagonal line between the two. That diagonal path is shorter than your walking route, and it does not run along either axis.
Here is the core insight: your two grid-walking segments — one horizontal, one vertical — form two sides of a right triangle, and the bird's diagonal is the third side. To find the length of that diagonal, we need to build exactly this kind of triangle on the coordinate plane.
Consider the points (1,2) and (5,7). They differ in both their x-coordinates and their y-coordinates, so the segment between them slants across the plane. This slanted segment is the distance we ultimately want to find.
We cannot measure this diagonal by simple subtraction, because it does not follow a single axis. Instead, we will turn it into the hypotenuse of a right triangle whose two legs we can measure with the skills from Lesson 1.
Here is the construction process, using our points (1,2) and (5,7):
Draw the diagonal segment connecting (1,2) to (5,7). This is the side whose length we want.
Draw a horizontal segment from one point, moving along the x-direction until you are directly below or above the other point.
Draw a vertical segment to close the triangle by connecting the end of that horizontal segment to the second point.
Following these steps, we move horizontally from (1,2) to (5,2), and then vertically from (5,2) up to (5,7). The result is a right triangle with three vertices: (1,2), (5,2), and (5,7).
The point (5,2) is the right-angle vertex — the corner where the horizontal and vertical legs meet at 90°.
The right-angle vertex is always the point that completes the "L" shape. Its coordinates come from combining one coordinate of each original point. Given two points (x1​,y1​) and (x2​,y2​), the right-angle corner sits at either:
(x2​,y1​) — taking the x-value of the second point and the y-value of the first, or
(x1​,y2​) — taking the x-value of the first point and the y-value of the second.
Both choices produce a valid right triangle with the same hypotenuse and the same leg lengths. For our points (1,2) and (5,7), the two possible corners are (5,2) and (1,7). In the construction above we used (5,2), but choosing (1,7) would give a triangle that is simply reflected to the other side of the diagonal.
The table below shows both corner options for several pairs of points, so you can see the pattern clearly.
Given Points
Corner Option A
Corner Option B
(1,2) and (5,7)
(5,2)
(1,7)
(−2,3) and (4,−1)
(4,3)
(−2,−1)
(0,0) and (3,4)
(3,0)
(0,4)
No matter which corner you pick, the two legs will have the same lengths, and the hypotenuse remains the diagonal segment between the original two points.
Once the triangle is built, labeling its parts is straightforward:
The horizontal leg runs left or right along a constant y-value. Its length is ∣x2​−x1​∣.
The vertical leg runs up or down along a constant x-value. Its length is ∣y2​−y1​∣.
The hypotenuse is the original diagonal segment connecting the two given points. It is always the longest side and sits opposite the right angle.
For (1,2) and (5,7) with the corner at (5,2), the horizontal leg measures ∣5−1∣=4 units and the vertical leg measures ∣7−2∣=5 units. The hypotenuse is the slanted segment from (1,2) to (5,7).
One detail worth remembering: the diagonal between the two original points is always the hypotenuse, never a leg. The legs are always the axis-aligned sides we create during the construction.
Let's confirm the process works just as smoothly when points cross into negative territory. Take (−2,3) and (4,−1).
Choosing the corner (4,3), we get:
Horizontal leg from (−2,3) to (4,3): ∣4−(−2)∣=6 units
Vertical leg from (4,3) to (4,−1): ∣−1−3∣=4 units
Hypotenuse: the diagonal from (−2,3) to (4,−1)
The absolute values handle the sign differences seamlessly. The construction itself does not change at all, regardless of which quadrants the points occupy.
In this lesson, you learned that any two points on the coordinate plane that do not share an x- or y-coordinate can be connected by a diagonal segment, and that segment becomes the hypotenuse of a right triangle. The two legs of that triangle are the horizontal and vertical distances between the points, and the right-angle vertex sits at one of two "corner" coordinates formed by mixing the x-value of one point with the y-value of the other.
This construction is the bridge between the simple axis-aligned distances from Lesson 1 and the full distance calculation you will tackle next using the Pythagorean theorem. Now head into the practice exercises to build these triangles with your own hands and lock the process into memory!