GIVEN: that there is a line L that is defined by two points, P1 and P2, and a point Q not on L.
FIND:
- the point Q0 that is the closest point to Q on the line L.
- the parametric equation of the second line L2 that contains points Q and Q0.
- the distance between the point Q and the line L.
SOLUTION:
The parametric equation of L is:
P = P1 + k * (P2 - P1)
where k is a scalar parameter that is equal to 0 when P = P1 and 1 when b>P = P2. The parameter value k can have positive or negative values.
The parametric equation of the second line L2 is:
R = Q + k2 * (Q0 - Q)
where k2 is the scalar parameter of L2 that is equal to 0 when R = Q and 1 when R = Q0. The parameter value k2 can also have positive and negative values.
Now you are ready to solve equations. The first equation is driven by the condition of normalcy. Since L2 ⊥ L, then
(Q0 - Q) • (P2 - P1) = 0
where • is the dot product. The second condition is that Q0 is a point on L:
(Q0 = P1 + k * (P2 - P1)
Solving for k:
k* = ((Q - P1) * (P2 - P1))/ |P2 - P1|2 ...or...
k* = ((Qx-P1x)(P2x-P1x)+(Qy-P1y)(P2y-P1y)) / ((P2x-P1x)2 +(P2y-P1y)2)
Now k* is that value of the line parameter k on line L which yields the coordinates of the point of intersection Q0, the point that lies on both lines L and L2. So we have our answer:
SOLUTION:
The point of intersection (which is also the point on the line L closest to point Q) is given by:
k* = ((Q - P1) * (P2 - P1))/ |P2 - P1| ...or...
k* = ((Qx-P1x)(P2x-P1x)+(Qy-P1y)(P2y-P1y)) / ((P2x-P1x)2 +(P2y-P1y)2)
and therefore the point of intersection is
Q = P1 + k* * (P2 - P1)
The parametric equation of the normal line L2 is:
R = Q + k2 * (Q0 - Q)
Finally, the distance between the point and the line is equal to
|Q0 - Q|
= SQRT((Q0x-Qx)2 +(Q0y-Qy)2)