Suppose we are given two lines, , defined as follows
where , and
. Let vector-valued functions
and
be defined as
and
, so that the two lines can be written in the form
.
Suppose that we want to compute the distance between these two lines. Back in December 2008, I wrote a very long post in which I derived from first principles an expression for the distance between two lines in . To cut a long story short, the distance between
and
is given by
.
My earlier post was quite conceptual and theoretical. In this post I focus on actually computing the distance between two given lines.
__________
Introduction
Note that the difference vector can be written in matrix form, as follows
.
Let . Then, the squared 2-norm of the difference vector can be written as
where are defined as follows
Note that , and also that
is a Gramian matrix, which means that
is either positive definite or positive semidefinite. Therefore, function
is convex in
, and we conclude that the minimum exists.
The pair that minimizes the 2-norm
is the same one that minimizes the squared 2-norm
and, hence, we have the unconstrained quadratic program
which can be solved analytically. The gradient of the objective function must vanish at the minimum
and, hence, we have a linear system of two equations in two unknowns
.
If , then the system of equations has a single unique solution. However, if
, there are infinitely many solutions. Let us consider both cases separately.
__________
Non-parallel lines
If matrix is invertible, we obtain the unique minimizer
. Thus, the minimum squared 2-norm of the distance vector is
Let . Then,
and
. Hence, we obtain
and, finally, the distance between lines and
,
, is the following
.
If direction vectors are linearly independent, i.e., the two given lines are non-parallel, then
is invertible, and we have a unique minimizer. Intuitively, this makes sense.
__________
Non-parallel lines
Suppose now that the direction vectors are linearly dependent, , where
. This corresponds to the case where the two lines are parallel (or coincident). In this case
which is clearly non-invertible. The linear system of equations, , now has a redundant equation. Eliminating this redundant equation, we obtain an underdetermined system
.
which has infinitely many solutions. We have one equation and two unknowns, i.e., one degree of freedom. Let us choose . Hence, we obtain
and
.
Let . Note that
represents an orthogonal projection along vector
. Hence, a distance-minimizing difference vector would be
.
Note that this difference vector is not unique (there are infinitely many alternatives). Finally, we obtain the distance between the two parallel lines
.
If is singular and
, we conclude that the two lines are coincident.
__________
Implementation
Here’s a MATLAB script that computes the distance between two lines:
function d = dist(x0,y0,u,v) % extract dimension n = size(x0,1); % build P, Q matrices P = [u -v]; Q = P' * P; % build projection matrix Pi_u = (u * u') / (u' * u); % compute distance if det(Q) == 0 d = norm((eye(n,n) - Pi_u)*(x0-y0),2); else d = sqrt((x0-y0)'*(eye(n,n) - P*inv(Q)*P')*(x0-y0)); end
__________
Related: