2. 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.
3. 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.
4. 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.
6. 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.
7. 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
8. 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.
9. 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.
10. 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: d1 · (L1(s) − L2(t)) = d1 · ((P1 − P2) + sd1 − td2) = 0 d2 · (L1(s) − L2(t)) = d2 · ((P1 − P2) + sd1 − td2) = 0. This can be expressed as the 2 × 2 system of linear equations (d1 · d1)s − (d1 · d2)t = −(d1 · r) (d2 · d1)s − (d2 · d2)t = −(d2 · r), where r = P1 − P2.