Solution

ID: past-exam-of-the-mathematics-course-of-the-university-of-cambridge/2006/iii/paper-66/1/solution

Use closest points on two line segments as a small constrained convex optimization problem. Write the two line segments as
and put . Minimize the squared Euclidean distance ; minimizing its square also minimizes the distance. Compute the dot products
For nonzero, nonparallel directions, an interior minimum must satisfy the two derivative equations
Their determinant is , so the stationary candidate is
Retain this candidate only when both parameters lie in the unit interval.
The other possibilities are on the boundary. Define . Minimizing the same quadratic on each of the four edges gives these candidates by orthogonal projection onto a line segment:
Evaluate at every retained candidate and choose the smallest. The resulting are the required closest points. The quadratic is convex because it is the squared norm of an affine map. A minimum in the interior is stationary, and a minimum on the boundary minimizes one of these edge restrictions, so the list is exhaustive. Ties correspond to multiple equally valid answers.
Handle degenerate line segments before dividing: if and , set and ; if and , set and ; if both vanish, use the two endpoints. For exactly parallel directions, . The distance depends on a single linear combination of ; any interior minimizing level line reaches the boundary, so the four edge candidates still include a minimum.
For nearly parallel directions, compare with the scale , and solve the interior least-squares problem using QR decomposition or singular value decomposition if necessary. A small determinant should trigger a stable solve, not arbitrary rejection of a possibly valid interior solution. In particular, do not merely clip both coordinates of the infinite-line solution: the dot product couples the two parameters, and changing one changes the optimum for the other.

New to topics? Read the docs here!