CCalclabhub

Coordinate Geometry

How To Calculate The Distance Between Two Points

The distance formula is Pythagoras in disguise, and seeing that connection is what stops it being a formula you have to memorise.

Quick Answer

distance = sqrt((x2 - x1)^2 + (y2 - y1)^2)

d
Straight line distance between the points
x1 y1
Coordinates of the first point
x2 y2
Coordinates of the second point

Subtract the x values and square the result, subtract the y values and square that, add the two and take the square root. From the point three, four to the point seven, one the distance is exactly five.

What Is The Distance Between Two Points?

The distance between two points on a plane is the length of the straight line joining them. It is found by subtracting the coordinates, squaring each difference, adding them and taking the square root. From three, four to seven, one the answer is exactly five.

The formula is Pythagoras applied to a right triangle. The horizontal difference and the vertical difference are the two legs, and the straight line between the points is the hypotenuse. Once you see the triangle, there is nothing left to memorise.

Order does not matter. Squaring removes the sign, so subtracting the points in the opposite order gives identical squares and therefore the same distance. That is a useful check when you are unsure which point to call the first.

The midpoint is a separate but related calculation: average the x values and average the y values. It lies exactly halfway along the line, and it needs no square root at all, which is why it is often asked for alongside the distance.

The distance formula works in three dimensions too, with a third squared difference added under the root. The same idea extends to any number of dimensions, which is why it is the foundation of the whole idea of distance in mathematics.

The formula gives the straight line distance, which is not the same as the distance you would actually travel. On a street grid you cannot walk diagonally through buildings, so the real distance is the sum of the horizontal and vertical legs. That is the Manhattan distance.

Because the differences are squared, the formula is symmetric and always produces a non negative answer. A negative distance is a sign that something has been subtracted where it should have been added, or that the square root was skipped.

Distances in coordinate geometry are measured in the same units as the coordinates. If the axes are in metres, the distance is in metres. If they are in degrees of latitude and longitude, the answer is not a real distance at all, which is a genuine trap in mapping work.

The formula underlies a great deal of practical work. Clustering algorithms group points by distance, navigation systems find the nearest of several candidates, and collision detection in games checks whether two objects are closer than the sum of their radii.

A common special case is the distance from a point to the origin. With one point at zero, zero the formula collapses to the square root of x squared plus y squared, which is the length of the position vector.

Finally, be careful with the order of operations. Square each difference before adding, not after. Adding first and then squaring gives a number that is too large, and it is a natural slip when the formula is written on one line.

Formula

d = sqrt((x2 - x1)^2 + (y2 - y1)^2)

Pythagoras with the coordinate differences as the two legs. Order of the points does not matter.

SymbolMeaning
dDistance
x1First x
y1First y
x2Second x
y2Second y

midpoint = ((x1 + x2) / 2, (y1 + y2) / 2)

Average each coordinate separately. No square root is involved.

SymbolMeaning
MxMidpoint x
MyMidpoint y

distance = |x2 - x1| + |y2 - y1|

The distance travelled along a grid rather than in a straight line, which is what a taxi actually drives.

SymbolMeaning
d1Manhattan distance
x1First x
y1First y
x2Second x
y2Second y

How To Calculate The Distance Between Two Points

  1. 1

    Label the coordinates of each point

    Call one point x1 y1 and the other x2 y2. It makes no difference which is which, because the differences get squared and the signs disappear.

  2. 2

    Subtract the x values and the y values

    From three, four to seven, one the x difference is four and the y difference is minus three. Keep the sign for now, it will not survive the squaring.

  3. 3

    Square each difference

    Four squared is sixteen and minus three squared is nine. Squaring is what turns a signed displacement into a length.

  4. 4

    Add and take the square root

    Sixteen plus nine is twenty five, and the square root of twenty five is five. The square root is the step that returns you from squared units to plain ones.

  5. 5

    Attach the units of the axes

    The distance carries the same unit as the coordinates. Mixing units between axes, such as metres on one and feet on the other, makes the answer meaningless.

Examples

Example 1: From three, four to seven, one

Point 1
(3, 4)
Point 2
(7, 1)
StepCalculationResult
Midpoint x(3 + 7) / 25
Midpoint y(4 + 1) / 22.5
Distancesqrt(4^2 + (-3)^2)5

Result: The points three four and seven one are exactly 5 units apart, and their midpoint is five, 2.5.

Example 2: From the origin to six, eight

Point 1
(0, 0)
Point 2
(6, 8)
StepCalculationResult
Midpoint x(0 + 6) / 23
Midpoint y(0 + 8) / 24
Distancesqrt(6^2 + 8^2)10

Result: The point six eight lies 10 units from the origin, the classic three four five triangle scaled up by two.

Example 3: From one, two to ten, six

Point 1
(1, 2)
Point 2
(10, 6)
StepCalculationResult
Midpoint x(1 + 10) / 25.5
Manhattan distance9 + 413
Distancesqrt(9^2 + 4^2)9.8489

Result: The straight line distance is 9.8489 units, while travelling along the grid would take 13 units, which is why taxis charge by the grid.

Calculator

Distance

5

Midpoint x
5
Midpoint y
2.5
Manhattan distance (grid route)
7

Values update as you type. This calculator covers the single scenario its formula assumes — see Common Mistakes for what it leaves out.

Prefer a full-width tool? Open the The Distance Between Two Points calculator page.

Common Mistakes

  • Adding the differences before squaring

    Square each difference first. Adding and then squaring gives a much larger number and is the most common slip with a one line formula.

  • Subtracting the squared values instead of adding

    The two squared differences are added, because they are the legs of a right triangle. Subtracting them is a different formula entirely.

  • Forgetting the square root

    Without the final root you have the squared distance, which is a different quantity and is out by the square of the answer.

  • Mixing the x and y of different points

    Each difference must pair an x with an x and a y with a y. Crossing them over gives the distance between two other points entirely.

  • Using the straight line distance for grid travel

    The straight line is shorter than any real route. On a street grid the Manhattan distance is what a vehicle actually covers.

  • Treating degrees of longitude as a distance

    A degree of longitude shrinks towards the poles, so coordinates in degrees cannot be fed into this formula to get kilometres. A projection or a great circle formula is needed.

  • Assuming the midpoint is the average of the distance

    The midpoint averages the coordinates, not the lengths. It lies on the segment halfway along, and it is found without any square root.

FAQ

What is the distance formula?

The square root of the sum of the squared differences in x and y. It is Pythagoras applied to the horizontal and vertical gaps between the points.

Does the order of the points matter?

No. Both differences get squared, so swapping the points changes the signs but not the squares, and the distance comes out the same.

How do you find the midpoint of two points?

Average the x coordinates and average the y coordinates separately. The midpoint of three, four and seven, one is five, 2.5.

Can the formula be used in three dimensions?

Yes. Add the squared difference in z under the same square root. The idea extends to any number of dimensions.

Why is the distance shorter than the route I would drive?

Because the formula measures a straight line, and roads follow a grid. The grid distance is the sum of the horizontal and vertical differences, called the Manhattan distance.

What units is the answer in?

Whatever units the coordinates are in. If the axes are metres, the distance is metres, provided both axes use the same unit.

References

  1. [1]Wikipedia, Euclidean distance — https://en.wikipedia.org/wiki/Euclidean_distance
  2. [2]Wikipedia, Distance — https://en.wikipedia.org/wiki/Distance
  3. [3]Wikipedia, Taxicab geometry — https://en.wikipedia.org/wiki/Taxicab_geometry
  4. [4]Wikipedia, Midpoint — https://en.wikipedia.org/wiki/Midpoint