This article deals with the intersection of two segments ( and ) in 3D. Of course, it also works in 2D : simply set the z-value of each point equal to zero. Each segment is defined by two points:

- segment is defined by points and
- segment is defined by points and

The following illustration shows a configuration of segments:

## Check for parallelism

Before calculating an intersection, you must check if the segments are parallel or not. An efficient method for checking parallelism can be found here.

If the segments are parallel, you must check if they are also collinear. Based on the knowledge of parallelism, checking if , and are aligned is enough to conclude about colinearity. An efficient method can be found here.

If the segments are collinear, an intersection may exist. This intersection may be a point or a segment. Let’s compute the following dot products:

If the segments are parallel, you must check if they are also collinear. Based on the knowledge of parallelism, checking if , and are aligned is enough to conclude about colinearity. An efficient method can be found here.

If the segments are collinear, an intersection may exist. This intersection may be a point or a segment. Let’s compute the following dot products:

The following table shows all the possibilities of intersection between the segments according to , and . There is no intersection in the red cells. In the grey cells the intersection is a point and in the blue cells the intersection is a segment. Note that in the middle cell, there is two possibilities that makes necessary to check if before concluding.

## 3D case

If the problem statement is expressed in 3D, it is first necessary to check if the lines segments are coplanar i.e. the four points , , and belong to the same plane. An efficient method can be found here. If the segments are not coplanar, there is no intersection.

## Parametric form

The segments can be rewritten in there parametric form :

with:

## Intersection point

From here we assume that the segments are coplanar and not parallel, therefore the lines passing through and and the line passing through and intersect. The lines intersection must perforce be a point, but this point does not necessarily lie on both segments and . If we assume that the lines intersect, we can look for the intersection point that satisfies the two following equations :

This gives us this equation to solve:

Rewrite the equation:

Take the cross product of each side with :

Simplify the equation:

Take the dot product of each side with

Now deduce and :

Note that must not be equal to zero. It happens when the vectors and are parallel and parallelism must be check prior to calculating and . The segments intersect if and :

- lies on segment if .

- lies on segment if .

If and the intersection point belongs on both segments. The coordinates of the intersection point can easily be calculated with the following formula:

This is also equal to: