Shortest Distance between Two Line Segments

​
xy coordinates:
A
xy
B
xy
C
xy
D
xy
z coordinates:
A
z
B
z
C
z
D
z
show plane for AB
show plane for CD
Select four points
A
,
B
,
C
,
D
in three dimensions to determine two line segments
AB
(green) and
CD
(blue). This Demonstration finds the shortest line segment between the segments
AB
and
CD
and draws it in red. The shortest line segment between the full lines
AB
and
CD
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
ABCD
. All possible solutions are drawn.

Details

To determine the shortest line joining two line segments,
AB
(green) and
CD
(blue), first compute the shortest line joining the full lines
AB
and
CD
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,
A
,
B
,
C
or
D
, 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
x
,
y
coordinates of points
A
,
B
,
C
and
D
; and the sliders on the left control the
z
coordinates. The minimum distance is displayed at the top.

External Links

Shortest Distance between Two Skew Lines
Skew Lines
Distance of a Point to a Segment
Distance from a Point to a Line
Line through Two Points
Noncoplanar Lines
Shortest Distance between a Point and a Line in 2D
Vectors in 3D

Permanent Citation

Aaron T. Becker, Victor M. Baez, Nikhil Navkar
​
​"Shortest Distance between Two Line Segments"​
​http://demonstrations.wolfram.com/ShortestDistanceBetweenTwoLineSegments/​
​Wolfram Demonstrations Project​
​Published: May 26, 2023