The Distance Formula

The distance between two points is the hypotenuse of a right triangle hiding on the grid. See where d = √(x2 − x1)2 + (y2 − y1)2 comes from, then use it confidently with negatives, decimals and three dimensions.

By the PythagoreanTheorem.net teamUpdated 10 min read

The distance formula gives the straight-line distance between two points (x1, y1) and (x2, y2) on a coordinate plane. It is the Pythagorean theorem in disguise: the horizontal and vertical gaps between the points are the legs of a right triangle, and the distance between the points is its hypotenuse.

d = √(x2 − x1)2 + (y2 − y1)2

Here x2 − x1 is the change in x (often written Δx, "delta x") and y2 − y1 is the change in y (Δy). To check an answer or skip the arithmetic, use the distance formula calculator, which shows every step.

Where the distance formula comes from

Take the points P(1, 1) and Q(7, 9). Nobody can measure the slanted segment PQ by counting grid squares, but you can count the horizontal and vertical gaps. Draw a horizontal line through P and a vertical line through Q. They meet at R(7, 1), and because horizontal and vertical lines are perpendicular, the angle at R is a right angle. The segment PQ has become the hypotenuse of a right triangle.

123456789123456789100 x₂ − x₁ = 6 y₂ − y₁ = 8 d = 10 P(1, 1) Q(7, 9) Pythagorean theorem: d² = 6² + 8² d² = 36 + 64 = 100 d = √100 = 10
The two points are opposite corners of a right triangle. The legs are the change in x (6) and the change in y (8); the distance is the hypotenuse.

The horizontal leg runs from x = 1 to x = 7, so it is 7 − 1 = 6 units long. The vertical leg runs from y = 1 to y = 9, so it is 9 − 1 = 8 units long. Now apply the Pythagorean theorem formula, a2 + b2 = c2:

d2= 62 + 82 = 36 + 64 = 100d= √100 = 10

The triangle is a 3-4-5 triangle scaled by 2, so a distance of exactly 10 makes sense.

The general case

Now repeat the argument with letters. For any two points P(x1, y1) and Q(x2, y2), the corner of the right triangle is R(x2, y1). The horizontal leg has length |x2 − x1| and the vertical leg has length |y2 − y1|. The absolute-value bars are there because a length cannot be negative, and the difference might be. By the Pythagorean theorem:

d2= |x2 − x1|2 + |y2 − y1|2= (x2 − x1)2 + (y2 − y1)2

The bars can be dropped in the second line because squaring removes the sign anyway: (−6)2 = 36 = 62. Taking the positive square root of both sides gives the distance formula.

Key ideaThe distance formula is c = √a2 + b2 with a = Δx and b = Δy. If you forget the formula, sketch the points, draw the right triangle and use the theorem directly. You will get the same answer.

If the two points share an x-coordinate or a y-coordinate, the triangle flattens into a single segment. The formula still works, because one of the two squares is zero.

How to use the distance formula

  1. Label the points. Call either point (x1, y1) and the other (x2, y2). It does not matter which is which.
  2. Subtract the coordinates. Find x2 − x1 and y2 − y1. Put every negative number in parentheses so that subtracting a negative turns into adding.
  3. Square both differences. Both results are positive (or zero), even if a difference was negative.
  4. Add the squares. The sum is d2, the square of the distance.
  5. Take the square root. Give the exact answer in simplest radical form if the sum is not a perfect square, then round if the question asks for a decimal.

TipBefore you calculate, estimate. The distance is always at least as long as the bigger of the two gaps and never longer than the two gaps added together. For P(1, 1) and Q(7, 9), it must lie between 8 and 14, and 10 does.

Worked examples with negative coordinates

Negative coordinates are where most mistakes happen, so every example below includes at least one. The first one is drawn on a four-quadrant grid.

xy −4−2246−4−22 8 6 d = 10 A(−3, 2) B(5, −4) Δx = 5 − (−3) = 8 Δy = −4 − 2 = −6 d² = 8² + (−6)² d² = 64 + 36 = 100 d = 10
The legs are found by subtraction, not by counting from the origin: from x = −3 to x = 5 is 8 units, even though the segment crosses the y-axis.

