# Distance from a point to a line

The distance (or perpendicular distance) from a point to a line is the shortest distance from a point to a line in Euclidean geometry. It is the length of the line segment which joins the point to the line and is perpendicular to the line. Knowing the shortest distance from a point to a line can be useful in a few situations. For example, the shortest distance to reach a road, quantifying the scatter on a graph, etc. It can be calculated in several ways.

## Cartesian coordinates

In the case of a line in the plane given by the equation ax + by + c = 0, where a, b and c are real constants with a and b not both zero, the distance from the line to a point (x0,y0) is[1]

$\operatorname{distance}(ax+by+c=0, (x_0, y_0)) = \frac{|ax_0+by_0+c|}{\sqrt{a^2+b^2}}.$

If the point on this line which is closest to (x0,y0) has coordinates (x1,y1), then:[2]

$x_1 = \frac{b(bx_0 - ay_0)-ac}{a^2 + b^2} \text{ and } y_1 = \frac{a(-bx_0 + ay_0) - bc}{a^2+b^2}.$

## Vector formulation

Illustration of the vector formulation.

Write the line in vector form:

$\mathbf{x} = \mathbf{a} + t\mathbf{n}$

where n is a unit vector in the direction of the line, a is a point on the line and t is a scalar. That is, a point x on the line, is found by moving to a point a in space, then moving t units along the direction of the line.

The distance of an arbitrary point p to this line is given by

$\operatorname{distance}(\mathbf{x} = \mathbf{a} + t\mathbf{n}, \mathbf{p}) = \| (\mathbf{a}-\mathbf{p}) - ((\mathbf{a}-\mathbf{p}) \cdot \mathbf{n})\mathbf{n} \|.$

This formula is constructed geometrically as follows: $\mathbf{a}-\mathbf{p}$ is a vector from p to the point a on the line. Then $(\mathbf{a} - \mathbf{p}) \cdot \mathbf{n}$ is the projected length onto the line and so

$((\mathbf{a} - \mathbf{p}) \cdot \mathbf{n})\mathbf{n}$

is a vector that is the projection of $\mathbf{a}-\mathbf{p}$ onto the line. Thus

$(\mathbf{a}-\mathbf{p}) - ((\mathbf{a}-\mathbf{p}) \cdot \mathbf{n})\mathbf{n}$

is the component of $\mathbf{a}-\mathbf{p}$ perpendicular to the line. The distance from the point to the line is then just the norm of that vector.[3] This more general formula can be used in dimensions other than two.

## Another vector projection proof

Let P be the point with coordinates (x0, y0) and let the given line have equation ax + by + c = 0. Also, let Q = (x1, y1) be any point on this line and n the vector (a, b) starting at point Q. The vector n is perpendicular to the line, and the distance d from point P to the line is equal to the length of the orthogonal projection of $\overrightarrow{QP}$ on n. The length of this projection is given by:

$d = \frac{|\overrightarrow{QP} \cdot \mathbf{n}|}{\| \mathbf{n}\|}.$

Now,

$\overrightarrow{QP} = (x_0 - x_1, y_0 - y_1),$ so $\overrightarrow{QP} \cdot \mathbf{n} = a(x_0 - x_1) + b(y_0 - y_1)$ and $\| \mathbf{n} \| = \sqrt{a^2 + b^2},$

thus

$d = \frac{|a(x_0 - x_1) + b(y_0 - y_1)|}{\sqrt{a^2 + b^2}}.$

Since Q is a point on the line, $c = -ax_1 - by_1$, and so,[4]

$d = \frac{|ax_0 + by_0 + c|}{\sqrt{a^2 + b^2}}.$

## Two special cases

In proofs that involve slopes of lines (as the ones below will), the main argument usually fails if the line in question is either vertical or horizontal. However, the result remains valid in these cases. In the general equation of a line, ax + by + c = 0, a and b can not both be zero. If a = 0, the line is horizontal and has equation y = -c/b. The distance from (x0, y0) to this line is measured along a vertical line segment of length |y0 - (-c/b)| = |by0 + c| / |b| in accordance with the formula. Similarly, for vertical lines (b = 0) the distance between the same point and the line is |ax0 + c| / |a|, as measured along a horizontal line segment.

## An algebraic proof

This proof is only valid if the line is neither vertical nor horizontal, that is, we assume that neither a nor b in the equation of the line is zero.

The line with equation ax + by + c = 0 has slope -a/b, so any line perpendicular to it will have slope b/a (the negative reciprocal). Let (m, n) be the point of intersection of the line ax + by + c = 0 and the line perpendicular to it which passes through the point (x0, y0). The line through these two points is perpendicular to the original line, so

$\frac{y_0 - n}{x_0 - m}=\frac{b}{a}.$

Thus, $a(y_0 -n) - b(x_0 - m) = 0,$ and by squaring this equation we obtain:

$a^2(y_0 - n)^2 + b^2(x_0 - m)^2 = 2ab(y_0 - n)(x_0 - m).$

Now consider,

$(a(x_0 - m) + b(y_0 - n))^2 = a^2(x_0 - m)^2 + 2ab(y_0 -n)(x_0 - m) + b^2(y_0 - n)^2 = (a^2 + b^2)((x_0 - m)^2 + (y_0 - n)^2)$

using the above squared equation. But we also have,

