[Math] Mutually closest point of two lines defined by vectors.

analytic geometrygeometrylinear algebra

I have two 3D lines defined by tangent vectors $A$ and $B$. I need to find a point that lies on the line defined by $A$. The point has to be the closest to $B$ out of all points that lay on $A$. Assume that the lines are not parallel.

I feel it's easy and solved somewhere, but I probably google it wrong.

Best Answer

Suppose the two lines are given by $At+a$ and $Bs+b$, where $a$ is a fixed point on one, $b$ on the other. Then the vector between a point on each line is $$ At-Bs+(a-b). \tag{1}$$ Hence the problem is mathematically the same as finding the shortest distance from the plane given by $ At-Bs $ to the point $a-b$.

The shortest distance is when the vector in $(1)$ is perpendicular to $A$ and $B$, i.e. a multiple of $A \times B$, which is nonzero since $A \not\parallel B$ and $A,B \neq 0$. Hence we have $$ At^*-Bs^*+(a-b) = \nu (A \times B), $$ for the optimal values $t^*,s^*$.

We can now get straight to $\nu$ by dotting with $A \times B$: $$ (a-b) \cdot (A \times B) = \nu\lVert A \times B \rVert^2. $$ The length of the vector (and hence the distance between the lines) is then $$ \lVert \nu (A \times B) \rVert = \lvert \nu \rvert \lVert A \times B \rVert = \frac{\lvert (a-b) \cdot (A \times B) \rvert}{\lVert A \times B \rVert^2} \lVert A \times B \rVert = \frac{\lvert (a-b) \cdot (A \times B) \rvert}{\lVert A \times B \rVert}. $$