The distance formula finds how far apart two points are on a flat surface
The distance formula calculates the straight-line distance between two points when you know their coordinates. If you have two points — call them Point A and Point B — and you know where each one sits on a grid (its x and y coordinates), you can plug those numbers into a formula and get the distance between them.
The formula is: distance = √[(x₂ − x₁)² + (y₂ − y₁)²]
This comes from the Pythagorean theorem. When you draw a line between two points, you can form a right triangle where that line is the hypotenuse. The two legs of the triangle are the horizontal distance (the difference in x values) and the vertical distance (the difference in y values). The formula adds the squares of those two legs, takes the square root, and gives you the hypotenuse — which is the straight-line distance you want.
Key Takeaways
- The distance formula uses the coordinates of two points: subtract the x values, subtract the y values, square both results, add them, and take the square root.
- The order in which you subtract does not matter — (x₂ − x₁) and (x₁ − x₂) give the same result because you square the answer either way.
- This formula works on any flat, two-dimensional grid, whether you are measuring pixels on a screen, points on a map, or positions in a spreadsheet.
- For three-dimensional space, add a third term: √[(x₂ − x₁)² + (y₂ − y₁)² + (z₂ − z₁)²].
Step-by-step walkthrough with an example
Say you have Point A at coordinates (3, 4) and Point B at coordinates (9, 12). You want to find the distance between them.
Step 1: Subtract the x coordinates. 9 − 3 = 6.
Step 2: Subtract the y coordinates. 12 − 4 = 8.
Step 3: Square the first result. 6² = 36.
Step 4: Square the second result. 8² = 64.
Step 5: Add the two squared results. 36 + 64 = 100.
Step 6: Take the square root of the sum. √100 = 10.
The distance between Point A and Point B is 10 units. This works whether your units are inches, meters, pixels, or anything else — the formula gives you the distance in whatever units your coordinates use.
Why the order of subtraction does not matter
You might wonder: what if I subtract in the opposite order? What if I do (x₁ − x₂) instead of (x₂ − x₁)? The answer is that it does not change the result, because you square the answer right after.
If you subtract 3 from 9, you get 6. If you subtract 9 from 3, you get −6. But 6² = 36 and (−6)² = 36. The negative sign disappears when you square. So you will always get the same distance no matter which point you call "Point 1" and which you call "Point 2".
Using the formula in spreadsheets and programming
Most spreadsheet programs and coding languages have a built-in square root function, so you can write the formula directly. In Excel or Google Sheets, if Point A is in cells (A1, B1) and Point B is in cells (A2, B2), you would write: =SQRT((A2-A1)^2+(B2-B1)^2)
In Python, you would import the math library and write something like: distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
Many programming languages also have a built-in distance function in their math or geometry libraries, so you may not need to type the formula yourself. Check your language's documentation to see what is available.
Distance in three dimensions
If your points exist in three-dimensional space — meaning each point has an x, y, and z coordinate — the formula extends naturally. You add a third squared term for the z coordinates: distance = √[(x₂ − x₁)² + (y₂ − y₁)² + (z₂ − z₁)²]
The logic is the same: you are measuring how far apart the points are along each axis, squaring those distances, adding them up, and taking the square root. This works for any number of dimensions, though three is the most common in practical use.
Common mistakes to avoid
The most frequent error is forgetting to square the differences before adding them. If you just add (x₂ − x₁) + (y₂ − y₁) without squaring, you will get the wrong answer. The squaring step is essential because it accounts for the geometry of the right triangle.
Another mistake is forgetting to take the square root at the end. If you stop after adding the squared differences, you have the square of the distance, not the distance itself. Always take the square root as the final step.
A third pitfall is mixing up which coordinate is x and which is y. On a standard grid, x is horizontal (left-right) and y is vertical (up-down). If your data labels them differently, make sure you know which is which before you plug numbers in.
When you might use this in real situations
Game developers use the distance formula constantly to check whether two objects are close enough to collide, or to calculate how far a character has moved. Mapping software uses it to find the straight-line distance between two locations (though real-world routing also accounts for roads and obstacles).
Data scientists use it to measure how similar two data points are in machine learning. Anyone working with coordinates — whether on a screen, a map, or in a dataset — may need to calculate distance at some point. The formula is one of the most practical tools in geometry and computer science.
Frequently Asked Questions
What if one or both coordinates are negative?
Negative coordinates work exactly the same way. If Point A is at (−3, 4) and Point B is at (5, −2), you subtract: 5 − (−3) = 8 and −2 − 4 = −6. Then square both: 8² = 64 and (−6)² = 36. Add and take the square root: √(64 + 36) = √100 = 10. The negatives disappear during the squaring step.
Can the distance ever be zero?
Yes. If both points have the same coordinates, the distance is zero — they are the same point. In that case, both differences are zero, so (0)² + (0)² = 0, and √0 = 0.
Does this formula work on a curved surface like Earth?
No. This formula assumes a flat plane. On a sphere like Earth, the shortest distance between two points is along a curve called a great circle, not a straight line. Calculating that distance requires a different formula (the haversine formula) that accounts for the planet's radius and curvature.
What if I only have the distance and one point, and I need to find the other point?
You cannot find a unique second point — infinitely many points sit at a given distance from any starting point. They form a circle (in 2D) or a sphere (in 3D) around the first point. You would need additional information, such as the angle or direction, to pinpoint a specific second location.