$(a(x_0 - m) + b(y_0 - n))^2 = (ax_0 + by_0 - am -bn )^2 = (ax_0 + by_0 + c)^2$

since (m, n) is on ax + by + c = 0. Thus,

$(a^2 + b^2)((x_0 - m)^2 + (y_0 - n)^2) = (ax_0 + by_0 + c)^2$

and we obtain the length of the line segment determined by these two points,

$d=\sqrt{(x_0 - m)^2+(y_0 - n)^2}= \frac{|ax_0+ by_0 +c|}{\sqrt{a^2+b^2}}.$[5]

## A geometric proof

Diagram for geometric proof

This proof is valid only if the line is not horizontal or vertical.[6]

Drop a perpendicular from the point P with coordinates (x0, y0) to the line with equation Ax + By + C = 0. Label the foot of the perpendicular R. Draw the vertical line through P and label its intersection with the given line S. At any point T on the line, draw a right triangle TVU whose sides are horizontal and vertical line segments with hypotenuse TU on the given line and horizontal side of length |B| (see diagram). The vertical side of ∆TVU will have length |A| since the line has slope -B/A.

SRP and ∆TVU are similar triangles since they are both right triangles and ∠PSR ≅ ∠VUT since they are corresponding angles of a transversal to the parallel lines PS and UV (both are vertical lines).[7] Corresponding sides of these triangles are in the same ratio, so:

$\frac{|\overline{PR}|}{|\overline{PS}|} = \frac{|\overline{TV}|}{|\overline{TU}|}.$

If point S has coordinates (x0,m) then |PS| = |y0 - m| and the distance from P to the line is:

$|\overline{PR} | = \frac{|y_0 - m||B|}{\sqrt{A^2 + B^2}}.$

Since S is on the line, we can find the value of m,

$m = \frac{-Ax_0 - C}{B},$

and finally obtain:[8]

$|\overline{PR}| = \frac{|Ax_0 + By_0 + C|}{\sqrt{A^2 + B^2}}.$

## Another formula

It is possible to produce another expression to find the shortest distance of a point to a line. This derivation also requires that the line is not vertical or horizontal.

The point P is given with coordinates ($x_0, y_0$). The equation of a line is given by $y=mx+k$. The equation of the normal of that line which passes through the point P is given $y=\frac{x_0-x}{m}+y_0$.

The point at which these two lines intersect is the closest point on the original line to the point P. Hence:

$mx+k=\frac{x_0-x}{m}+y_0.$

We can solve this equation for x,

$x=\frac{x_0+my_0-mk}{m^2+1}.$

The y coordinate of the point of intersection can be found by substituting this value of x into the equation of the original line,

$y=m\frac{(x_0+my_0-mk)}{m^2+1}+k.$

Using the equation for finding the distance between 2 points, $d=\sqrt{(X_2-X_1)^2+(Y_2-Y_1)^2}$, we can deduce that the formula to find the shortest distance between a line and a point is the following:

$d=\sqrt{ \left( {\frac{x_0 + m y_0-mk}{m^2+1}-x_0 } \right) ^2 + \left( {m\frac{x_0+m y_0-mk}{m^2+1}+k-y_0 }\right) ^2 }.$

Recalling that m = -a/b and k = - c/b for the line with equation ax + by + c = 0, a little algebraic simplification reduces this to the standard expression.[9]

## Line defined by two points

If the line passes through two points, $(x_1,y_1)$ and $(x_2,y_2)$, and if we write $D_x$ for $(x_2-x_1)$ and $D_y$ for $(y_2-y_1)$, the perpendicular distance from $(x_0,y_0)$ to the line is given by:

$d=\frac{|D_yx_0-D_xy_0-x_1y_2+x_2y_1|}{\sqrt{(D_x)^2+(D_y)^2}}$

The denominator of this expression is the distance between (x1,y1) and (x2,y2). The numerator is twice the area of the triangle with its vertices at the three points, (x0,y0), (x1,y1), and (x2,y2). See: Area of a triangle#Using coordinates. The expression is equivalent to $\scriptstyle h=\frac{2A}{b}$, which can be obtained by rearranging the standard formula for the area of a triangle: $\scriptstyle A=\frac{1}{2} bh$, where b is the length of a side, and h is the perpendicular height from the opposite vertex.

## Notes

1. ^ Larson & Hostetler 2007, p. 452
2. ^ Larson & Hostetler 2007, p. 522
3. ^ Sunday, Dan. "Lines and Distance of a Point to a Line". softSurfer. Retrieved 6 December 2013.
4. ^ Anton 1994, pp. 138-9
5. ^ Between Certainty and Uncertainty: Statistics and Probability in Five Units With Notes on Historical Origins and Illustrative Numerical Examples
6. ^ Ballantine & Jerbert 1952 do not mention this restriction in their article
7. ^ If the two triangles are on opposite sides of the line, these angles are congruent because they are alternate interior angles.
8. ^ Ballantine & Jerbert 1952
9. ^ Larson & Hosttler 2007, p. 522

## References

• Anton, Howard (1994), Elementary Linear Algebra (7th ed.), John Wiley & Sons, ISBN 0-471-58742-7
• Ballantine, J.P.; Jerbert, A.R. (1952), "Distance from a line or plane to a point", American Mathematical Monthly 59: 242–243
• Larson, Ron; Hostetler, Robert (2007), Precalculus: A Concise Course, Houghton Mifflin Co., ISBN 0-618-62719-7