I recently butchered an explanation of the equation for the shortest distance between two lines.
As penance, I've hacked together a bunch of terrible Javascript to create an interactive tutorial that will hopefully explain it a little better!
Let's imagine we have two lines, defined by four points.
Line \(\vec{AB}\) is defined by points \(A\) and \(B\), and line \(\vec{CD}\) is defined by points \(C\) and \(D\).
Our task is to find the shortest distance between \(\vec{AB}\) and \(\vec{CD}\).
For notational consistency with the notes for this lecture course, we're gonna need to define a bunch of stuff[1]
.
We will define \(\mathbf{u} = B - A\) as a vector perpendicular to \(\vec{AB}\), and \(\mathbf{v} = D - C\) as a vector perpendicular to \(\vec{CD}\).
We'll also define \(\mathbf{a}\) to be any point on the line \(\vec{AB}\), and \(\mathbf{c}\) to be any point on the line \(\vec{CD}\).
In a small break from the lecture notes I will define \(d\) to be the shortest distance between the two lines.
What you will potentially not recognise, however, is where on earth this equation comes from...
I'm hoping that by the end of this post you'll have just a little bit more intuition about why this works[2]
.
I'll start by presenting, without justification, a "recipe" for this formula.
Then I'll try to justify why each of the steps is true, using interactive (and colourful) graphs.
And finally I'll go over worked example to show how you would solve a question using this equation.
The first thing we need for this formula is a vector perpendicular to both lines.
Remembering that \(\mathbf{u}\) and \(\mathbf{v}\) are parallel to the two lines, we can obtain a perpendicular vector with \(\mathbf{u} \times \mathbf{v}\).
This vector will be parallel to the line of shortest distance between \(\vec{AB}\) and \(\vec{CD}\).
Because \(\mathbf{u}\) and \(\mathbf{v}\) could be any length, we need to normalise them and obtain a unit vector (which I will call \(\hat{\mathbf{w}}\)) perpendicular to the lines
This is just a scaling factor, so this unit vector will still be parallel to the line of shortest distance.
We then need to set up a right-angled triangle with the line of shortest distance as one side, and a known vector as the hypotenuse.
We can ensure this is a right-angled triangle by choosing any vector between the two lines.
We therefore pick a point \(\mathbf{a}\) from \(\vec{AB}\) and a point \(\mathbf{c}\) from \(\vec{CD}\), making the vector \(\mathbf{c} - \mathbf{a}\) a vector between the two lines.
From trigonometry and the right-angled triangle we constructed above we know that
\[
d = \left|\mathbf{c} - \mathbf{a}\right| \cos \theta \,.
\]
However, from the definition of the dot product (bearing in mind that \(\left|\hat{\mathbf{w}}\right| = 1\)) we also know that
Ok, so let's try to see where these steps actually come from and why they work.
First we need to decide on some lines.
I've chosen the default values for \(A\), \(B\), \(C\), and \(D\) for a reason (they are slight adaptations from a particular problem in the lecture course), but you can set your own points using the boxes below (you might need to click out of the box for updates to register).
⚠ Warning!
If you go too crazy with the values of these points some of the assumptions I've made about how I should plot these lines break down.
Technically all of the lines that don't stop at a point should extend off to infinity, but I can't plot infinitely long lines.
If any graph looks weird (e.g. you think two lines should coincide but they don't) this is probably why.
So let's take a look at these lines.
I've plotted on this diagram the lines \(\vec{AB}\) and \(\vec{CD}\) themselves, as well as the shortest line between them.
Take some time to convince yourself that this shortest line is perpendicular to both \(\vec{AB}\) and \(\vec{CD}\), and in fact that it must be.
The direction (though not the length) of this shortest line will be the same as the direction of \(\hat{\mathbf{w}}\).
I've also plotted a fourth line.
This is a line which is parallel to the line \(\vec{AB}\) but passes through the line \(\vec{CD}\).
It will be very important later to understand that if you take that shortest line and slide it along \(\vec{AB}\), it will always remain touching the parallel line.
That is to say, the shortest distance from any point \(\vec{AB}\) to the parallel line is the same as the shortest distance from \(\vec{AB}\) to \(\vec{CD}\).
Then we need to choose \(\mathbf{a}\) and \(\mathbf{c}\).
In reality we would pick either \(A\) or \(B\) to be \(\mathbf{a}\), and \(C\) or \(D\) to be \(\mathbf{c}\).
However to really hammer home that they can be any point on those lines, I will allow you to choose any[4]
.
⚙ Note
To ensure that the points you choose are in fact on the appropriate lines, I've made it so that you choose the \(\lambda\) value from the equation of the line
If that makes no sense to you, ignore the \(\lambda\) value and just pay attention to the coordinate listed next to it.
a = (
,
,
)
c = (
,
,
)
On this second figure, I've done two things.
Firstly, I've plotted the points \(\mathbf{a}\) and \(\mathbf{c}\), and drawn a line between them.
This line is the vector \(\left(\mathbf{c} - \mathbf{a}\right)\) and will form the hypotenuse of the right-angled triangle we will use.
Secondly, I've taken the shortest line between \(\vec{AB}\) and \(\vec{CD}\) and slid it along \(\vec{AB}\) until it is at point \(\mathbf{a}\).
I've named the point where this shortest line meets the parallel line \(\mathbf{e}\).
Hopefully by playing around with this figure you can convince yourself that the vectors \(\left(\mathbf{c} - \mathbf{a}\right)\) and \(\left(\mathbf{e} - \mathbf{a}\right)\) form a right-angled triangle.
Once you've convinced yourself of this, take a look at the next figure, where I've plotted out this right-angled triangle.
Now, hopefully, the connection is starting to make more sense.
The length of the shortest line (\(d\)) is the same, even though the line is in a different place, and the arbitrary vector between the lines really doesn't matter, as long as it forms a right-angled triangle.
From our right-angled triangle we can apply some trigonometry to find that
\[
d = \left|\mathbf{c} - \mathbf{a}\right| \cos \theta \,.
\]
But remember, \(\hat{\mathbf{w}}\) is a unit vector parallel to the shortest line, and so
I know I'm repeating myself a lot here, but there's a lot of details to keep in your head here and I want to make sure you remember them.
It's an equation that doesn't follow neatly from its setup, and we need to go through several transformations before we can arrive at it.
But we can get there even if we need to go back and forth a few times.
Fortunately, we don't need to understand where this equation comes from in order to be able to use it!
And to that end, I will now take you through a worked example to show how you would actually solve the problem considering that we already know the equation.
I'm gonna use a set of static points for this example (i.e. you can't pick and choose your points for this), largely because I don't want to have to do more cursed things with Javascript...
We'll use
\[
A = \begin{pmatrix}-16 \\ 6 \\ 4\end{pmatrix} \qquad B = \begin{pmatrix} -1 \\ 2 \\ -3 \end{pmatrix}
\]
for the line \(\vec{AB}\), and
\[
C = \begin{pmatrix}1 \\ -1 \\ 3\end{pmatrix} \qquad D = \begin{pmatrix} 4 \\ 9 \\ 7 \end{pmatrix}
\]
for the line \(\vec{CD}\).
The first thing we need to do is find \(\mathbf{u}\)
⤶
I'm sorry, ok? I tried. Like, I really tried to not start this whole post with two separate paragraphs of definitions but ... I just ... I just can't, ok?
⤶
I want to stress that this won't be a rigorous argument, just an intuitive one.
⤶
The easiest way to see this is to imagine deviating from the perpendicular line slightly. You will form a right-angled triangle with the perpendicular line, and so that deviated line must be longer.