Ma · Mathematics

How to Find the Shortest Distance Between Two Parallel Lines in 3D

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose points

    Pick one point on each line and identify a nonzero direction vector shared by both lines.

  2. 2. Find the separation

    Subtract the point coordinates to get a vector connecting the two chosen points.

  3. 3. Take the cross product

    Cross the separation vector with the direction vector, then find the cross product's magnitude.

  4. 4. Divide by direction length

    Divide that magnitude by the direction vector's magnitude to get the shortest distance.

To find the shortest distance between two parallel lines in three-dimensional space, compare points on the lines in the direction perpendicular to both. The points need not be chosen specially: a cross product removes the component of their separation that runs along the lines.

The distance formula

Write the two lines using points PP and QQ and a shared nonzero direction vector uu:

L1:P+tu,L2:Q+su.L_1: P+t u, \qquad L_2: Q+s u.

Here tt and ss range over all real numbers. The shortest distance is

d=∥(Q−P)×u∥∥u∥.d=\frac{\lVert (Q-P)\times u\rVert}{\lVert u\rVert}.

The cross product’s magnitude is the area of the parallelogram formed by Q−PQ-P and uu. Dividing that area by the length of the base, ∥u∥\lVert u\rVert, gives the parallelogram’s height. That height is exactly the perpendicular separation between the lines.

The direction vectors in the lines’ equations do not have to be written identically. If they are parallel, one is a nonzero scalar multiple of the other, so you can use either as uu. The formula is unchanged if you scale uu by a nonzero number.

Why the formula gives the minimum

Let w=Q−Pw=Q-P. A point on the second line minus a point on the first is

w+(s−t)u.w+(s-t)u.

As you move along the lines, the value of s−ts-t can be any real number. This changes only the component parallel to uu; the component of ww perpendicular to uu stays the same. The smallest possible separation occurs when the parallel component has been canceled.

The perpendicular component of ww is ww minus its projection onto uu. Its length is therefore

∥w∥2−(w⋅u)2∥u∥2.\sqrt{\lVert w\rVert^2-\frac{(w\cdot u)^2}{\lVert u\rVert^2}}.

The cross-product identity ∥w×u∥2=∥w∥2∥u∥2−(w⋅u)2\lVert w\times u\rVert^2=\lVert w\rVert^2\lVert u\rVert^2-(w\cdot u)^2 shows that this is the same as ∥w×u∥/∥u∥\lVert w\times u\rVert/\lVert u\rVert.

Worked example

Consider the lines

L1=(1,2,0)+t(2,−1,2),L2=(4,0,3)+s(4,−2,4).L_1=(1,2,0)+t(2,-1,2), \qquad L_2=(4,0,3)+s(4,-2,4).

Their direction vectors are parallel because (4,−2,4)=2(2,−1,2)(4,-2,4)=2(2,-1,2). Choose u=(2,−1,2)u=(2,-1,2), P=(1,2,0)P=(1,2,0), and Q=(4,0,3)Q=(4,0,3). The vector between the selected points is

Q−P=(3,−2,3).Q-P=(3,-2,3).

Compute the cross product and the direction-vector length:

(Q−P)×u=(−1,0,1),∥(Q−P)×u∥=2,∥u∥=3.(Q-P)\times u=(-1,0,1), \qquad \lVert (Q-P)\times u\rVert=\sqrt{2}, \qquad \lVert u\rVert=3.

So the shortest distance is

d=23≈0.471.d=\frac{\sqrt{2}}{3}\approx 0.471.

The points PP and QQ themselves are not the closest pair; their separation is 22\sqrt{22}. The formula extracts only the part of that separation perpendicular to the lines.

How to identify the needed vectors

If a line is given in vector form r=P+tur=P+t u, the point is PP and the direction vector is uu. In parametric equations such as x=x0+atx=x_0+at, y=y0+bty=y_0+bt, z=z0+ctz=z_0+ct, the direction vector is (a,b,c)(a,b,c) and a point is (x0,y0,z0)(x_0,y_0,z_0).

If a line is specified by two points, subtract the coordinates of those points to obtain its direction vector. For two lines, check that their direction vectors are parallel—for example, that their cross product is the zero vector—before using the parallel-lines formula.

Common mistakes

  • Forgetting to divide by ∥u∥\lVert u\rVert. The magnitude ∥(Q−P)×u∥\lVert (Q-P)\times u\rVert is an area, not a distance. Division by the direction-vector length converts it to a height.
  • Using the distance between arbitrary points. ∥Q−P∥\lVert Q-P\rVert is generally longer than the shortest distance because it can include a component along the lines.
  • Using nonparallel direction vectors. The formula above assumes the lines are parallel. For skew, nonparallel lines with direction vectors uu and vv, the distance is instead ∣(Q−P)⋅(u×v)∣/∥u×v∥\lvert (Q-P)\cdot(u\times v)\rvert/\lVert u\times v\rVert.
  • Treating coincident lines as separate. If the lines lie on top of each other, the cross product in the parallel-lines formula is zero, correctly giving distance zero.

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.