Parameterize the two line segments by
Put and abbreviate , , , , and . Minimizing the Euclidean distance is equivalent to minimizing the convex quadratic function
over a closed square. For nonzero nonparallel segment directions, . An interior minimum must satisfy
whose unique solution is
Keep it as a candidate only if both parameters lie in .
Every remaining minimum lies on the boundary. Define . The four edge minima are the point-to-segment orthogonal projections
Evaluate at those four pairs and at the admissible interior pair, and return a pair with the smallest value. This solves closest points on two line segments completely: continuity guarantees a minimum, and every interior or boundary location has been tested. Do not independently clamp and : the mixed term couples them. For example, , , and give the line minimizer . Its independently clamped pair has squared distance , whereas the correct edge minimizer has squared distance .
Without general position, explicitly handle these configurations:
  • If either segment has zero length, solve a point-to-segment projection; if both do, return the two points. This avoids division by or .
  • If the directions are parallel, and the interior linear system is singular. The four edge projections still contain a minimum. Overlapping projected parameter intervals can yield a whole family of closest pairs.
  • Collinear segments may overlap, touch, or be disjoint. Overlap gives zero distance with many choices; disjoint intervals give their nearest endpoints.
  • Coplanar nonparallel segments can intersect, including endpoint contacts. The same candidate tests return zero distance if the line-intersection parameters lie in the square. Coincident endpoints and equal candidate distances need no arbitrary division or uniqueness assumption.
In floating-point arithmetic, almost-parallel directions require a condition-aware or higher-precision solve; they must not be declared exactly parallel solely because the computed determinant is small. The returned distance is , and the returned points are obtained by substitution into .
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.