# .

The distance from a point to a line is the shortest distance from a point to a line in Euclidean geometry. It can be calculated in the following 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

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

Vector formulation

Illustration of the vector formulation.

Suppose we express the line in vector form:

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

where n is a unit vector. 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 more general formula can be used in dimensions other than two. This equation 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 and so

$$(\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. Proof 1 (algebraic proof) Let point (x,y) be the intersection between the line ax + by + c = 0 and its perpendicular which contains (m,n), where point (m,n) is any arbitrary point on the perpendicular line to ax + by + c = 0. Then it is necessary to show\( a^2(n-y)^2 + b^2(m-x)^2 = 2ab(m-x)(n-y).$$

The above equation can be changed to $$(a^2(n-y))/(m-x) + (b^2(m-x))/(n-y) = 2ab$$, because the slope of the perpendicular to the ax+by+c which contains (x,y) and (m,n) is b/a.

Then

$$(a^2+b^2)((m-x)^2+(n-y)^2)=[a(m-x)+b(n-y)]^2=(am+bn+c)^2. So the distance is \( d=\sqrt{(m-x)^2+(n-y)^2}= |am+bn+c|/\sqrt{a^2+b^2} Proof 2 (geometric proof) Let the point S(m,n) connect to the point G(x,y) which is on the line ax+by+c=0, both lines being perpendicular to each other. Draw a line am+bn+d=0, containing the point S(m,n), which is parallel to ax+by+c=0. The absolute value of (c-d)/b, which is the distance of the line connecting the point G and some point F on the line am+bn+d=0 and parallel to the y-axis, is equal to the absolute value of (am+bn+c)/b. Then the desired distance SG can be derived from the right triangle SGF, which is in the ratio of \( a:b:\sqrt{a^2+b^2}.$$

The absolute value of $$(am+bn+c)/b$$is the diagonal of the right triangle, so just multiply by the absolute value of b and divide by $$\sqrt{a^2+b^2}$$, and the proof is complete.
Sample code

The following Java snippet provide distance from point P to the line that passes through A-B:

public double pointToLineDistance(Point A, Point B, Point P)
{
double normalLength = Math.sqrt((B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y));
return Math.abs((P.x - A.x) * (B.y - A.y) - (P.y - A.y) * (B.x - A.x)) / normalLength;
}