The whole discussion is on two-dimensional only. Suppose the 2 line segments are defined by points (\vec{a_0}, \vec{a_1}) and (\vec{p_0}, \vec{p_1}). The 2 segments can either be parallel or non-parallel.


Non-parallel case. There are at least 2 methods to determine, straightforward thinking is to construct the equation of the 2 lines using the 2 segments

\displaystyle \vec{r} = \vec{a_0} + h (\vec{a_1} - \vec{a_0}) \\ \vec{r} =  \vec{p_0} + k (\vec{p_1} - \vec{p_0})

solve for the parameters h, k. One way is using bull force to solve it. Another way is the area ratio:

\displaystyle h = \frac{ (\vec{p_0} - \vec{a_0}) \times (\vec{p_1} - \vec{a_0}) } { (\vec{p_0} - \vec{p_1}) \times (\vec{a_1} - \vec{a_0}) }

The nominator is the area of the triangle \vec{a_0}, \vec{p_0}, \vec{p_1} . The denominator is the area of the whole area \vec{a_0}, \vec{p_0}, \vec{a_1}, \vec{p_1}. The whole area is

\displaystyle A= (\vec{p_0} - \vec{a_0}) \times (\vec{a_1} - \vec{a_0}) +(\vec{a_1} - \vec{a_0}) \times (\vec{p_1} - \vec{a_0}) = (\vec{p_0} - \vec{p_1}) \times (\vec{a_1} - \vec{a_0})

The simplification uses the identity \vec{u}\times \vec{v} + \vec{v} \times \vec{w} = (\vec{u} - \vec{w} ) \times \vec{v} .Similarly.

\displaystyle k = \frac{ (\vec{a_1} - \vec{p_0}) \times (\vec{a_0} - \vec{p_0}) } { (\vec{p_0} - \vec{p_1}) \times (\vec{a_1} - \vec{a_0}) }

The intersect condition is

\displaystyle 0 \le h \le 1~ \textrm{and} ~0 \le k \le 1


In the case of parallel, there are 2 cases: with offset or without offset

In the case of offset, the “whole” area A = 0 , geometrically, the cross-product (\vec{p_0} - \vec{a_0}) \times (\vec{a_1} - \vec{a_0}) < 0 as it is clockwise. Since the 2 segments are parallel, the perpendicular distances from \vec{p_0} or \vec{p_1} to line (\vec{a_0}, \vec{a_1}) are the same. So, the area A = 0

When the 2 segments are “co-planar”, the nominator becomes zero too. To check if the two segments overlap or not, simply check x- or y-component is enough.


\displaystyle A = (a_0.x - a_1.x) (p_0.y - p_1.y) -  (a_0.y - a_1.y) (p_0.x - p_1.x)

\displaystyle h = \frac{1}{A} \left( (a_0.x - p_1.x) (p_0.y - a_0.y) -  (a_0.y - p_1.y) (p_0.x - a_0.x)  \right)

\displaystyle k = \frac{1}{A} \left( (p_0.x - a_0.x) (a_1.y - p_0.y) -  (p_0.y - a_0.y) (a_1.x - p_0.x)  \right)