Two lines in 3D may be skew , neither parallel nor intersecting. The shortest distance between them is the length of the common perpendicular: the unique line segment that meets both lines at right angles.
The skew-line formula
Let line 1 be r=a1+td1 and line 2 be r=a2+sd2, with d1∥d2.
A vector perpendicular to both directions is d1×d2. Its unit version is n^=∣d1×d2∣d1×d2.
The shortest distance is the absolute value of the projection of the displacement a2−a1 onto n^:
D=∣d1×d2∣(a2−a1)⋅(d1×d2).
The numerator is the signed triple product; the absolute value gives the geometric distance.
Why does this work?
The common perpendicular has direction d1×d2. To find how "far apart" the lines are along this perpendicular direction, project the line-to-line displacement a2−a1 onto n^. The result is the gap.
Parallel lines
If d1∥d2, the formula above breaks down (the cross product is zero). For two parallel lines, every perpendicular to one is perpendicular to the other; the distance is just the perpendicular distance from any point of line 2 to line 1:
D=∣d1∣∣(a2−a1)×d1∣.
Cartesian version
Lines a1x−x1=b1y−y1=c1z−z1 and a2x−x2=b2y−y2=c2z−z2 have shortest distance
D=(b1c2−b2c1)2+(c1a2−c2a1)2+(a1b2−a2b1)2det(x2−x1a1a2y2−y1b1b2z2−z1c1c2).
(The numerator is the triple product written componentwise; the denominator is ∣d1×d2∣.)
Intersecting check
Distance =0⟺ the two lines intersect. So computing this number doubles as a test: zero means they meet, non-zero means they don't.
Worked examples
Example 1. Find the shortest distance between r=i^+2j^+3k^+t(i^+j^+k^) and r=2i^+3j^+4k^+s(2i^−j^+k^).
Example 4. Find the distance between the two parallel lines r=i^+j^+t(2i^−j^+k^) and r=2i^−j^+s(2i^−j^+k^).
Parallel direction d=(2,−1,1), ∣d∣=6. a2−a1=(1,−2,0). (a2−a1)×d=i^12j^−2−1k^01=i^(−2−0)−j^(1−0)+k^(−1+4)=−2i^−j^+3k^. Magnitude 4+1+9=14.
Distance =14/6=14/6=7/3.
Example 5. Find the shortest distance between r=(1,2,3)+t(1,0,0) and r=(0,0,0)+s(0,0,1).
d1=(1,0,0), d2=(0,0,1). d1×d2=(0,−1,0), magnitude 1. a2−a1=(−1,−2,−3). Dot with cross: (−1)(0)+(−2)(−1)+(−3)(0)=2. Distance =2. (Note: this is the y-coordinate difference, since the lines are the x-axis-parallel line at y=2,z=3 and the z-axis. Closest approach has y=2 in line 1 and y=0 in line 2, so the gap in y alone is 2.)
Example 6. Determine whether the lines 1x=2y=3z and 1x−2=2y−4=3z−6 are coincident, parallel, or skew.
Same direction (1,2,3). Try the point (2,4,6) on the second line: does it lie on the first? 12=24=36=2. Yes , coincident.
Try it yourself
Shortest distance between r=(1,0,0)+t(0,0,1) and r=(0,1,0)+s(1,0,0).
Shortest distance between 1x−1=2y−2=3z−3 and 1x−2=2y−3=3z−4 , note they may be parallel or coincident.
Find the shortest distance between r=(3,5,7)+t(1,−2,1) and r=(−1,−1,−1)+s(7,−6,1).
Show that lines 1x+2=2y−1=3z−5 and 2x+1=4y=6z+1 are coplanar.
Find the shortest distance between two parallel lines 2x−1=3y+1=1z and 2x−4=3y=1z−1.
Find the line that is the common perpendicular of r=(0,0,0)+t(1,0,0) and r=(0,0,1)+s(0,1,0).
For what values of λ are the lines 2x−1=3y+1=4z−1 and 1x−3=2y−λ=1z coplanar?
Determine if r=(1,2,3)+t(2,0,1) and r=(2,4,6)+s(1,1,0) intersect.
Find the perpendicular distance from origin to 1x−1=1y=1z.
Lines L1:r=td1 and L2:r=b+sd2 both pass through origin (if b=0) , find shortest distance for b=(1,1,1), d1=(1,0,0), d2=(0,1,0).
Find shortest distance between x-axis and r=(1,1,1)+t(0,0,1).
The two diagonals of opposite faces of a unit cube , find the distance between them.
Find the foot of the common perpendicular from (0,0,0) on the line through (1,1,1) with direction (1,1,1).
Two skew edges of a regular tetrahedron with side 1 , find the shortest distance between them.
Pitfalls and tricks
Test parallelism first before applying the skew formula , division by zero awaits.
Triple product zero⇔ coplanar lines (intersect or parallel).
Absolute value outside the entire expression , the unsigned distance is what you want.
For parallel lines use the cross-product formula ∣(a2−a1)×d∣/∣d∣.
Sketch when possible. Even a sketch of one line plus a perpendicular helps verify the geometry.