The distance from a point to an infinite straight line is the length of the perpendicular segment from the point to the line. A horizontal or vertical gap gives that distance only when it happens to be perpendicular.

There are two useful methods. You can find the perpendicular foot and apply the point-to-point distance formula, or use a direct formula involving the line’s coefficients. Both describe the same geometric length.

On this page

Why the shortest segment is perpendicular

Let PP be the point and let HH be its perpendicular foot on the line. For any other point QQ on the line, triangle PHQPHQ is right-angled at HH, so

PQ2=PH2+HQ2≥PH2.PQ^2=PH^2+HQ^2\ge PH^2.

Equality occurs at Q=HQ=H. Therefore PHPH is the shortest distance. If PP already lies on the line, the distance is zero.

The perpendicular foot gives the shortest distance

The perpendicular foot gives the shortest distancePoint P at three, two is joined to H at three halves, seven halves on y equals x plus two.−1123456−11234560xyP (3, 2)H (3/2, 7/2)Q (3, 5)
The perpendicular length 3/√2 is less than the vertical gap of 3.
Diagram explanation and exact data

H satisfies both y = x + 2 and the perpendicular y = −x + 5. The dashed P-to-Q segment is not perpendicular and is longer.

The perpendicular foot gives the shortest distance: exact reference data
Point or segment Exact data
P (3,2)(3,2)
H (3/2,7/2)(3/2,7/2)
Q (3,5)(3,5)
PH 3/23/\sqrt2
PQ 33

Worked example by finding the foot

Find the distance from P=(3,2)P=(3,2) to the line y=x+2y=x+2. The line has slope 1, so the perpendicular through PP has slope −1-1:

y−2=−(x−3)⇒y=−x+5.y-2=-(x-3)\Rightarrow y=-x+5.

At the intersection,

x+2=−x+5⇒2x=3⇒x=32.x+2=-x+5\Rightarrow2x=3\Rightarrow x=\frac32.

Then y=32+2=72y=\tfrac32+2=\tfrac72, giving H=(32,72)H=(\tfrac32,\tfrac72).

Apply the distance formula:

PH=(3−32)2+(2−72)2=94+94=32=322.\begin{aligned}PH&=\sqrt{\left(3-\frac32\right)^2+\left(2-\frac72\right)^2}\\&=\sqrt{\frac94+\frac94}=\frac3{\sqrt2}=\frac{3\sqrt2}{2}.\end{aligned}

The point (3,5)(3,5) is also on the line, but its vertical distance of 3 from PP is longer. The perpendicular distance is approximately 2.121 units.

Direct point-to-line formula

Write the line as

Ax+By+C=0,A2+B2>0.Ax+By+C=0,\qquad A^2+B^2>0.

For P=(x0,y0)P=(x_0,y_0), the distance is

d=|Ax0+By0+C|A2+B2.d=\frac{|Ax_0+By_0+C|}{\sqrt{A^2+B^2}}.

The absolute value makes the result non-negative. The denominator accounts for the size of the normal vector (A,B)(A,B). A derivation using the normal direction is given in Wolfram MathWorld’s point-line distance reference.

For y=x+2y=x+2, use x−y+2=0x-y+2=0. Hence A=1A=1, B=−1B=-1 and C=2C=2:

d=|3−2+2|12+(−1)2=32.d=\frac{|3-2+2|}{\sqrt{1^2+(-1)^2}}=\frac3{\sqrt2}.

This agrees with the foot method. Be careful about signs: when a line is initially written Ax+By=DAx+By=D, move DD to the left, so the formula’s constant is C=−DC=-D.

Why rescaling the line equation changes nothing

Multiplying the entire line equation by a non-zero constant λ\lambda gives the same line. In the distance formula, the numerator is multiplied by |λ||\lambda|, and so is the denominator:

(λA)2+(λB)2=|λ|A2+B2.\sqrt{(\lambda A)^2+(\lambda B)^2}=|\lambda|\sqrt{A^2+B^2}.

The factors cancel. This is why x−y+2=0x-y+2=0 and 2x−2y+4=02x-2y+4=0 give the same distance. Multiplying only some terms would not preserve the line.

Another example and the perpendicular foot formula

Find the distance from the origin to 3x+4y−20=03x+4y-20=0:

d=|3(0)+4(0)−20|32+42=205=4.d=\frac{|3(0)+4(0)-20|}{\sqrt{3^2+4^2}}=\frac{20}{5}=4.

The foot can also be obtained without solving a second line equation. Define

q=Ax0+By0+CA2+B2.q=\frac{Ax_0+By_0+C}{A^2+B^2}.

Then the foot is

H=(x0−Aq,y0−Bq).H=(x_0-Aq,\ y_0-Bq).

In this example q=−20/25=−4/5q=-20/25=-4/5, so H=(12/5,16/5)H=(12/5,16/5). It lies on the line because 3(12/5)+4(16/5)=203(12/5)+4(16/5)=20, and its distance from the origin is 4.

Vertical lines, horizontal lines and parallel lines

For a vertical line x=kx=k, the distance from (x0,y0)(x_0,y_0) is |x0−k||x_0-k|. For a horizontal line y=ky=k, it is |y0−k||y_0-k|. Both follow directly from the general formula.

For parallel lines written with identical A,BA,B coefficients,

Ax+By+C1=0,Ax+By+C2=0,Ax+By+C_1=0,\qquad Ax+By+C_2=0,

the distance between them is |C2−C1|/A2+B2|C_2-C_1|/\sqrt{A^2+B^2}. Normalize the coefficients first; subtracting the constants of differently scaled equations is not valid.

A segment is not an infinite line

If the perpendicular foot lies outside a specified segment, the nearest point on that segment is an endpoint, not the foot on its infinite extension.

For example, consider P=(4,2)P=(4,2) and the horizontal segment from (0,0)(0,0) to (1,0)(1,0). The distance to the infinite line y=0y=0 is 2, but the foot (4,0)(4,0) is outside the segment. The nearest endpoint is (1,0)(1,0), at distance 32+22=13\sqrt{3^2+2^2}=\sqrt{13}.

Always identify whether the target is a line, ray or segment before choosing a distance method.

Practice questions

  1. Find the distance from (3,2)(3,2) to y=x+2y=x+2.
  2. Find the distance from (0,0)(0,0) to 3x+4y=203x+4y=20.
  3. Find the distance from (5,−1)(5,-1) to the vertical line x=2x=2.
Check your answers
  1. Write x−y+2=0x-y+2=0. The distance is |3−2+2|/2=3/2|3-2+2|/\sqrt2=3/\sqrt2 units.
  2. Write 3x+4y−20=03x+4y-20=0. The distance is 20/9+16=420/\sqrt{9+16}=4 units.
  3. The perpendicular direction is horizontal, so the distance is |5−2|=3|5-2|=3 units.

Related topics

Circles in Coordinate Geometry compares the distance of a line from a centre with a radius. Parabolas uses equality of point-to-point and point-to-line distances to define a curve.

Sources and further reading