# Distance between two lines by orthogonal projection

I've got the lines' points and vectors $p,q$. My idea was to find a subspace (plane) with the basis of $p,q$ - perpendicular to the lines' axis. Then find the intersecting point $P$ of the lines' projections onto that plane. Then project $P$ onto both lines, get two points $a,b$ and calculate their distance. The problem is that the lines don't go through the origin, which means they're not subspaces and I can't project onto them. Any thoughts?

First I find the orthogonal complement to the line vectors. Since the lines are skew and we're in $\mathbb{R}^3$, I get the lines' axis. Now I just orthogonally project difference vector of any points on the lines onto the axis. The projected vector's norm is the answer. Any formal explanation why this works appreciated.

The lines are affine subspaces, let $P+\lambda p$ and $Q+\mu q$.

The direction of the common perpendicular is $r=p\times q$. If you look in any direction perpendicular to $r$, you will see the projections of the two lines as two parallels and their distance is just the orthogonal projection of $PQ$ onto $r$,

$$d=\frac{PQ\cdot(p\times q)}{\|p\times q\|}.$$

You can obtain the same result by minimizing the squared distance vector,

$$d^2=(PQ+\lambda p-\mu q)^2.$$

To minimize, you cancel the derivatives wrt to the parameters,

$$\frac{\partial d^2}{\partial\lambda}=(PQ+\lambda p-\mu q)p=PQ\cdot p+\lambda p^2-\mu p\cdot q=0,\\ \frac{\partial d^2}{\partial\mu}=(PQ+\lambda p-\mu q)q=PQ\cdot q+\lambda p\cdot q-\mu p^2=0$$ and solve for $\lambda,\mu$.

The determinant of this system is $p^2q^2-(p\cdot q)^2$ which you should recognize to be $(p\times q)^2$.

Using the definition of a norm, the distance between two vectors $d(A,B)=||A-B||$.

• i need to find these vectors first... – k5f Mar 8 '14 at 17:33

We are in the affine euclidean space $\mathbb{R}^n$ and we consider two non-parallel lines $D_1=\{P+\lambda u|\lambda\in \mathbb{R}\}$ and $D_2=\{Q+\mu v|\mu\in \mathbb{R}\}$. Then $d(D_1,D_2)=RS$ where $R=P+\lambda u\in D_1,S=Q+\mu v\in D_2$ and $\vec{SR}=P-Q+\lambda u-\mu v$ is perpendicular to $D_1,D_2$.

We obtain the system in the unknowns $\lambda,\mu$: $(P-Q).u+\lambda||u||^2-\mu v.u=0,(P-Q).v+\lambda u.v-\mu ||v||^2=0$. The determinant is $-||u||^2||v||^2+(u.v)^2<0$ and there is a unique solution. Then we obtain $R,S,d(D_1,D_2)$.

If you have already figured out how to orthogonally project each of the lines onto your chosen subspace, then you can take one of the lines (call it $L$) and a point on that line, and take any point $Q$ on $L.$ Project $Q$ onto the subspace; that gives you a projected point $Q',$ and the vector $v = Q - Q'$ is orthogonal to the subspace. Then $P' = P + v$ is the orthogonal projection of $P$ onto $L.$

Do that once for each line, and then you have your points $a$ and $b.$

On the other hand, the first part of Yves Daoust's answer is equivalent to this (after some algebraic simplification) and constructs a lot fewer intermediate objects on the way to its result, so that's what I'd do.