Shortest Distance between two Line Segments

Franz J. Vesely
May 2002

Model systems made of sticks play a role both in Statistical Mechanics
(Hard Spherocylinders, Linear Kihara Molecules) and in Robotics (Robot
Arms). The basic exercise in both settings is to determine the shortest
distance between two such line segments. The method sketched here was
described by Allen et al., Adv. Chem. Phys. Vol. LXXXVI, 1993, p.1.

Shortest distance between two line segments

We divide the problem in two steps:

Determine the distance in 3D space between
the two ``carrier'' lines of the line segments; keep the vector
between the closest points on the two lines.
Note that this vector is normal to both lines.

Project the two lines onto a plane normal to .
Since is normal to both lines this projection conserves
distances on the lines.
Now determine the shortest distance within the plane between
the two line segments.

The shortest total distance is then
.

Distance between two lines in 3D:
Let and denote the center positions and
directional unit vectors of the two segments of lengths .
The carrier lines are then given by
and
.
The vector between any two points on the two lines is then

(1)

with
. The shortest among these
vectors is perpendicular to both lines; thus

(2)

(3)

Solving for the line parameters we find

(4)

(5)

(If you suspect that your lines may be nearly parallel, you should take
care of the small denominator in some meaningful manner.)

The vector between the proximate points is

(6)

and the minimum interline distance is
.

Distance between two line segments in 2D:
Think of the two carrier lines being projected down into a plane
normal to . This projection, being normal to both lines,
conserves distances along the lines.

The intersection point is, of course, given by
.
Taking this as the origin, the lines are now represented
by
and
.
For points on the given segments the parameters and
are confined to
and
which defines a
rectangle in (
) parameter space. We are now searching
for the minimum of
within that parameter region:
with

(7)

Figure: Finding the minimum of f=gamma**2+delta**2+2*gamma*delta*(e1*e2).
The bold sides of the rectangle are the lines gamma_m, delta_m (see text)

If the origin is contained in the rectangle,
life is easy: the origin is the minimum, and
the in-plane distance is zero!

If not, proceed as follows:
First, find the lines , that are nearest to the
origin:
,
.

Now choose the line
and find the value of that
minimizes :
.
If is in the permitted range, we set
;
otherwise, we pick the allowed nearest to .
The distance at
is computed:

The same steps are taken for
and its optimal ,
yielding a trial value .