Subject 5.18: How can the distance between two lines in space be computed?
The following is robust C code from Seth Teller that computes the “points of closest approach” on two 3D lines. It also classifies the lines as parallel, intersecting, or (the generic case) skew. // computes pB ON line B closest to line A // computes pA ON line A closest to line B // return: 0 if parallel; 1 if coincident; 2 if generic (i.e., skew) int line_line_closest_points3d ( POINT *pA, POINT *pB, // computed points const POINT *a, const VECTOR *adir, // line A, point-normal form const POINT *b, const VECTOR *bdir ) // line B, point-normal form { static VECTOR Cdir, *cdir = &Cdir; static PLANE Ac, *ac = &Ac, Bc, *bc = &Bc; // connecting line is perpendicular to both vcross ( cdir, adir, bdir ); // check for near-parallel lines if ( !vnorm( cdir ) ) { *pA = *a; // all points are closest *pB = *b; return 0; // degenerate: lines parallel } // form plane containing line A, parallel to cdir plane_from_two_vectors_and_point ( ac, cdir, adir, a ); // form plane containing line B, paralle