Distance from a point to a line
Encyclopedia
The distance from a point to a line is the shortest distance from a point
Point (geometry)
In geometry, topology and related branches of mathematics a spatial point is a primitive notion upon which other concepts may be defined. In geometry, points are zero-dimensional; i.e., they do not have volume, area, length, or any other higher-dimensional analogue. In branches of mathematics...

 to a line
Line (mathematics)
The notion of line or straight line was introduced by the ancient mathematicians to represent straight objects with negligible width and depth. Lines are an idealization of such objects...

 in Euclidean geometry
Euclidean geometry
Euclidean geometry is a mathematical system attributed to the Alexandrian Greek mathematician Euclid, which he described in his textbook on geometry: the Elements. Euclid's method consists in assuming a small set of intuitively appealing axioms, and deducing many other propositions from these...

. It can be calculated in the following ways.

Cartesian coordinates

In the case of a line in the plane given by the equation where a, b and c are real
Real number
In mathematics, a real number is a value that represents a quantity along a continuum, such as -5 , 4/3 , 8.6 , √2 and π...

 constants with a and b not both zero, the distance from the line to a point (x0,y0) is

Vector formulation

Suppose we express the line in vector form:


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

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


This more general formula can be used in dimensions other than two. This equation is constructed geometrically as follows: is a vector from to the point on the line. Then is the projected length onto the line and so
is a vector that is the projection of onto the line and so
is the component of perpendicular to the line. The distance from the point to the line is then just the norm
Norm (mathematics)
In linear algebra, functional analysis and related areas of mathematics, a norm is a function that assigns a strictly positive length or size to all vectors in a vector space, other than the zero vector...

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

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

Then


So the distance is

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

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 , and the proof is complete.

Sample code

The following Java snippet provide distance from point P to the line 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;
}
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK