Distance from a point to a line explained

The distance (or perpendicular distance) from a point to a line is the shortest distance from a fixed point to any point on a fixed infinite 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. The formula for calculating it can be derived and expressed in several ways.

Knowing the shortest distance from a point to a line can be useful in various situations - for example, finding the shortest distance to reach a road, quantifying the scatter on a graph, etc. In Deming regression, a type of linear curve fitting, if the dependent and independent variables have equal variance this results in orthogonal regression in which the degree of imperfection of the fit is measured for each data point as the perpendicular distance of the point from the regression line.

Cartesian coordinates

Line defined by an equation

In the case of a line in the plane given by the equation 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

\operatorname{distance}(ax+by+c=0,(x0,y0))=

|ax0+by0+c|
\sqrt{a2+b2
}.

The point on this line which is closest to (x0,y0) has coordinates:

x=

b(bx0-ay0)-ac
a2+b2

andy=

a(-bx0+ay0)-bc
a2+b2

.

Horizontal and vertical lines

In the general equation of a line, ax + by + c = 0, a and b cannot both be zero unless c is also zero, in which case the equation does not define a line. If a = 0 and b  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.

Line defined by two points

If the line passes through two points P1=(x1,y1) and P2=(x2,y2) then the distance of (x0,y0) from the line is:

\operatorname{distance}(P1,P2,(x0,y0))=

|(y2-y1)x0-(x2-x1)y0+x2y1-y2x1|
\sqrt{(y
2+(x
2-x
2
1)
2-y
}.

The denominator of this expression is the distance between P1 and P2. The numerator is twice the area of the triangle with its vertices at the three points, (x0,y0), P1 and P2. See: . The expression is equivalent to h=\frac, which can be obtained by rearranging the standard formula for the area of a triangle: A=\frac bh, where b is the length of a side, and h is the perpendicular height from the opposite vertex.

Proofs

An algebraic proof

This proof is valid only 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

y0-n=
x0-m
b
a

.

Thus,

a(y0-n)-b(x0-m)=0,

and by squaring this equation we obtain:
2(y
a
0

-n)2+

2(x
b
0

-m)2=2ab(y0-n)(x0-m).

Now consider,

(a(x0-m)+b(y0-n))2=

2(x
a
0

-m)2+2ab(y0-n)(x0-m)+

2(y
b
0

-n)2=(a2+

2)((x
b
0

-m)2+(y0-n)2)

using the above squared equation. But we also have,

(a(x0-m)+b(y0-n))2=(ax0+by0-am-bn)2=(ax0+by0+c)2

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

(a2+

2)((x
b
0

-m)2+(y0-n)2)=(ax0+by0+c)2

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

d=\sqrt{(x0-

2+(y
m)
0

-n)2}=

|ax0+by0+c|
\sqrt{a2+b2
}.[1]

A geometric proof

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

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 -A/B.

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

|\overline{PR
|}{|\overline{PS}|}

=

|\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}|=

|y0-m||B|
\sqrt{A2+B2
}.Since S is on the line, we can find the value of m,

m=

-Ax0-C
B

,

and finally obtain:

|\overline{PR}|=

|Ax0+By0+C|
\sqrt{A2+B2
}.

A variation of this proof is to place V at P and compute the area of the triangle ∆UVT two ways to obtain that

D|\overline{TU}|=|\overline{VU}||\overline{VT}|

where D is the altitude of ∆UVT drawn to the hypoteneuse of ∆UVT from P. The distance formula can then used to express

|\overline{TU}|

,

|\overline{VU}|

, and

|\overline{VT}|

in terms of the coordinates of P and the coefficients of the equation of the line to get the indicated formula.

A 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=

|\overrightarrow{QP

n|}{\|n\|}.

Now,

\overrightarrow{QP}=(x0-x1,y0-y1),

so

\overrightarrow{QP}n=a(x0-x1)+b(y0-y1)

and

\|n\|=\sqrt{a2+b2},

thus

d=

|a(x0-x1)+b(y0-y1)|
\sqrt{a2+b2
}.Since Q is a point on the line,

c=-ax1-by1

, and so,

d=

|ax0+by0+c|
\sqrt{a2+b2
}.

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 (

x0,y0

).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=x0-x
m

+y0

.

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

mx+k=x0-x
m

+y0.

We can solve this equation for x,
x=x0+my0-mk
m2+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(x0+my0-mk)
m2+1

+k.

Using the equation for finding the distance between 2 points,

d=\sqrt{(X2-X

2+(Y
2-Y
2}
1)
, we can deduce that the formula to find the shortest distance between a line and a point is the following:

d=\sqrt{\left({

x0+my0-mk
m2+1

-x0}\right)2+\left({m

x0+my0-mk
m2+1

+k-y0}\right)2}=

|k+mx0-y0|
\sqrt{1

+m2}.

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.

Vector formulation

The equation of a line can be given in vector form:

x=a+tn

Here is the position of a point on the line, and is a unit vector in the direction of the line. Then as scalar t varies, gives the locus of the line.

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

\operatorname{distance}(x=a+tn,p)=\|(a-p)-((a-p)n)n\|.

This formula can be derived as follows:

a-p

is a vector from to the point on the line. Then

(a-p)n

is the projected length onto the line and so

((a-p)n)n

is a vector that is the projection of

a-p

onto the line. Thus

(a-p)-((a-p)n)n

is the component of

a-p

perpendicular to the line. The distance from the point to the line is then just the norm of that vector.[4] This more general formula is not restricted to two dimensions.

Another vector formulation

\vecu

, the distance between point P and line (l) is

d(P,(l))=

\left\|\overrightarrow{AP
x \vec

u\right\|}{\|\vecu\|}

where

\overrightarrow{AP

} \times\vec u is the cross product of the vectors

\overrightarrow{AP

} and

\vecu

and where

\|\vecu\|

is the vector norm of

\vecu

.

Note that cross products only exist in dimensions 3 and 7 and trivially in dimensions 0 and 1 (where the cross product is constant 0).

See also

Notes and References

  1. Between Certainty and Uncertainty: Statistics and Probability in Five Units With Notes on Historical Origins and Illustrative Numerical Examples
  2. do not mention this restriction in their article
  3. If the two triangles are on opposite sides of the line, these angles are congruent because they are alternate interior angles.
  4. Web site: Sunday. Dan. Lines and Distance of a Point to a Line. softSurfer. 6 December 2013.