Geometry: How to determine if two lines are parallel in 3D based on coordinates of 2 points on each line?

Compute $$AB\times CD$$ which is zero for parallel lines.

In practice there are truncation errors and you won't get zero exactly, so it is better to compute the (Euclidean) norm and compare it to the product of the norms. Hence

$$(AB\times CD)^2<\epsilon^2\,AB^2\,CD^2.$$


Note that this is the same as normalizing the vectors to unit length and computing the norm of the cross-product, which is the sine of the angle between them.

So in the above formula, you have $\epsilon\approx\sin\epsilon$ and $\epsilon$ can be interpreted as an angle tolerance, in radians.


The two lines are parallel just when the following three ratios are all equal: $$ \frac{ax-bx}{cx-dx}, \ \frac{ay-by}{cy-dy}, \ \frac{az-bz}{cz-dz} \ . $$ It's easy to write a function that returns the boolean value you need. But the floating point calculations may be problematical. If any of the denominators is $0$ you will have to use the reciprocals. If your points are close together or some of the denominators are near $0$ you will encounter numerical instabilities in the fractions and in the test for equality. Take care. Program defensively.


All you need to do is calculate the DotProduct. (Google "Dot Product" for more information.)

In detail:

If line #1 contains points A and B, and line #2 contains points C and D, then:

Calculate vector #1: Vector1 = A - B.

Calculate vector #2: Vector2 = C - D.

Then, normalize both vectors.

Then, calculate the dot product of the two vectors. (The dot product is a pretty standard operation for vectors so it's likely already in the C# library.) This will give you a value that ranges from -1.0 to 1.0.

If Vector1 and Vector2 are parallel, then the dot product will be 1.0. If the vector C->D happens to be going in the opposite direction as A->B, then the dot product will be -1.0, but the two lines will still be parallel.

There could be some rounding errors, so you could test if the dot product is greater than 0.99 or less than -0.99. In either case, the lines are parallel or nearly parallel.

If you google "dot product" there are some illustrations that describe the values of the dot product given different vectors. Here's one: http://www.kimonmatara.com/wp-content/uploads/2015/12/dot_prod.jpg