Calcylator
Coordinate Geometry

Distance formula:
straight-line distance from coordinates

The distance formula is the Pythagorean theorem written with coordinates. Subtract, square, add, take the root.

Calcylator Editorial Team

Updated · 5 min read

The distance formula is a right triangle in disguise

Mark two points on graph paper. Draw a horizontal line from the first and a vertical line from the second until they meet. You have a right triangle whose legs are the horizontal gap and the vertical gap, and whose hypotenuse is the straight line joining the points.

That is the full idea. The horizontal gap is x₂ − x₁, the vertical gap is y₂ − y₁, and the Pythagorean theorem gives the hypotenuse as the square root of the sum of their squares. Nothing new has to be memorised once you see it.

Distance between two points =d = √[(x₂ − x₁)² + (y₂ − y₁)²]
(x₁, y₁), (x₂, y₂):
the two points
x₂ − x₁:
horizontal gap
y₂ − y₁:
vertical gap
Because the gaps are squared, the order of the points and the signs of the gaps do not matter.

Step by step: (1, 2) to (7, 10)

  1. Subtract the x-values: 7 − 1 = 6.
  2. Subtract the y-values: 10 − 2 = 8.
  3. Square each gap: 6² = 36 and 8² = 64.
  4. Add: 36 + 64 = 100.
  5. Take the square root: √100 = 10.
  • Point A

    (1, 2)

  • Point B

    (7, 10)

Distance

10 units

The 6-8-10 triangle is a scaled 3-4-5, so the answer is a whole number.

Most pairs of points are not so tidy. From (0, 0) to (3, 5): 9 + 25 = 34, and √34 = 5.831. Quote it as √34 if an exact value is needed, or round to the precision of your data.

Negative coordinates and the sign trap

Negative coordinates cause more mistakes than the formula. The error is usually in the subtraction: x₂ − x₁ with x₁ = −3 and x₂ = 2 is 2 − (−3) = 5, not −1. Write the subtraction with brackets so that the double negative is visible.

  • Point A

    (−3, 4)

  • Point B

    (2, −8)

Distance

13 units

Horizontal gap: 2 − (−3) = 5. Vertical gap: −8 − 4 = −12. Squares: 25 and 144, total 169. √169 = 13.

The vertical gap came out negative, but squaring removes the sign. That is why you may subtract in either order, provided each coordinate pair is treated consistently.

Decimals behave the same way. For (1.5, 2.5) and (4.5, 6.5) the gaps are 3 and 4, so the distance is 5. When the points come from a spreadsheet or GPS log, keep full precision through the subtraction and round only at the end.

Units, scale and when the formula does not apply

The formula gives the distance in whatever unit the axes use. If a floor plan has grid squares of 0.5 m, a distance of 10 grid units is 5 m. Check the scale before you quote a real length.

It also assumes a flat plane. On a globe, two points given in latitude and longitude lie on a curved surface, and treating degrees as flat coordinates gives wrong results, increasingly so over long distances and away from the equator, where a degree of longitude covers less ground. For travel distances use a great-circle method instead.

SituationDistance formula suitable?
Room or site plan in metresYes
Pixels on a screenYes, in pixels
Short distance in a city using projected map coordinatesYes, in the map's units
Latitude and longitude over a long distanceNo, use great-circle
Road travel distanceNo, it is not straight-line

Adding a third coordinate

In three dimensions, add the depth gap: d = √[(x₂ − x₁)² + (y₂ − y₁)² + (z₂ − z₁)²]. The distance from the origin to (3, 4, 12) is √(9 + 16 + 144) = √169 = 13.

This turns up in 3D graphics, drone positions and engineering drawings. The reasoning is the same: the 2D hypotenuse becomes a leg of a second right triangle.

A distance calculator is a convenient way to double-check hand work when decimals are involved, or when you need a batch of distances for the same start point.

Comparing several distances at once

A courier stands at (2, 1) on a map where each unit is 1 km. Three drop points sit at (5, 5), (6, 4) and (9, 2). Which is the nearest, and does the order matter?

Drop pointGaps (x, y)Sum of squaresDistance
(5, 5)3, 4255.00 km
(6, 4)4, 3255.00 km
(9, 2)7, 1507.07 km

The first two are tied at 5 km, so a tie-break such as traffic or time window is needed. The shortcut is useful: you can compare sums of squares without taking square roots, because a larger sum always means a larger distance. Take the root only for the answer you quote.

Straight-line distance is a lower bound for real travel. On a street grid you may have to travel the horizontal gap plus the vertical gap, which is 7 km for the first drop, not 5 km. That figure is called the Manhattan distance, and it is the right one when movement is restricted to axes.

A circle is a set of points at one distance

A circle with centre (h, k) and radius r is every point whose distance from the centre equals r. Squaring the distance formula gives the circle's equation: (x − h)² + (y − k)² = r². A circle centred on (2, 1) with radius 5 contains (5, 5) on its edge, since 3² + 4² = 25.

Points with a smaller sum of squares are inside the circle and those with a larger sum are outside. This turns a geometric question, is the point within range, into a single comparison. Wi-fi coverage, delivery zones and sensor ranges are checked in just this way.

Uses and quick checks

  • Is a point inside a circle? Compare its distance from the centre with the radius.
  • Is a triangle isosceles or right? Find all three side lengths and compare them.
  • How far is the nearest shop, given grid positions? Compute each distance and take the smallest.
  • Sanity check: the result can never be smaller than either gap alone.

Common questions

What is the distance formula?

The distance between (x₁, y₁) and (x₂, y₂) is √[(x₂ − x₁)² + (y₂ − y₁)²]. For (1, 2) and (7, 10), the gaps are 6 and 8, so the distance is √100 = 10.

Does the order of the points matter?

No. Swapping the points changes the sign of each gap, but squaring removes the sign. The distance from A to B equals the distance from B to A. What matters is subtracting the x-values and y-values consistently.

How is the distance formula related to the Pythagorean theorem?

The horizontal and vertical gaps between the points are the legs of a right triangle, and the straight line joining the points is its hypotenuse. The formula is the theorem solved for the hypotenuse.

Can the distance be negative?

No. It is a square root of a sum of squares, which is zero or positive. A distance of zero means the points are the same. A negative gap in the subtraction is normal, but the final answer cannot be negative.

Can I use this formula for latitude and longitude?

Not accurately over long distances, because the Earth's surface is curved. Over a small area, projected coordinates in metres work fine. For distances between cities, use a great-circle (haversine) calculation instead.

Was this guide helpful?

Continue reading

View all blogs