given 2 lines with equations:

$\vec{r_0}+s \vec{n_0} =0$

$\vec{r_1}+ z \vec{n_1} = 0$

the shortest distance between them can be found by 3 methods.

***** method 1 ******

$\hat{n} = \frac{\vec{n_0}\times \vec{n_1}}{|\hat{n}|}$

$d = |(\vec{r_1}-\vec{r_0})\cdot \hat{n}|$

***** method 1 ******

the shortest distance line make right angle with 2 given lines. Thus, bu simple geometry, we have

$\vec{r_0}+s \vec{n_0} + d (\vec{n_0} \times \vec{n_1}) = \vec{r_1}+ z \vec{n_1}$

where $d$ is the distance between 2 lines. there are 2 unknowns : $s, d, z$.  the 3 components of the vectors give 3 equations. Thus, by solving the matrix equation:

$N\cdot (s, z, d) = \vec{r_1}-\vec{r_0}$

Notice that the matrix N is formed by 3 independence vectors when the 2 given lines is not parallel, therefore, it has an inverse and the there is always an unique solution.

***** method 2 ******

another method is minimized the distance between 2 lines. the distance between 2 lines is:

$d= |\vec{r_0} + s \vec{n_0} - \vec{r_1} - z \vec{n_1}|$

by minimize the distance square for variate the parameter $s, z$, i.e.

$\frac{\partial d^2}{\partial s} = (\vec{r_0}-\vec{r_1})\cdot \vec{n_0} + s - z \vec{n_0}\cdot \vec{n_1} = 0$

$\frac{\partial d^2}{\partial z} = (\vec{r_0}-\vec{r_1}) \cdot \vec{n_1} +s \vec{n_0}\cdot \vec{n_1} -z =0$

by solving the matrix equation:

$\left( \begin{array}{cc} -1 & \vec{n_0}\cdot \vec{n_1} \\ \vec{n_0}\cdot \vec{n_1} & 1 \end{array} \right) \cdot \left( \begin{array}{c} s \\ n \end{array} \right)= \left( \begin{array}{c} \left(\vec{r_0}-\vec{r_1}\right)\cdot \vec{n_0} \\ \left(\vec{r_0}-\vec{r_1}\right)\cdot \vec{n_1} \end{array} \right)$

solving and get $s , z$. then get $d$.

*********** remark *****************

it is interesting that, the method 2 separate the problem into 2 parts, while method 1 solve it at once. And reduced a 3 dimensional problem into 2 dimensional.