Mutually closest point of two lines defined by vectors.

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.

• A line is not defined by a normal vector. Do you mean parallel/tangent vector? – Paul Jan 4 '17 at 16:20
• Tangent vector seems legit. – LukeN Jan 4 '17 at 16:21
• Google "the perpendicular distance between 2 lines" maybe. – Paul Jan 4 '17 at 16:46
• Searching for “distance between two lines” gives several explicit solutions among the top few hits. There are also answers to this question already on this site, as you can see by perusing the related questions on the right. – amd Jan 4 '17 at 19:04

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}.$$