# How to find shortest distance between two skew lines in 3D?

If given 2 lines $\alpha$ and $\beta$, that are created by

1. 2 points: A and B
2. 2 plane intersection

I want to find shortest distance between them.

$$\left\{\begin{array}{c} P_1=x_1X+y_1Y+z_1Z+C=0 \\ P_2=x_2X+y_2Y+z_2Z+C=0\end{array}\right.$$
$$A=\left(x_3;y_3;z_3\right)$$
$$B=\left(x_4;y_4;z_4\right)$$
$$\alpha =n_1\times n_2=\left(\left|\begin{array}{cc} y_1 & z_1 \\ y_2 & z_2\end{array}\right|;-\left|\begin{array}{cc} x_1 & z_1 \\ x_2 & z_2\end{array}\right|;\left|\begin{array}{cc} x_1 & y_1 \\ x_2 & y_2\end{array}\right|\right)$$
$$\beta =$$

From here I tried:

The question of "shortest distance" is only interesting in the skew case. Let's say $p_0$ and $p_1$ are points on the lines $L_0$ and $L_1$, respectively. Also $d_0$ and $d_1$ are the direction vectors of $L_0$ and $L_1$, respectively. The shortest distance is $(p_0 - p_1)$ * , in which * is dot product, and is the normalized cross product. The point on $L_0$ that is nearest to $L_1$ is $p_0 + d_0(((p_1 - p_0) * k) / (d_0 * k))$, in which $k$ is $d_1 \times d_0 \times d_1$.

I tried, but failed.

-
What do you mean 2 planes intersection? If two planes intersect in $\mathbb{R}^3$ they can only create a line. – Jon Beardsley Dec 10 '10 at 0:23
@JBeardz Sorry, I made this unclear ... It is totally unimportant. I'm looking for a way to find shortest distance of 2 lines in 3D. – Margus Dec 10 '10 at 0:48
The wiki answers page you cite and quote looks to me like a failed copy/paste of something else—the symbols don't make sense to me in the way that they are arranged, suggesting to me that something is missing or out of place. I think the Wikipedia article to which I link in my answer is probably a better resource. – Isaac Dec 10 '10 at 1:12
@Isaac Yes. I'm now struggling to remember how to find $\vec{X_1}$ in case of planes. It would be most helpful, if you could show that. – Margus Dec 10 '10 at 1:58
@Margus: Interestingly, I was trying to remember the same thing when I was thinking about my answer. One way you could try is to pick some arbitrary value of one variable, say $z=0$, and solve the system of the two plane equations with that value to find the corresponding $x$ and $y$ coordinates. – Isaac Dec 10 '10 at 2:05

Per this wikipedia article, if your lines are $\vec{X}=\vec{X_1}+t\vec{D_1}$ and $\vec{X}=\vec{X_2}+t\vec{D_2}$, the distance between them is $$\left|\frac{\vec{D_1}\times\vec{D_2}}{|\vec{D_1}\times\vec{D_2}|}\cdot(\vec{X_1}-\vec{X_2})\right|.$$

-
You will have a problem when $D_1=D_2$ – Grigory Ilizirov Jul 15 '15 at 7:39
If $D_1=D_2$, the lines aren't skew. – Isaac Jul 15 '15 at 14:52

Say you have two lines $\vec L_1 = \vec X_1 + t\vec D_1$ and $\vec L_2 = \vec X_2 + t\vec D_2$.

Start with $(\vec X_1 - \vec X_2)$, which is a skew (non-perpendicular) segment from one line to the other. The distance from $\vec L_1$ to $\vec L_2$ is the component of $(\vec X_1 - \vec X_2)$ that is perpendicular to the lines $\vec L_1$ and $\vec L_2$. We can find this direction by taking the cross product $(\vec L_1 \times \vec L_2)$. After normalizing by dividing by the norm $|\vec L_1 \times \vec L_2|$, take the dot product with $(\vec X_1 - \vec X_2)$ to find the length of the component.

The distance is therefore $$\left|\frac{\vec{D_1}\times\vec{D_2}}{|\vec{D_1}\times\vec{D_2}|}\cdot(\vec{X_1}-\vec{X_2})\right| = \frac{\left|(\vec{D_1}\times\vec{D_2})\cdot(\vec{X_1}-\vec{X_2})\right|}{|\vec{D_1}\times\vec{D_2}|}.$$

-

This is the best (quickest and most intuitive) method I've found so far:

Quick method to find line of shortest distance for skew lines

The idea is to consider the vector linking the two lines ($r, s$) in their generic points and then force the perpendicularity with both lines. (Call the line of shortest distance $t$)

This solution allows us to quickly get three results: The equation of the line of shortest distance between the two skew lines; the intersection point between t and s; the intersection point between t and r.

-