Suppose we are given two lines in
,
and
. Line
passes through the point
and its direction is given by vector 
.
Line
passes through the point
and its direction is given by vector 
.
What is the distance between lines
and
?
__________
Lines as functions
Let vector-valued functions
and
be defined as
.
Lines
and
are thus the images of set
under functions
and
, respectively
.
Function
maps each
into point
. Therefore, choosing a particular value of
corresponds to choosing a point on line
, and vice versa (because function
is injective). Thus, each point on the line
can be unambiguously specified by a value of
. Similarly, following the same line of thought, each point on the line
can be unambiguously specified by a value of
.
__________
Distance between two points
In order to compute the distance between lines
and
, we first need to know how to compute the distance between two points in
, i.e., we need to choose a metric on
. For the sake of simplicity, we will use the Euclidean metric, i.e., we will use the 2-norm to measure lengths and distances. In other words, we will be working in normed vector space
.
Let us fix parameters
and
, i.e.,
and
, and compute the distance between fixed points
and 
.
Pictorially, we have that
is the length of the line segment connecting fixed points
and 

To each pair of parameters
, it is assigned a non-negative scalar 
.
__________
Distance between a point and a line
Suppose now that we vary
, while
remains fixed,
. As we vary
, we travel along line
and hence the distance between free point
and fixed point
is given by
.
Since
is now free,
is a function of
.
For example, let us pick three distinct values for
, say
. The image below depicts the line segments connecting fixed point
and points
. The length of these line segments gives us the distance between the fixed point and each of the three points on line
.

The distance between fixed point
and line
is thus
.
We have an optimization problem: we would like to find the value of parameter
which minimizes function
. This minimizer is (see appendix A)
.
Evaluating function
at
we obtain
, the point on line
that is closest to fixed point 
,
where

is a symmetric, idempotent (
) projection matrix, and
is the orthogonal projection of vector
onto vector
. It can be shown that vectors
and
are orthogonal (see appendix B).
For example, let us consider the
case: we have two lines in
,
and
, where
lies on plane
. Pictorially, we have

We would like to find the point on line
that is closest to fixed point
. Projecting vector
orthogonally onto vector
, we obtain
The image above illustrates rather nicely that vectors
and
are orthogonal (see appendix B). The distance between fixed point
and line
is
,
where
. Please note that plane
serves no purpose other than to make it easier to visualize in 3-d (plane
gives an impression of depth). In other words, plane
has some “artistic value”, but no “mathematical value”.
__________
Distance between two lines
Let us now vary both
and
. As we vary
and
, we travel along lines
and
, respectively. Since both parameters are free, the distance between free points
and
is
,
which is a function of both
and
. The distance between lines
and
is thus
.
The original problem of computing the distance between two lines has thus been cast as an optimization problem: we would like to find the pair
which minimizes
. Once we have found the pair of minimizers
, the distance between
and
is
.
The pair
is not necessarily unique. For example, if the lines are parallel to each other, then there are infinitely many pairs of minimizers
.
Trying to find the pair
which minimizes
is a one-stage 2-dimensional optimization problem. However, we can solve this optimization problem in two stages, where at each stage we must solve a 1-dimensional optimization problem
.
Note that

is the distance between point
and line
, and thus
.
We already know how to compute the distance between a point on line
and line
: given a value of
, the choice of
which minimizes
is
, where function
is defined as (see appendix A)
,
and the point on line
which is closest to
is
.
Therefore, for a given value of
, we have that the distance between
and line
is
.
Note that
is a function of
only, as we have already optimized function
for
. Finally, the distance between lines
and
can be found by solving a 1-dimensional optimization problem in 
.
Once we have found the
which minimizes the distance
, the distance between lines
and
is simply
.
Note that:
: lines
and
do intersect.
: lines
and
do not intersect.
The value of
can be written in terms of points
and direction vectors
, as I will show on some future posts.
__________
The shortest distance game
It might seem a bit obscure that the one-stage 2-dimensional optimization problem

is equivalent to a two-stage optimization problem where at each stage one must solve a 1-dimensional optimization problem
.
Personally, I like to look at this from a game-theoretic viewpoint. Let us play a sequential game! We shall call the shortest distance game. I play first, then you play:
- I choose a value for
, I tell you what
I chose.
- You then choose
such that
is minimized.
Note that you are required to play optimally, i.e., to minimize distance. Hence you have no freedom in the choice of
: you choose
as a response to my choice of
. Your choice is now coupled to mine, and thus, we are left with only one degree of freedom, the choice of
. I now choose the value of
which minimizes 
.
Fortunately, I can use calculus to find the minimizer
. Trying all values in
for parameter
would be rather tedious ;-)
__________
Appendix A
Note that the value of parameter
which minimizes the distance
is the same that minimizes the squared distance
. Finding the minimizer of the squared norm is much easier, thus we introduce a cost function
defined as
.
Differentiating with respect to
, we obtain

The first derivative of
must vanish at extrema. From the geometry of the problem, we know that there are no maxima (note that the cost function is unbounded above). Therefore we can find minima by checking the first derivative alone, without having to check the second derivative. We have
,
and since
, the minimizer is
.
Note that this minimizer depends on the value of fixed parameter
.
__________
Appendix B
We would like to show that vectors
and
are orthogonal. Recall that
, and thus
.
Multiplying both sides on the left by
, we obtain

and since
(recall that
), then we finally have
,
which means that vectors
and
are orthogonal.
__________
Related: