,

The distance between two points is the length of the straight segment joining them. Coordinates let us calculate that length without measuring a drawing. The key is to use the horizontal and vertical changes as the legs of a right triangle.

This page uses ordinary Euclidean distance in a rectangular coordinate system with perpendicular axes and compatible length units. A road route, a grid-constrained route and a straight-line separation need not have the same length.

On this page

Deriving the distance formula

Let A=(x1,y1)A=(x_1,y_1) and B=(x2,y2)B=(x_2,y_2). Their horizontal separation has length |x2−x1||x_2-x_1| and their vertical separation has length |y2−y1||y_2-y_1|. By the Pythagorean theorem,

d2=(x2−x1)2+(y2−y1)2.d^2=(x_2-x_1)^2+(y_2-y_1)^2.

Distance is non-negative, so take the principal square root:

d(A,B)=(x2−x1)2+(y2−y1)2.d(A,B)=\sqrt{(x_2-x_1)^2+(y_2-y_1)^2}.

The absolute-value signs are unnecessary inside the squares because a number and its negative have the same square. The derivation, rather than the appearance of the diagram, is what justifies the formula. See the OpenStax distance-formula treatment.

A diagonal and its perpendicular components

A diagonal and its perpendicular componentsA right triangle joins A at negative two, one to B at four, nine using horizontal length six and vertical length eight.−4−224682468100xyA (−2, 1)B (4, 9)6 units8 unitsd = 10
The 10-unit diagonal is shorter than the 14-unit route along both legs.
Diagram explanation and exact data

The coordinate differences are 6 and 8. The Pythagorean calculation is d² = 6² + 8² = 100. Equal unit scales keep the triangle proportional.

A diagonal and its perpendicular components: exact reference data
Object Exact coordinates or length
A (−2,1)(-2,1)
B (4,9)(4,9)
Right-angle corner (4,1)(4,1)
Horizontal leg 66
Vertical leg 88
Diagonal 1010

Worked example: a 6–8–10 triangle

Find the distance from A=(−2,1)A=(-2,1) to B=(4,9)B=(4,9).

Calculate both differences

Δx=4−(−2)=6,Δy=9−1=8.\Delta x=4-(-2)=6,\qquad\Delta y=9-1=8.

Apply the formula

d=62+82=36+64=100=10.\begin{aligned}d&=\sqrt{6^2+8^2}\\&=\sqrt{36+64}\\&=\sqrt{100}=10.\end{aligned}

The distance is 10 coordinate units. If each unit represents a metre, it is 10 m. It is not 6+8=146+8=14 units: that is the length of a route following the horizontal and vertical legs.

The answer is reasonable because the diagonal must be longer than either leg, but shorter than travelling both legs consecutively when both legs are non-zero.

An exact answer need not be an integer

For P=(3,2)P=(3,2) and Q=(5,−1)Q=(5,-1),

d=(5−3)2+(−1−2)2=22+(−3)2=13≈3.606.\begin{aligned}d&=\sqrt{(5-3)^2+(-1-2)^2}\\&=\sqrt{2^2+(-3)^2}\\&=\sqrt{13}\approx3.606.\end{aligned}

The exact distance is 13\sqrt{13} units. The decimal is an approximation to three decimal places. Keep the radical during later algebra if an exact answer is needed; early rounding can change subsequent results.

The negative vertical change does not make the distance negative. It records direction before squaring, whereas distance records length.

Horizontal, vertical and identical points

If the points have the same yy-coordinate, the vertical difference is zero, so the formula reduces to d=|x2−x1|d=|x_2-x_1|. The distance from (−3,4)(-3,4) to (5,4)(5,4) is therefore 8 units.

If the points have the same xx-coordinate, use d=|y2−y1|d=|y_2-y_1|. For identical points, both differences are zero and the distance is zero. No special exception to the formula is needed.

Changing the endpoint order leaves the result unchanged:

d(A,B)=d(B,A).d(A,B)=d(B,A).

Both coordinate differences reverse sign together, but their squares do not change. This also provides a useful check on a calculation involving negative coordinates.

Units and scale

Suppose a rectangular site plan uses 2 m per horizontal coordinate unit and 3 m per vertical coordinate unit. A change of (3,4)(3,4) in the drawing represents physical changes of 6 m and 12 m. The physical separation is

d=62+122=180=65 m.d=\sqrt{6^2+12^2}=\sqrt{180}=6\sqrt5\text{ m}.

Calculating 32+42=5\sqrt{3^2+4^2}=5 first and multiplying by a single scale factor would be wrong because no single scale factor applies to both directions.

Likewise, coordinates such as “seconds” and “degrees Celsius” should not be combined under this formula to claim a physical length. The coordinate axes must describe compatible spatial quantities.

Use squared distance when only a comparison is needed

Square roots preserve order for non-negative inputs. To decide which of two points is closer to the origin, compare their squared distances.

For P=(2,3)P=(2,3) and Q=(4,0)Q=(4,0),

OP2=22+32=13,OQ2=42=16.OP^2=2^2+3^2=13,\qquad OQ^2=4^2=16.

Since 13<1613<16, PP is closer. There is no need to approximate either square root. This same idea makes circle membership tests efficient: compare the squared distance from the centre with r2r^2.

Avoid mixing directions and lengths

Subtract corresponding coordinates: an xx-coordinate from an xx-coordinate, and a yy-coordinate from a yy-coordinate. Write parentheses around negative values. In particular, 4−(−2)=64-(-2)=6, not 2.

Square each complete difference before adding. The expression (x2−x1)2(x_2-x_1)^2 is not generally x22−x12x_2^2-x_1^2. Finally, take one square root of the sum; adding the two separate square roots computes a different length.

Practice questions

  1. Find the exact distance between (3,2)(3,2) and (5,−1)(5,-1).
  2. Find the distance between (−3,4)(-3,4) and (5,4)(5,4).
  3. A plan uses 0.50.5 m per unit on both axes. How far apart physically are (−2,1)(-2,1) and (4,9)(4,9)?
Check your answers
  1. (5−3)2+(−1−2)2=4+9=13\sqrt{(5-3)^2+(-1-2)^2}=\sqrt{4+9}=\sqrt{13} units.
  2. The points are on one horizontal line. Their separation is |5−(−3)|=8|5-(-3)|=8 units.
  3. Their coordinate distance is 10 units, so the physical distance is 10(0.5)=510(0.5)=5 m. A single scale factor is valid here because both axes use the same scale.

Related topics

Midpoint Formula locates the centre of a segment. Circles in Coordinate Geometry uses a fixed distance to define an entire curve.

Sources and further reading