# Solving shortest distance between two lines in vector form using calculus

When I have two parallel lines in vector form, e.g. $$r_1=(1+5t)i+(2+4t)j+(-1+3t)k$$ $$r_2=(3+5s)i+(4s)j+(1+3s)k$$ I can solve for $$r_1$$ to $$r_2$$ by substituting the general equation of a point on one to find the vector from one line to the other, then substitute $$u=s-t$$ as they have the same direction vector to get, $$r_2-r_1=(1+5u)i +(-2+4u)i+(2+3u)k$$ Then by using absolute value, with $$d$$ being the distance between $$d^2=50t^2+6t+9$$ Then differentiate with respect to t to find minimum value of d, $$100t+6=0$$ $$(50(\frac{-3}{50})^2+6(\frac{-3}{50})+9)^{\frac{1}{2}}=\frac{21(2)^{\frac{1}{2}}}{10}$$ However, when the substitution of u is not possible(any time when vectors are not parallel), this method does not work as there are two variables, s and t. Is this just a special case where the method is possible, or is the a general way of applying this method?

• Its tough doing with calculus. Easier method is taking absolute of value dot product of a vector joining any 2 points on these lines(one from line 1 and one from line 2) with unit vector along $\vec x$$\times$$\vec y$(where $\vec x$ is parallel vector to line 1 and $\vec y$ is parallel vector to line 2). Jan 4, 2020 at 15:59
• You don't need calculus if the lines are not parallel. See this answer. Jan 4, 2020 at 17:09

In your example, the two lines are parallel, so the distance $$d$$ only depends on a single variable $$u= t-s$$.

So, there are two cases:

1. If the two lines are parallel, then the distance only depends on a single variable, so you can just fix an abitrary point on a line and let the point on the other line change and you will find the point and the extreme distance by differentiating.

2. If the two lines are not parallel, then you can not fix one point at beginning, in this case you have to use two variables $$t,s$$ together to express the distance. And in general this distance function can not be reduced to a single-variable function, if you want to get the extreme value by differentiating, you should use partial derivatives.

By the way, in your solution, the next step is to fix a abitrary point on one line and solve for the other. There are infinite solutions of point pairs.

• You don't need calculus if the lines are not parallel. See this answer. Jan 4, 2020 at 17:10

Here's something I worked out for the closest points on two lines in any number of dimensions.

Closest points on two lines (intersection in 2d)

Lines through $$A, B$$ and $$C, D$$.

Lines are $$(1-t)A+tB =A+t(B-A) =A+tE$$ and $$(1-u)C+uD =C+u(D-C) =C+uF$$ where ($$G$$ used below) $$E = B-A, F = D-C, G = A-C$$.

Distance squared is

$$\begin{array}\\ P &=|(A+tE)-(C+uF)|^2\\ &=|A-C+tE-uF|^2\\ &=|G+tE-uF|^2\\ &=(G+tE-uF)\cdot(G+tE-uF)\\ &=G G+t^2E E+u^2F F +2tGE-2uGF-2tuEF\\ \dfrac{\partial P}{\partial t} &=2tE E +2GE-2uEF\\ &= 0\\ \dfrac{\partial P}{\partial u} &=2uF F -2GF-2tEF\\ &= 0\\ \implies\\ GE &=-tE E+uEF\\ GF &=-tEF+uF F\\ d &=\begin{vmatrix} -EE & EF\\ -EF & FF\\ \end{vmatrix}\\ &=-EE\ FF+(EF)^2\\ d_t &=\begin{vmatrix} GE & EF\\ GF & FF\\ \end{vmatrix}\\ &= GE\ FF - GF\ EF\\ d_u &=\begin{vmatrix} -EE & GE\\ -EF & GF\\ \end{vmatrix}\\ &=-EE\ GF + EF\ GE\\ t &= \dfrac{d_t}{d}\\ u &= \dfrac{d_u}{d}\\ P &=G G+t^2E E+u^2F F +2tGE-2uGF-2tuEF\\ &=G G+\dfrac{d_t^2}{d^2}E E+\dfrac{d_u^2}{d^2}F F +2\dfrac{d_t}{d}GE-2\dfrac{d_u}{d}GF-2\dfrac{d_td_u}{d^2}EF\\ \end{array}$$