$
\newcommand{\vo}{\mathbf{o}}
\newcommand{\vv}{\mathbf{v}}
\newcommand{\vs}{\mathbf{s}}
\newcommand{\vp}{\mathbf{p}}
$
In this page I show how to easily compute the minimal distance between two arbitrary lines.
Assume we have two lines, each represented by a point and a normalized
direction vector (first line is $(\vo_1,\vv_1$) and second ($\vo_2,\vv_2)$).
For simplicity, we will use these definitions:
$$
\begin{array}{rclcrclcrcl}
a_1 & = & \vs\cdot\vv_1 & ~~~~~~ & b & = & \vv_1\cdot\vv_2 & ~~~~~~ & \vs & = & \vo_1-\vo_2 \\
a_2 & = & \vs\cdot\vv_2 & ~~~~~~ & c & = & \vs\cdot\vs & ~~~~~~ & & &
\end{array}
$$
A pair of points (one on each line) can be obtained by using two real values
$t_1$ and $t_2$, by substituion on the parametric equations of the lines.
These points are, obviously:
$$
\vp_1(t_1) ~~=~~ \vo_1\,+\,t_1\vv_1 ~~~~~~~~~~~~~~
\vp_2(t_2) ~~=~~ \vo_2\,+\,t_2\vv_2
$$
The squared
distance function $d_{sq}(t_1,t_2)$ is defined as:
$$
d_{sq}(t_1,t_2) ~~=~~ \left(\vp_1(t_1)-\vp_2(t_2)\right)\cdot\left(\vp_1(t_1)-\vp_2(t_2)\right)
$$
Non parallel lines
When the two lines are not parallel, then $b$ is not $1$. Note that $\vv_1$ and $\vv_2$ are normalized, thus in the
case of non-parallel lines, these two vectors are not collinear and their inner prodcut ($b$) cannot be $1$ (it is a value between $-1$ and $1$).
It can be easily shown (by equating its derivate to cero) that, in this case,
function $d_{sq}$ has its (unique) minimun value
for $t_1=s_1$ and $t_2=s_2$, where:
$$
\begin{equation}
s_1~~=~~ \frac{a_2b-a_1}{1-b^2} ~~~~~~~~~~
s_2~~=~~ \frac{a_2-ba_1}{1-b^2}
\end{equation}
$$
The squared minimun distance ($m_{sq}$) between these two lines can be obtained by
computing the pair of closest points ($\vp_1(s_1)$ and $\vp_2(s_2)$) and then
the computing the squared distance between then, that is:
$$
m_{sq} ~~=~~ d_{sq}(s_1,s_2)
$$
by expanding $d$, this distance can also be written directly in terms of $s_1$ and $s_2$, as follows:
$$
m_{sq} ~~=~~ s_1^2 \,+\, s_2^2 \,+\, 2(a_1s_1-a_2s_2-bs_1s_2)
$$
by substituting $s_1$ and $s_2$ above and then simplifying, we arrive
to this expression:
$$
m_{sq} ~~=~~ \frac{a_1^2\,+\,a_2^2\,-\,2ba_1a_2}{b^2-1} \,+\,c
$$
Parallel lines
In the case $b=1$ then the two lines are parallel ($\vv_1=\vv_2$). The squared distance can be computed
simply as the squared length of the $\vs$ component perpendicular to the lines. To obtain this value,
we observe the component of $\vs$ parallel to the lines ($\vs_{||}$), and the component perpendicular to those lines ($\vs_{\perp}$).
These vectors can be expressed as:
$$
\vs_{||} ~=~ (\vs\cdot\vv_1)\,\vv_1 ~~~~~~~~~~~~~~~
\vs_{\perp} ~=~ \vs\,-\,\vs_{||}
$$
If we consider the triangle formed by $\vs$, $\vs_{||}$ and $\vs_{\perp}$, and by using Pythagoras theorem, we get this equality for the squared lengths:
$$
\vs\cdot\vs ~~=~~ (\vs_{||}\cdot\vs_{||}) \,+\, (\vs_{\perp}\cdot\vs_{\perp})
$$
in this expression, we can do these substitutions:
\begin{eqnarray*}
\vs_{\perp}\cdot\vs_{\perp} & = & ~m_{sq} \\
\vs_{||}\cdot\vs_{||} & = & ~(\vs\cdot\vv_1)^2\,(\vv_1\cdot\vv_1) ~~=~~ (\vs\cdot\vv_1)^2 ~~=~~ a_1^2 \\
\vs\cdot\vs & = & ~c \\
\end{eqnarray*}
Thus we can finally get an expression for $m_{sq}$:
$$
m_{sq} ~~=~~ c-a_1^2
$$
See also:
(this page uses
MathJax)