Example 1: points in different quadrants

Find the distance between A(−3, 2) and B(5, −4).

d= √(5 − (−3))2 + (−4 − 2)2= √82 + (−6)2= √64 + 36 = √100 = 10

The distance is 10 units. The difference −4 − 2 = −6 is negative, but its square, 36, is not.

Example 2: two negative coordinates

Find the distance between (−4, −1) and (1, 11).

d= √(1 − (−4))2 + (11 − (−1))2= √52 + 122 = √25 + 144 = √169 = 13

The distance is 13 units. The legs and the distance form the 5-12-13 triangle.

Example 3: an answer that is a radical

Find the distance between (2, −1) and (−3, 4).

d= √(−3 − 2)2 + (4 − (−1))2= √(−5)2 + 52 = √25 + 25 = √50= √25 · 2 = 5√2 ≈ 7.07

The exact distance is 5√2, about 7.07 units rounded to the nearest hundredth.

Example 4: decimal coordinates

Find the distance between (0.5, 1.2) and (2.9, −0.6).

d= √(2.9 − 0.5)2 + (−0.6 − 1.2)2= √2.42 + (−1.8)2 = √5.76 + 3.24 = √9 = 3

The distance is exactly 3 units. Keep full decimals until the last step; rounding 5.76 to 5.8 early would spoil the exact answer.

Example 5: a horizontal segment

Find the distance between (−6, 3) and (4, 3).

d = √(4 − (−6))2 + (3 − 3)2 = √102 + 02 = 10

The distance is 10 units. When two points share a y-coordinate (or an x-coordinate), the distance is simply the absolute difference of the other coordinates: |4 − (−6)| = 10.

Example 6: find a missing coordinate

The point (x, 5) is 13 units from (2, −7). Find x.

Put the known distance into the formula and square both sides to remove the root:

(x − 2)2 + (5 − (−7))2= 132(x − 2)2 + 144= 169(x − 2)2= 25x − 2= ±5

x = 7 or x = −3. There are two answers because two points on the horizontal line y = 5 are 13 units from (2, −7), one on each side. Check: from (2, −7) to (7, 5) the gaps are 5 and 12, and √25 + 144 = 13.

For printable practice with an answer key, try the distance formula worksheet.

Order of the points and common mistakes

It never matters which point you call the first one. Swapping the points changes the sign of each difference, x1 − x2 = −(x2 − x1), and squaring erases the sign. Example 1 with the points reversed gives the same result:

√(−3 − 5)2 + (2 − (−4))2 = √(−8)2 + 62 = √64 + 36 = 10

Because each difference is squared separately, you can even subtract the x-values in one order and the y-values in the other. What you must never do is mix an x-coordinate with a y-coordinate. These are the slips that cost marks:

  • Subtracting a negative. 5 − (−3) = 8, not 2. Writing the parentheses makes the double negative visible.
  • The calculator sign trap. Typing -6^2 on most calculators gives −36, because the calculator squares first and applies the minus sign afterward. Type (-6)^2, or square the positive number, since the square is the same.
  • Mixing up coordinates. Subtract x from x and y from y. Writing the points one above the other before you start helps.
  • Splitting the square root. √64 + 36 = 10, but √64 + √36 = 14. The root applies to the whole sum.
  • Stopping at d2. If your answer is 100 for two points that are clearly about 10 apart, you forgot the final square root.

The common mistakes page covers these and other errors with the theorem itself.

Distance formula vs midpoint formula

The two formulas use the same inputs, so students often confuse them. The distance formula subtracts coordinates and gives a length. The midpoint formula averages coordinates and gives a point:

M = (x1 + x2 over 2, y1 + y2 over 2)
Distance formulaMidpoint formula
AnswerA number (a length)A point (x, y)
OperationSubtract, square, add, square rootAdd and divide by 2
For (−3, 2) and (5, −4)d = 10M = (1, −1)

