Distance From a Point to a Straight Line
On this page we'll derive an engaging formula for the distance from a point to a straight line. If a line L is given by its general equation
(1)
Ax + By + C = 0
and a point P = (u, v) is given in the plane, then the distance
(2)
dist(P, L) = |Au + Bv + C| / ||n||,
where n = (A, B) is the vector of the coefficients, normal to the line. ||n|| is the norm of the vector. If the equation is normalized, i.e., if
(2')
dist(P, L) = |Au + Bv + C|.
To see why this is so, observe that (1) tells us that a straight line is a level curve
(3)
f(x, y) = Ax + By + C.
(For any function f of any number of arguments, a level surface is the locus of points where f takes on the same value
For the function (3), the level curves
Ax + By + C = c
are all straight lines parallel to the one given by (1) because all of them are perpendicular to the same normal vector
(In the applet below, a straight line is defined by two points, each of which can be dragged independently causing a rotation around the other point. The line can also be dragged parallel to itself at any other point. There is an additional point with the distance to the line indicated in blue. The point can also be dragged. Note: the applet gives you a precision of 0 to 6 digits. The equalities therefore are not quite exact.)
What if applet does not run? |
The distance from a point to a line (or to any set, for that matter) is defined as the minimum distance between the given point and the points on the line:
dist(P, L) = min(dist(P, r)),
over all r = (x, y) belong to line L. The minimum always exists for a straight line, but may not exist for other sets. For a straight line, the minimum is achieved at the foot of the perpendicular from the given point to the line. It is noteworthy that all points on one of the level lines have the same distance to any other level line, because the lines are parallel and the distances are measured via the perpendiculars. This is how the normal to the line comes into the play.
The function does not change its value along the level lines. All the change comes along the direction of the normal
m = (A/||n||, B/||n||).
Pick a point (x0, y0) and define
f(x1, y1) - f(x0, y0) | = (Ax1 + By1 + C) - (Ax0 + By0 + C) |
= A·A/||n|| + B·B/||n|| | |
= A2/||n|| + B2/||n|| | |
= (A2 + B2)/||n|| | |
= ||n||2/ ||n|| | |
= ||n||. |
That is, every step by one unit in the direction normal to the line change the value of the function by the norm of its normal vector? This is true for any linear function and for any form of its equation. In particular, if the equation is normalized to start with, a unit step in the direction normal to the line changes its value by exactly 1!. With every unit step, the values on the level lines grow by 1 in the direction of the normal, but they decrease by 1 in the opposite direction. The line (1) divides the plane into two halves. In one, which is pointed to by the normal
Once this simple behavior has been observed, let
Define now
(4)
(x, y) = (u, v) - d(±m).
where we choose the sign "+" if P lies in the "positive half-plane". We choose "-" otherwise.
Since d is the distance from P = (u, v) to the line, and the distance from a point to the line is measured along the normal, i.e. m, the point
0 | = f(x, y) |
= A(u - d·(±A/||n||)) + B(v - d·(±B/||n||)) + C | |
= (Au + Bv + C) - d·[±(A2/||n|| + B2/||n||)] | |
= f(u, v) - [±d||n||]. |
In other words,
(5)
d = ± f(u, v) / ||n||,
where the sign is chosen so as to make ± f(u, v) positive. If d is considered the signed distance, i.e., if d comes with a sign minus if
(5')
d = f(u, v) / ||n||.
In any event, we can conclude that (2) is indeed true.
If a straight line is given by an implicit equation
f(r) = n·(r - r0)
for r not necessarily on the line. Then the distance d from a point P to the line can be found from
d = | n·(P - r0)| / ||n|| = | f(P)/||n|| |.
|Contact| |Front page| |Contents| |Algebra|
Copyright © 1996-2018 Alexander Bogomolny
71867334