Check if two line segments intersect

geometry

I have a two line segments. Points at both ends of those segments are given by cooridnates (x, y, z).
What's more I know that those segments lie on the same plane given by a normal. How can I check if those segments intersect?

Best Answer

Since the segments are on the same plane, you can test whether they intersect by testing their projection onto one of the coordinate planes. The standard 2D test that does not compute the intersection point is described here. Here is pseudo code:

ccw(a,b,c):
   return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y)

intersect(a,b):
   if (ccw(a.p, a.q, b.p) * ccw(a.p, a.q, b.q) > 0) return false
   if (ccw(b.p, b.q, a.p) * ccw(b.p, b.q, a.q) > 0) return false
   return true