The two results agree with each other: the distance from (−3, 2) to the midpoint (1, −1) is √42 + (−3)2 = 5, exactly half of 10.

The distance formula in three dimensions

In space a point has three coordinates, (x, y, z), and the formula gains a third term:

d = √(x2 − x1)2 + (y2 − y1)2 + (z2 − z1)2

It comes from using the Pythagorean theorem twice: once across the floor of a box to get a diagonal, then again up to the opposite corner. From (1, 2, 3) to (4, 6, 15) the changes are 3, 4 and 12, so d = √9 + 16 + 144 = √169 = 13. The page on the Pythagorean theorem in 3D draws both steps, and the same pattern continues into any number of dimensions.

Where the distance formula is used

Any time positions are stored as coordinates, the distance formula turns them into straight-line distances:

  • Maps and grids. On a flat map with a square grid, the formula gives the "as the crow flies" distance between two grid references. Over a small area a flat grid is a good model; across whole countries or oceans the curve of the Earth matters and navigators use spherical methods instead (see the theorem in navigation).
  • Games and graphics. To test whether two round objects touch, a program compares the distance between their centers with the sum of their radii. Square roots cost computing time, so programmers often compare d2 with (r1 + r2)2 instead. Both sides are positive, so comparing the squares gives the same answer. More in the Pythagorean theorem in computer science.
  • Data. If each item in a data set is described by a list of numbers, the same formula with more terms, called the Euclidean distance, measures how different two items are. Nearest-neighbor methods, which classify an item by finding the most similar known items, often use it.
  • Circles. A circle is every point at one fixed distance from a center. Write that condition with the distance formula and you get the equation of a circle.

Straight-line distance vs taxicab distance

In a city laid out in square blocks you cannot walk through buildings, so the distance you actually travel is the taxicab distance, |Δx| + |Δy|. The distance formula measures the straight line instead.

A(0, 0) B(4, 3) up 3, right 4: 7 staircase: 7 straight: 5 Taxicab: |Δx| + |Δy| = 7 Euclidean: √(Δx² + Δy²) = 5
Every shortest route along the grid from A to B is 7 blocks long, however it zigzags. The straight line, the hypotenuse of a 3-4-5 triangle, is only 5.

The straight-line distance is never longer than the taxicab distance, and the two are equal only when the points lie on the same horizontal or vertical line. That is the familiar fact that the hypotenuse is shorter than the two legs added together.

Frequently asked questions

What is the distance formula?

It is d = √(x2 − x1)2 + (y2 − y1)2, which gives the length of the segment joining the points (x1, y1) and (x2, y2). Subtract the coordinates, square the two differences, add them and take the square root.

How is the distance formula related to the Pythagorean theorem?

It is the Pythagorean theorem applied to a right triangle drawn on the grid. The horizontal gap x2 − x1 and the vertical gap y2 − y1 are the legs, and the distance is the hypotenuse, so d = √a2 + b2 with those two gaps in place of a and b.

Does it matter which point is (x1, y1)?

No. Reversing the points makes each difference change sign, and squaring removes the sign, so the result is identical. Just make sure you subtract an x-coordinate from an x-coordinate and a y-coordinate from a y-coordinate.

Can a distance be negative?

No. The sum of two squares is never negative, and the square root symbol always means the non-negative root. The distance is 0 only when the two points are the same point. If you get a negative answer, a sign slipped in after the square root.

How do I find the distance between two points without the formula?

Plot the points on graph paper, count the horizontal and vertical squares between them, and use a2 + b2 = c2 with those counts as the legs. This is exactly what the formula does. If the points are on the same horizontal or vertical line, just subtract the coordinates that differ.

What is the distance formula in 3D?

For points (x1, y1, z1) and (x2, y2, z2), add a third squared difference: d = √(x2 − x1)2 + (y2 − y1)2 + (z2 − z1)2. It comes from applying the Pythagorean theorem twice. The 3D diagonal calculator handles boxes and 3D points.

Keep exploring