Find shortest distance between lines in 3D Find shortest distance between lines given by
$$\frac{x-2}{3}=\frac{y-6}{4}=\frac{z+9}{-4}$$
and
$$\frac{x+1}{2}=\frac{y+2}{-6}=\frac{z-3}{1}$$
Is there any shortcut method for this problems?
 A: Here is an approach
that uses dot products
instead of cross products.
This works in any number of dimensions,
not just 3.
The skew lines are
$L = a+bt, M=c+ds$.
The distance 
between two points
on $L$ and $M$ is
$D
=(a+bt-c-ds)^2
=(e+bt-ds)^2
$
where
$e = a-c$.
For this to be a minimum,
taking partials,
we want
$D_s = D_t
= 0$.
$D_s 
= -2d(e+bt-ds)
$
and
$D_t 
= 2b(e+bt-ds)
$.
Therefore,
with multiplication of vectors
being dot product,
$0
=d(e+bt-ds)
=de+dbt-d^2s
$
and
$0
=b(e+bt-ds)
=be+b^2t-bds)
$.
These are two equations
in the two unknowns
$s$ and $t$:
$\begin{array}\\
de
&= d^2s-dbt\\
be
&= bds-b^2t\\
\end{array}
$
The determinant is
$A
=-b^2d^2+(bd)^2
=-(b^2d^2-(bd)^2)
$.
By Cauchy-Schwarz,
this is non-zero 
unless $b$ and $d$
are parallel
(which is a good thing).
The solutions
(by Cramer's rule)
 are
$s
=\dfrac{-(b^2)(de)+(be)(db)}{A}
$
and
$t
=\dfrac{(d^2)(be)-(be)(db)}{A}
$.
Putting these into
$L = a+bt,
M = c+ds,
D
=(e+bt-ds)^2
$
we get the endpoints
of the closest line
and the distance.
A: If anyone is interested on a implementation of the algorithm proposed by @John Alexiou using python there it is:
def distance_from_two_lines(e1, e2, r1, r2):
    # e1, e2 = Direction vector
    # r1, r2 = Point where the line passes through

    # Find the unit vector perpendicular to both lines
    n = np.cross(e1, e2)
    n /= np.linalg.norm(n)

    # Calculate distance
    d = np.dot(n, r1 - r2)

    return d

A: Hint:
write the equations of the two lines in the form $\vec x=\vec p+t\vec q$:
$$
r_1) \qquad \begin{pmatrix}
x\\y\\z
\end{pmatrix}=\begin{pmatrix}
2\\6\\-9
\end{pmatrix}+t\begin{pmatrix}
3\\4\\-4
\end{pmatrix}
$$
$$
r_2) \qquad \begin{pmatrix}
x\\y\\z
\end{pmatrix}=\begin{pmatrix}
-1\\-2\\3
\end{pmatrix}+t\begin{pmatrix}
2\\-6\\1
\end{pmatrix}
$$
than, noted the the two lines are not parallel nor intersecting,  use the formula from here.
A: I'm using a method similar to marty cohen's but without matrix/linear system
We first parametrize the two lines as:
$$
\vec{L_0} = \vec{p_0}+s\vec{u}\\
\vec{L_1} = \vec{p_1}+t\vec{v}
$$
The distance between any points on line 1 to line 0 is
$$
d^2 = \dfrac{\left[(\vec{p_1}-\vec{p_0}+t\vec{v})\times\vec{u}\right]^2}{\vec{u}\cdot \vec{u}}
$$
To find the minimum distance, we need to minimize the numerator
$$
\dfrac{d\left[(\vec{p_1}-\vec{p_0})\times \vec{u}+t\vec{v}\times\vec{u}\right]^2}{dt}=\dfrac{d\left[2(\vec{p_1}-\vec{p_0})\times\vec{u}\cdot(t\vec{v}\times\vec{u})+t^2(\vec{v}\times\vec{u})^2\right]}{dt}\\
=(\vec{p_1}-\vec{p_0})\times\vec{u}\cdot(\vec{v}\times\vec{u})+t(\vec{v}\times\vec{u})^2=0
$$
Then we can get the solution of t
$$
t = -\dfrac{(\vec{p_1}-\vec{p_0})\times\vec{u}\cdot(\vec{v}\times\vec{u})}{(\vec{v}\times\vec{u})^2}
$$
Taking the value of $t$ back to the distance expression,
$$
d^2=\dfrac{\left[(\vec{p_1}-\vec{p_0})\times\vec{u}\right]^2(\vec{v}\times\vec{u})^2-\left[((\vec{p_1}-\vec{p_0})\times\vec{u})\cdot(\vec{v}\times\vec{u})\right]^2}{\vec{u}\cdot \vec{u}(\vec{v}\times\vec{u})^2}\\
= \dfrac{\left[((\vec{p_1}-\vec{p_0})\times\vec{u})\times(\vec{v}\times\vec{u})\right]^2}{\vec{u}\cdot \vec{u}(\vec{v}\times\vec{u}^2}\\
= \dfrac{\left[\vec{u}\cdot((\vec{p_1}-\vec{p_0})\times\vec{v})\vec{u}\right]^2}{\vec{u}\cdot \vec{u}(\vec{v}\times\vec{u})^2}\\
= \dfrac{\left[\vec{u}\cdot((\vec{p_1}-\vec{p_0})\times\vec{v})\right]^2}{(\vec{v}\times\vec{u})^2}\\
= \dfrac{\left[(\vec{p_1}-\vec{p_0})\cdot(\vec{v}\times\vec{u})\right]^2}{(\vec{v}\times\vec{u})^2}
$$
The final solution
$$
d=\dfrac{\left|(\vec{p_1}-\vec{p_0})\cdot(\vec{v}\times\vec{u})\right|}{|\vec{v}\times\vec{u}|}
$$
By simply switching the index, we can also get the solution of s
$$
s = -\dfrac{(\vec{p_1}-\vec{p_0})\times\vec{v}\cdot(\vec{v}\times\vec{u})}{(\vec{v}\times\vec{u})^2}\\
$$
Then we can also get the direction of the line connecting the two point:
$$
\vec{n}=\vec{p_0}+s\vec{u}-\vec{p_1}-t\vec{v}
$$
A: This is a generalization of the code of @Leonardo Mariga to handle half-lines that start at r and go to infinity in the direction of e
def distance_from_two_lines(e1, e2, r1, r2):
    # e1, e2 = Direction vector
    # r1, r2 = Point where the line passes through

    # Find the unit vector perpendicular to both lines
    n = np.cross(e1, e2)

    # Calculate distance
    d = np.dot(n/ np.linalg.norm(n), r1 - r2)
    #print(np.dot(e1, e2))
    
    t1 = np.dot(np.cross(e2, n), (r2 - r1) ) / np.dot(n, n)
    t2 = np.dot(np.cross(e1, n), (r2 - r1) ) / np.dot(n, n)
            
    #print(t1, t2)

    return d if t1 >= 0 and t2 >= 0 else np.linalg.norm(r1 - r2)

I just check that both t1 and t2 are positive. Keep in mind that if the half-lines go in "opposite" directions, the distance is just the distance of the origins.
