Minimum Distance between
Written by Paul Bourke |
Sample source code
|
This note describes the technique and gives the solution to finding the shortest distance from a point to a line or line segment. The equation of a line defined through two points P1 (x1,y1) and P2 (x2,y2) is

The point P3 (x3,y3) is closest to the line at the tangent to the line which passes through P3, that is, the dot product of the tangent and line is 0, thus
Substituting the equation of the line gives
Solving this gives the value of u

Substituting this into the equation of the line gives the point of intersection (x,y) of the tangent as
The distance therefore between the point P3 and the line is the distance between (x,y) above and P3.
Notes
Let Pa = (xa, ya, za) be the point in question.
A plane can be defined by its normal n = (A, B, C) and any point on the plane Pb = (xb, yb, zb)
Any point P = (x,y,z) lies on the plane if it satisfes the following

The minimum distance between Pa and the plane is given by the absolute value of
|
| . . . 1 |
To derive this result consider the projection of the line (Pa - Pb) onto the normal of the plane n, that is just ||Pa - Pb|| cos(theta), where theta is the angle between (Pa - Pb) and the normal n. This projection is the minimum distance of Pa to the plane.
This can be written in terms of the dot product as
That is
|
| . . . 2 |
Since point (xb, yb, zb) is a point on the plane
|
| . . . 3 |
Substituting equation 3 into equation 2 gives the result shown in equation 1.