Distance between two points:
formula and worked example
Pythagoras on a grid: subtract, square, add, take the root, and keep both axes in the same unit.
Calcylator Editorial Team
Updated · 5 min read
What the distance formula actually gives you
On a flat grid, the distance between two points is the length of the straight line that joins them. It is the shortest possible path, as a bird would fly it, and it is not the same as walking along the grid lines, which would be the horizontal difference plus the vertical one.
The formula is Pythagoras in disguise. The horizontal gap and the vertical gap are the two shorter sides of a right triangle, and the distance is the hypotenuse. That is why the same calculation turns up in map work, game and graphics programming, engineering drawings, and clustering methods that measure how far apart records are.
Because squares are involved, the distance is always zero or positive, and it comes out the same whichever point you label first.
The formula and the four inputs
Subtract the x values, subtract the y values, square both differences, add them and take the square root.
- x1:
- X1
- y1:
- Y1
- x2:
- X2
- y2:
- Y2
- X1 and Y1: the coordinates of the first point.
- X2 and Y2: the coordinates of the second point.
- Result: a length in the same unit as the coordinates, such as metres on a site plan or pixels on a screen.
Both axes must share a scale. If x is in kilometres and y is in metres, the answer is meaningless until one of them is converted. Screen coordinates add one more wrinkle: in most graphics systems y grows downward, which does not change the distance but does change how you read direction.
Worked example: from (2, 3) to (8, 11)
Find the straight-line distance between the points (2, 3) and (8, 11).
X1
2
Y1
3
X2
8
Y2
11
Distance between two points
10 units
The points (2, 3) and (8, 11) are 6 apart across and 8 apart up, which makes a 6-8-10 right triangle, a doubled 3-4-5.
- Horizontal gap: 8 − 2 = 6.
- Vertical gap: 11 − 3 = 8.
- Square and add: 6² + 8² = 36 + 64 = 100.
- Square root: √100 = 10 units.
A neat result like 10 is a hint that the triangle is a scaled 3-4-5, and spotting that pattern is a handy way to check a hand calculation. Other whole-number triples, such as 5-12-13 and 8-15-17, appear often in textbook problems for the same reason.
Notice also that the answer cannot be smaller than the larger of the two gaps. Here the longer gap is 8 and the distance is 10, so the result passes that quick bound.
Interpreting the figure on a real map or plan
A distance of 10 units only means something once the unit is attached. On a drawing at 1:100, 10 centimetres on paper is 10 metres on the ground, so the scale has to be applied before you quote the answer.
Remember also that this is a straight line. A road, a pipe run or a delivery route will almost always be longer, so treat the result as a lower bound on travel distance, useful for estimating ranges and for sanity checks.
On the surface of the Earth, latitude and longitude are angles, not flat coordinates. For short distances inside one city a flat approximation can be acceptable, but over long distances it drifts and a spherical formula is the better choice.
How moving one coordinate changes the distance
The distance responds to how the gap changes, not to how the coordinate itself changes. Raising X1 from 2 to 2.2 moves the first point closer to X2 at 8, which shrinks the horizontal gap from 6 to 5.8 and pulls the distance down slightly.
| Scenario | X1 | Calculated result |
|---|---|---|
| Original assumptions | 2 | 10 units |
| X1 increased by 10% | 2.2 | 9.881 units |
This is the opposite of what many people expect when they hear that an input has increased. If the same coordinate had moved away from the other point, the distance would have grown instead. In general, a distance is sensitive to a coordinate in proportion to how much of the total separation lies along that axis.
Where distance calculations usually go wrong
- Forgetting to square before adding, which gives 6 + 8 = 14 and treats the grid path as the straight path.
- Taking the square root of each difference separately instead of the root of the total.
- Mixing axis units or using a coordinate pair with a different scale on each axis.
- Swapping an x with a y between points, which silently pairs the wrong gaps.
A practical check on a site plan
Suppose you need the length of a cable run between two points read off a plan, assuming it can go in a straight line.
- Read both coordinate pairs in the same unit and note the plan scale.
- Compute the two gaps and the root of their squares.
- Apply the scale to turn plan units into real length.
- Add an allowance for routing, slack and connections, since the straight line is the minimum.
Common questions
What is the formula for the distance between two points?
Subtract the x values and the y values, square each difference, add them, and take the square root. This is the Pythagorean theorem applied to the horizontal and vertical gaps between the points on a flat coordinate grid.
Can the distance between two points be negative?
No. The differences are squared before the root is taken, so the result is zero when the points coincide and positive otherwise. A negative figure means a sign or arithmetic error somewhere in the working.
Does the order of the points matter?
It does not. Swapping the first and second points flips the sign of each difference, but squaring removes the sign, so the distance is identical either way round.
How do I extend the formula to three dimensions?
Add a third squared difference for the z coordinate under the root. The distance is the square root of the sum of the squared x, y and z gaps. The same idea continues to any number of dimensions.
Is this the same as the distance on a map of the Earth?
Only roughly, and only over short ranges. Latitude and longitude are angles on a curved surface, so long distances need a spherical or ellipsoidal formula, while small areas can be projected onto a flat grid first.
Was this guide helpful?
Continue reading
View all blogsMidpoint of x coordinates: formula and worked example
Find the midpoint of two x coordinates by averaging them. See a worked example with a negative value, a change table and the sign slips to avoid.
5 min read
Sum of arithmetic series: formula and worked example
Add up an arithmetic series without listing every term: average of first and last times the term count, with a 4 to 40 example and a sensitivity table.
5 min read
Geometric series sum: formula and worked example
Add the terms of a geometric series with first term, ratio and term count, shown with a 3-6-12 example, a sensitivity table and the ratio-of-one trap.
5 min read




