This question already has an answer here:

The answer for the shortest distance between two skew lines has already been answered here and is also found on Wikipedia.

However, I need to apply a further constraint. I need to find the shortest distance between two skew line segments. Or, if it is easier, two rays, whose starting points are $P_1$ and $P_2$. How can I modify the equations for $c_1$ and $c_2$ in the wiki link to account for this? Or is this a far more complicated problem?


marked as duplicate by hardmath, Namaste, Jose Arnaldo Bebita-Dris, JMP, Xander Henderson Jun 8 '18 at 2:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ I added an answer to this to the question this is marked a duplicate of, with a step by step procedure on how to find the closest points. It is trivial to modify the procedure to apply to rays, instead. $\endgroup$ – Nominal Animal Jun 9 '18 at 10:44


$$ s_1(\lambda)=\lambda p_1+(1-\lambda)p_2\\ s_2(\mu) = \mu p_3+(1-\mu)p_4 $$

with $0\le \lambda\le 1$ and $0 \le \mu\le 1$ and with the help of four slack variables $\epsilon_1,\epsilon_2,\epsilon_3,\epsilon_4$ to handle the inequality restrictions, the problem can be stated using the Lagrange Multipliers as follows

$$ L(\lambda, \mu, \eta,\epsilon) = \frac{1}{2}||s_1(\lambda)-s_2(\mu)||^2+\eta_1 (\lambda-\epsilon_1^2) +\eta_2(\mu-\epsilon_2^2)+\eta_3(\lambda-1+\epsilon_3^2)+\eta_4(\mu-1+\epsilon_4^2) $$

with the stationary conditions

$$ \nabla L = 0 $$


$$ \left\{ \begin{array}{rcl} (p_2+\lambda(p_1-p_2))(p_1-p_2)+\eta_1 + \eta_3 & = & 0\\ (p_4+\mu(p_3-p_4))(p_3-p_4)+\eta_2 + \eta_4 & = & 0\\ \lambda-\epsilon_1^2 & = & 0\\ \mu-\epsilon_2^2 & = & 0\\ \lambda-1+\epsilon_3^2 & = & 0\\ \mu-1+\epsilon_4^2 & = & 0\\ \eta_1\epsilon_1 & = & 0\\ \eta_2\epsilon_2 & = & 0\\ \eta_3\epsilon_3 & = & 0\\ \eta_4\epsilon_4 & = & 0 \end{array} \right. $$

Solving those conditions we get the possible stationary points.

  • $\begingroup$ Very nice. Thanks! $\endgroup$ – HiddenBabel Jun 8 '18 at 0:41
  • $\begingroup$ I found this very interesting, especially as it looks so clean, but as far as I can see, this system of equations has lots of solutions (Maple says 25 or so). Are you sure the first two equations in the system are correct? Anyway, in the procedural method, the solution is either directly obtained, or chosen between four possible point pairs. To me, it looks like the procedural method yields the answer with less computation/effort. $\endgroup$ – Nominal Animal Jun 9 '18 at 11:07
  • $\begingroup$ Yes. There are "duplicated" solutions due to the squared slack variables $\epsilon_i$. Those solutions contemplate all possible extremal situations. The qualification is made analyzing the values for $\epsilon_i,\eta_i$ so $\epsilon_i = 0 $ indicates that the restriction is actuating etc. $\endgroup$ – Cesareo Jun 9 '18 at 11:23
  • $\begingroup$ To me it gives with MATEMATICA a set of 9 possible extrema. $\endgroup$ – Cesareo Jun 9 '18 at 11:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.