Lably
Skip to content

Distance from a point to a line

Geometry problem

  • 5 min read
  • 6 chapters

Related on Lably

Through Euclidean distance, Point (geometry) and Line (geometry)

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

distance⁡(ax+by+c=0,(x0,y0))=|ax0+by0+c|a2+b2.

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

x=b(bx0−ay0)−aca2+b2 and y=a(−bx0+ay0)−bca2+b2

or equivalently

x=x0−aax0+by0+ca2+b2 and y=y0−bax0+by0+ca2+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 that 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:

distance⁡(P1,P2,(x0,y0))=|(y2−y1)x0−(x2−x1)y0+x2y1−y2x1|(y2−y1)2+(x2−x1)2.

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: Area of a triangle § Using coordinates. The expression is equivalent to h=2Ab, which can be obtained by rearranging the standard formula for the area of a triangle: A=12bh, where b is the length of a side, and h is the perpendicular height from the opposite vertex.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Mathematics