Closest point on line segment to point Presentation Transcript
Closest Point on line segment to point
Let AB be a line segment specified by the endpoints A and B. Given an arbitrary point C, the problem is to determine the point D on AB closest to C. As shown in Figure 5.2, projecting C onto the extended line through AB provides the solution. If the projection point P lies within the segment, P itself is the correct answer.
Any point on the line through AB can be expressed parametrically as P(t) = A + t (B − A). Using the projective properties of the dot product, the t corresponding to the projection of C onto the line is given by t = (C − A) · n/ _B − A_, where n = (B − A)/ _B − A_ is a unit vector in the direction of AB.
Because the closest point on the line segment is required, t must be clamped to the interval 0 ≤ t ≤ 1, after which D can be obtained by substituting t into the parametric equation.
Closest points on two lines
We know that lines in 2D always intersect unless they are parallel. 3D is otherwise. They almost never intersect. It may seem that they do but due to rounding errors they just get really close but never intersect.
The closest points of two lines can be determined as follows: Let the lines L1 and L2 be specified parametrically by the points P1 and Q1 and P2 and Q2: L1(s) = P1 + sd1, d1 = Q1 − P1 L2(t) = P2 + td2, d2 = Q2 − P2
For some pair of values for s and t, L1(s) and L2(t) correspond to the closest points on the lines, and v(s, t) = L1(s)−L2(t) describes a vector between them.
The points are at their closest when v is of minimum length. The key realization is that this happens when v is perpendicular to both L1 and L2. To see this, consider that the shortest distance between a point P and a line L is the length of a straight line between P and the point Q corresponding to the orthogonal projection of P onto L. Consequently, the line PQ is orthogonal to L. Because this reasoning holds for both L1(s) with respect to L2 and L2(t) with respect to L1, v must be perpendicular to both lines. For nonparallel lines, v is unique.
The problem is now finding values for s and t satisfying these two perpendicularity constraints:
d1 · v(s, t) = 0 d2 · v(s, t) = 0.
Substituting the parametric equation for v(s, t) gives: