# Finding the shortest distance between two lines

I know how to find the distance between a point and a line, not between two lines.

Find the shortest distance between the lines $(-1,1,4) + t(1,1,-1)$ and $(5,3,-3) + s(-2,0,1)$

Any help would be appreciated.

• Can you use differential calculus? Oct 11, 2012 at 3:20
• No, I cannot, I think I may have figured it out by doing the do product of the vectors t and s with the cross product of s and t, then subtracting them. Incidentally the shortest distance is 0, I believe. Oct 11, 2012 at 3:25

The distance between two lines in $\Bbb R^3$ is equal to the distance between parallel planes that contain these lines.

To find that distance first find the normal vector of those planes - it is the cross product of directional vectors of the given lines. For the normal vector of the form (A, B, C) equations representing the planes are:

$Ax + By + Cz + D_1 = 0$
$Ax + By + Cz + D_2 = 0$

Take coordinates of a point lying on the first line and solve for D1.
Similarly for the second line and D2.

The distance we're looking for is: $$d = \frac{|D_1 - D_2|}{\sqrt{A^2 + B^2 + C^2}}$$

Let $x_1$ and $y_1$ be 2 points on the line 1 and line 2 respectively. Form the difference vector $d=x_1-y_1$. Take another point $x_2$ on the line 1. Form the direction vector $x=x_1-x_2$. Project $d$ on to the direction vector $x$.

\begin{align} x_{parallel}= \frac{(d.x)}{||x||^2}x \end{align}

Now the norm of the following vector (the euclidean distance from the origin), will give you the required minimum distance.

\begin{align} x_{perp}= d-x_{parallel} \end{align}

(if they are not parallel, this will not work, instead it gives the shortest distance between the point $x_1$ and line 2.)

• This method does not give the correct answer because $x_{perp}$ is not guaranteed to be orthogonal to line 2. In fact, there are infinitely many lines that pass through $y_1$ and give a different distance. Counterexample: Consider lines $f: x=(0,0,0)+(1,0,0)t$ and $g: x=(0,0,1)+(0,1,0)t$. Choose $x_1=(2,0,0)$, $x_2=(1,0,0)$, $y_1=(0,1,1)$. Then $d=(2,-1,-1)$, $x=(1,0,0)$, $x_{para}=(2,0,0)$, $x_{perp}=(0,-1,-1)$, which has norm $\sqrt{2}$, but the correct answer is clearly $1$. Jan 20, 2021 at 19:11

The answer is a little tricky, first use the cross product to find $$n$$ by using the two direction vector. $$(d_1 \times d_2)$$ $$\begin{bmatrix} \hat{i} & \hat{j} & \hat{k} \\ 1 & 1 & -1 \\ -2 & 0 & 1 \end{bmatrix}$$ = $$i+j+2k$$.

Then (with points $$P$$ and $$S$$, a point from each line) find vector $$\vec{PS}. = (5,3,-3)-(-1,1,4) = (6,2,-7)$$; then find the projection of $$\vec{PS}$$ onto $$n$$ and find the length of the projection. $$(6,2,-7) \cdot \frac{(1,1,2)}{||1,1,2||^2}= 6^{1/2},$$ or $$2.44949$$

Here is a working translation using SymPy to calculate the distance as suggested by @user287699 (but the answer here agrees with wolframalpha.com):

def separation(l1, l2):
"""return separation of two skew lines

Examples
========

>>> from sympy import Line
>>> a, b, c, d = (0, 0, 0), (1, 2, 3), (1, 1, 1), (2, 3, 5)
>>> separation(Line(a, b), Line(c, d))
sqrt(5)/5
>>> a, b, c, d = (-1, 1, 4), (1, 1, -1), (5, 3, -3), (-2, 0, 1)
>>> separation(Line(a, b), Line(c, d))
sqrt(110)/55
"""
from sympy import Matrix, Point
n = Matrix(l1.direction.unit).cross(Matrix(l2.direction.unit))
ps = l2.p1 - l1.p1
return n.dot(ps)/Point(0,0,0).distance(n)