What is the distance formula?
The distance formula gives you the straight-line distance between two points in a flat coordinate system. For two points in the plane, written as (x1, y1) and (x2, y2), the distance d between them is:
d = sqrt((x2 - x1)^2 + (y2 - y1)^2).
In plain English: subtract the x-coordinates of the two points, square the result; do the same with the y-coordinates; add the two squares; take the square root. The answer is the length of the straight line segment connecting the two points. That line is sometimes called the Euclidean distance, after the Greek mathematician Euclid, whose system of geometry is the one ordinary graph paper assumes.
Because both differences are squared, the result is always non-negative, and it does not matter which point you label as point 1 and which as point 2: the distance from A to B is the same as the distance from B to A. If you ever get a negative answer, you have made an arithmetic mistake.
Why the formula is true: the Pythagorean connection
The distance formula looks like it was invented just for coordinate geometry, but it is really the Pythagorean theorem wearing a different outfit. Plot the two points on graph paper and connect them with a straight segment. Then drop a horizontal line from the higher point and a vertical line from the other point until they meet at a right angle. You now have a right triangle whose hypotenuse is the segment you care about.
The horizontal leg of that triangle has length |x2 - x1|, the absolute value of the difference in x-coordinates. The vertical leg has length |y2 - y1|. The Pythagorean theorem says a^2 + b^2 = c^2 for any right triangle, so plugging in our two legs:
(x2 - x1)^2 + (y2 - y1)^2 = d^2.
Take the square root of both sides and you have the distance formula. That is why a separate page on the Pythagorean theorem and a page on the distance formula are really about the same idea: the formula is the theorem, written in coordinates.
Worked examples
Example 1 (basic). Find the distance between (1, 2) and (4, 6).
x2 - x1 = 4 - 1 = 3. y2 - y1 = 6 - 2 = 4. So d = sqrt(3^2 + 4^2) = sqrt(9 + 16) = sqrt(25) = 5. The two points are exactly 5 units apart. Notice that 3-4-5 is the most famous Pythagorean triple, which is why this example comes out so cleanly.
Example 2 (negative coordinates). Find the distance between (-2, 3) and (4, -5).
x2 - x1 = 4 - (-2) = 6. y2 - y1 = -5 - 3 = -8. d = sqrt(36 + 64) = sqrt(100) = 10. The minus signs only affect the differences, not the final result, because both differences are squared.
Example 3 (decimal answer). Find the distance between (0, 0) and (2, 5).
d = sqrt(4 + 25) = sqrt(29). Since 29 is not a perfect square, the exact answer is the radical sqrt(29), which is about 5.385 to three decimal places. Many real-world distances are irrational like this; round only at the end.
Extending to three dimensions
If your points live in 3D space, with coordinates (x1, y1, z1) and (x2, y2, z2), the formula extends naturally:
d = sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2).
You can derive this the same way: drop perpendiculars to form a right-angled triangle in space, where one leg is the 2D distance you would compute by ignoring z, and the other leg is the z difference. The Pythagorean theorem then gives the 3D distance.
For example, from the origin (0, 0, 0) to the point (3, 4, 12): d = sqrt(9 + 16 + 144) = sqrt(169) = 13. The triple (3, 4, 12, 13) is a 3D Pythagorean quadruple, the spatial cousin of the classic 3-4-5 triple.
The pattern continues into higher dimensions and is the foundation of how distance is measured in data science, where 'points' often live in dozens or thousands of dimensions.
Common mistakes
Forgetting the square root. The intermediate sum (x2 - x1)^2 + (y2 - y1)^2 is the squared distance, not the distance. You must take the square root before reporting the answer.
Squaring before subtracting. The formula uses the square of the difference, not the difference of the squares. (x2 - x1)^2 is not the same as x2^2 - x1^2. Subtract first, then square.
Treating distance as a signed value. Distance is always zero or positive. If your answer is negative, you almost certainly forgot to square one of the differences.
Using the formula on the curved surface of the Earth. The plain distance formula assumes flat space. For two points specified by latitude and longitude, you need the great-circle (haversine) formula, which accounts for the curvature of the planet. Treating GPS coordinates as flat (x, y) gives you the length of an underground tunnel between two cities, not the route you would walk.
Related ideas
Midpoint formula. The midpoint of two points (x1, y1) and (x2, y2) is the simple average of the coordinates: ((x1 + x2) / 2, (y1 + y2) / 2). It is the point exactly halfway along the segment.
Slope of a line. The slope between two points uses the same differences but as a ratio instead of a sum: slope = (y2 - y1) / (x2 - x1). Distance, midpoint, and slope are the three core coordinate-geometry results students meet first.
Manhattan distance. Some applications (notably grid-based pathfinding and certain machine-learning problems) use the Manhattan distance instead: |x2 - x1| + |y2 - y1|. It is the distance a taxi would travel along a city grid rather than the straight-line distance.
Great-circle distance. For points on a sphere (like Earth), distance is measured along the surface of the sphere, not through it. The standard formula for that is the haversine formula, which uses latitude and longitude rather than (x, y, z).
Applications in the real world
Surveying and construction rely on the distance formula every time a builder lays out a foundation or a surveyor verifies that a corner is square. The 3-4-5 trick used by carpenters to check a right angle is just the Pythagorean theorem, the same one that produces the distance formula.
Computer graphics use the formula constantly: every time a game engine asks 'is this enemy close enough to attack the player?' it is computing the distance between two points. In 2D platformers, the points are screen coordinates; in 3D games, they live in world space.
Machine learning algorithms such as k-nearest neighbors and k-means clustering measure similarity between data points using Euclidean distance in many dimensions. A user profile with 50 attributes is treated as a point in 50-dimensional space, and the distance formula tells you which other users are most alike.
Even maps and navigation use the formula indirectly: a road network is approximated by a graph of intersection points, and the system searches for the shortest path through that graph.
Quick reference
2D: d = sqrt((x2 - x1)^2 + (y2 - y1)^2). 3D: d = sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2). Distance is always non-negative; the order of the two points does not matter; round only at the very end to keep the answer accurate; and the whole thing is just the Pythagorean theorem written for coordinates.
If you would rather skip the arithmetic, you can plug the coordinates into our distance calculator and read the answer directly.
- 2D formula: d = sqrt((x2 - x1)^2 + (y2 - y1)^2)
- 3D formula: d = sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)
- Derivation: drop perpendiculars to form a right triangle; apply Pythagoras
- Midpoint: ((x1 + x2)/2, (y1 + y2)/2)
- Slope: (y2 - y1) / (x2 - x1)
- On Earth: use the haversine (great-circle) formula instead