Shortest Distance between Two Line Segments
Select four points , , , in three dimensions to determine two line segments (green) and (blue). This Demonstration finds the shortest line segment between the segments and and draws it in red. The shortest line segment between the full lines and is drawn in yellow. If the segments are parallel, there can be an infinite number of solution lines, which are represented by the 3D rectangle . All possible solutions are drawn.
To determine the shortest line joining two line segments, (green) and (blue), first compute the shortest line joining the full lines and and draw it in yellow. If this line is not contained within the two line segments, then the shortest line contains at least one of the segment endpoints, , , or , and is drawn in red.
If a line segment consists of a single point, then compute the shortest distance by projecting the point onto the second line segment. If the projection is within the line segment, this is the nearest point; otherwise, the nearest segment endpoint is selected. The solution is trivial if the segments intersect. If the lines are parallel and the segments overlap, there is a range of possible line segments, all the same length. These solutions are drawn as a transparent red rectangle as shown in Snapshot 2.
The sliders at the top control the , coordinates of points , , and ; and the sliders on the left control the coordinates. The minimum distance is displayed at the top.