# How can I find the location on the Z axis where two skew lines pass closest to each other on the XY plane?

I'm given one point $\overrightarrow{P}$ and the slopes $\frac{dX}{dZ}$ and $\frac{dY}{dZ}$ for each of my two lines. I'm trying to figure out where on the Z axis my lines pass closest to each other.

I would think that the parameterization for each line would be $\overrightarrow{L_1} = \overrightarrow{P_1} + s(\frac{dX}{dZ}, \frac{dY}{dZ}, 1)$, where the 1 comes from $\frac{dZ}{dZ}$

I've tried doing this with an algorithm that populates points along the lines by incrementing $s$, and then compares the distance between the points on the $XY$ plane. I determine whether or not the points are coplanar on the $XY$ plane by checking if the $Z$ values for each point on each line are nearly equal.

Unfortunately the length of time this takes is exponentially related to how many points I choose to populate the lines with, so I'm looking for a way to directly calculate the location on the Z axis where the lines pass closest to each other.

I've looked over some other stack exchange questions, but so far all I've been able to find are methods that tell me the minimum distance between the lines, or the locations of the points that are closest to each other.

Is there a way to directly calculate the $Z$ value where the distance between the lines is the smallest in the same $XY$ plane?

I can post my iterative code if it would be helpful.

• Let me try to reword the problem a bit. You are given two lines and want to find such $z$ value, that points $Q(x_1, y_1, z)$ on one line and $R(x_2, y_2, z)$ on the other one, both at the same $z$, make the line segment $\overline {QR}$ the shortext possible. Is that right? – CiaPan Jan 29 '16 at 6:06
• Correct, that's what I'm trying to do. Thank you for the succinct rewording. – jalconvolvon Jan 29 '16 at 6:20
• I've found an error in my answer (there were subtractions instead of additions), please see the corrected version. – CiaPan Jan 29 '16 at 7:28

For lines parameterization: \begin{align} Q &= P_1 + s[a_1, b_1, 1] \\ R &= P_2 + t[a_2, b_2, 1] \end{align} where $a$ and $b$ are your given slopes, the $z$ coordinate of both points is, respectively: \begin{align} z_Q &= z_{P_1} + s \\ z_R &= z_{P_2} + t \end{align} so to find points $Q$ and $R$ at a given $z$ we need \begin{align} s &= z - z_{P_1} \\ t &= z - z_{P_2} \end{align} Then \begin{align} Q &= P_1 + (z - z_{P_1})[a_1, b_1, 1] \\ R &= P_2 + (z - z_{P_2})[a_2, b_2, 1] \end{align} which is \begin{align} Q &= [x_{P_1} + (z - z_{P_1})a_1, y_{P_1} + (z - z_{P_1})b_1, z] \\ R &= [x_{P_2} + (z - z_{P_2})a_2, y_{P_2} + (z - z_{P_2})b_2, z] \end{align} and their distance, which you need to minimize, is $$d(Q,R) = \sqrt{(x_Q-x_R)^2+(y_Q-y_R)^2}$$
You can minimize its square to make calculations easier: $$(d(Q,R))^2 = (x_Q-x_R)^2+(y_Q-y_R)^2$$ If you substitute those four expressions and reduce them carefully you'll see it is a square function of $z$. Finding its minimum shouldn't be a big problem.