Let l1 and l2 be two lines such that their parametric vector equations are
l1 = a1 + t1*b1
l2 = a2 + t2*b2
where
a1 is any point on l1
a2 is any point on l2
b1 is the unit vector in the direction of l1
b2 is the unit vector in the direction of l2
t1 is the parameter for any point on l1
t2 is the parameter for any point on l2
Let
a = a2 - a1
l = l2 - l1
= a - t1*b1 + t2*b2
then
l² = l⋅l
= (a - t1*b1 + t2*b2)⋅(a - t1*b1 + t2*b2)
= a² + (t1*b1)² + (t2*b2)² - 2 t1(a⋅b1) + 2 t2(a⋅b2) - 2 t1*t2(b1⋅b2)
thus
d(l²)
----- = 2 t1 (b1)² - 2 (a⋅b1) - 2 t2 (b1⋅b2)
dt1
d(l²)
----- = 2 t2 (b2)² + 2 (a⋅b2) - 2 t1 (b1⋅b2)
dt2
Minimise l² by letting
0 = d(l²) = d(l²)
----- -----
dt1 dt2
which gives us two simultaneous linear equations (SLEs) in two unknowns (t1 and t2):
0 = (b1)² t1 + (-b1⋅b2) t2 + (-a⋅b1)
0 = (-b1⋅b2) t1 + (b2)² t2 + (a⋅b2)
To find the closest points of approach solve the SLEs for t1 and t2.