# distance between lines in the space (with calculus)

If I have two lines \eqalign{ & L_1 \left( t \right):p_1 + td_1 \cr & L_2 \left( q \right):p_2 + qd_2 \cr} living in $\mathbb{R}^n$, there exists a classical formula to find the distance between them involving dot and cross products. The question is: can I deduce that formula only using calculus? (In this case, 2 variables) i.e., find the values such that the function $$f\left( {t,q} \right) = \left \| L_1 (t) - L_2(t) \right \| = \left \| p_1 + td_1 - p_2 - qd_2 \right\|$$ reaches its minimum value.

Oh sorry; for simplicity, to have the natural cross product, just take $\mathbb{R}^3$.

-

Consider the distance squared between two points on these lines are $d_2(t_1, t_2) = \vert\vert p_1 - p_2 + t_1 q_1 - t_2 q_2 \vert\vert^2$.

Simple algebra shows, here $(u,v)$ denotes dot product of two vectors:

$$d_2(t_1,t_2) = \vert p_{12} \vert^2 + 2 t_1 (p_{12}, q_1)- 2 t_2 (p_{12},q_2) + t_1^2 \vert q_1 \vert^2 + t_2^2 \vert q_2 \vert^2 - 2 t_1 t_2 (q_1,q_2)$$

You now minimize it by requiring derivatives with respect to $t_1$ and $t_2$ to vanish. This yields

$$t_1 = \frac{ (p_{12},q_1) \vert q_2\vert^2 - (p_{12},q_2) (q_1, q_2) }{(q_1, q_2)^2 - \vert q_1 \vert^2 \vert q_2 \vert^2} \; \;\; \text{and} \;\;\; t_2 = - \frac{ (p_{12},q_2) \vert q_1\vert^2 - (p_{12},q_1) (q_1, q_2) }{(q_1, q_2)^2 - \vert q_1 \vert^2 \vert q_2 \vert^2}$$

Upon substitution I obtain the minimal distance squared is

$$\vert p_{12} \vert^2 + \frac{ \vert q_1\vert^2 (p_{12},q_2)^2 + \vert q_2\vert^2 (p_{12},q_1)^2 - 2 (q_1,q_2)(p_{12},q_1)(p_{12},q_2) }{ (q_1, q_2)^2 - \vert q_1 \vert^2 \vert q_2 \vert^2}$$

where $p_{12}=p_1-p_2$.

-
What did you do? I did not understand you , sorry. – Daniel Aug 9 '11 at 20:11
@Daniel I constructed the function of distance squared between two points on two curve, i.e. point $p_1 + t_1 q_1$ and point $p_2 + t_2 q_2$. This gives bivariate polynomial in $t_1$ and $t_2$ with coefficients expressible as scalar products of appropriate vectors. Then I solved for minimal parameters, obtained from $\frac{d}{d{t_1}} d_2(t_1,t_2) = 0$ and $\frac{d}{d{t_2}} d_2(t_1,t_2) = 0$ and substituted them back. – Sasha Aug 9 '11 at 20:14
@Daniel I have updated my response filling in steps. Hope this helps. – Sasha Aug 9 '11 at 21:00
I assume that $(u,v)$ denotes the scalar (dot) product $u\cdot v$. – Grumpy Parsnip Aug 9 '11 at 21:11
By $d_2$, I assume you mean $d^2$. – robjohn Aug 17 '11 at 10:37

In the past, I have solved these types of problems without calculus just by requiring the minimizing line to be orthogonal to whatever it was supposed to minimize the distance between.

Since I am lazy, I will rewrite the equations as $L_1 ( p):a + p b$ and $L_2 ( q):c + q d$. We want to find values of $p$ and $q$ such that, if $P = L_1(p)$ and $Q = L_2(q)$, then $P-Q$ is orthogonal to both $b$ and $d$.

Using "$.$" for dot product (I don't see where cross product is needed), this becomes $$0 = ( a + p b - c - q d).b = ( a + p b - c - q d).d$$ or, letting $g = c-a$, \eqalign{p |b|^2 - q(d.b) = g.b \cr p(b.d)-q |d|^2 = g.d\cr}

Solving these for $p$ and $q$ and substituting back in $P$ and $Q$ should give the points (modulo and errors on my part). Note that, if $b$ and $d$ are not proportional, Cauchy-Schwarz shows this equation has a unique solution.

-
"The Schwarz of inequality / And lemma too, he has no T. // The "Distribution" Schwartz you see / Is French, and so he has a T. -- Ralph Boas, Spelling Lesson. :D – J. M. Aug 17 '11 at 4:27
@J.M. that reminds me... :) – t.b. Aug 21 '11 at 9:38
...apparently you went on a Schwarz correction spree @Theo... :D – J. M. Aug 21 '11 at 10:53
@J.M. Yes I did :) (I only see your comment now that I'm back at it. Note that the system only removes one punctuation character after the notification, so a trailing ellipsis undermines notification.) Is it sooo hard to spell names correctly? I'll never understand how mathematicians can see $\ddot{u} = u$ as a mildly interesting equation (as opposed to $u = u$) while they fail to see the difference between Holder and Hölder, Schwarz and Schwartz, etc. – t.b. Aug 26 '11 at 23:33
@Theo: I could understand the "Holder" bit at least; not everybody a.) has an international keyboard; b.) can remember the ASCII code for those fancy letters; or c.) has the patience to bring up a character map application or search for that stuff in his/her favorite search engine. Oh well. We'll never run out of work... ;) – J. M. Aug 28 '11 at 20:29