Chapter 120
HomeCheck if two line segments intersect using orientation tests. Two segments AB and CD intersect if orient(A,B,C) and orient(A,B,D) have opposite signs AND orient(C,D,A) and orient(C,D,B) have opposite signs.
bool on_segment(Point a, Point b, Point c) {
return min(a.x, b.x) <= c.x && c.x <= max(a.x, b.x)
&& min(a.y, b.y) <= c.y && c.y <= max(a.y, b.y);
}
bool segments_intersect(Point a, Point b, Point c, Point d) {
double o1 = orient(a, b, c);
double o2 = orient(a, b, d);
double o3 = orient(c, d, a);
double o4 = orient(c, d, b);
if (o1 == 0 && on_segment(a, b, c)) return true;
if (o2 == 0 && on_segment(a, b, d)) return true;
if (o3 == 0 && on_segment(c, d, a)) return true;
if (o4 == 0 && on_segment(c, d, b)) return true;
return (o1 > 0) != (o2 > 0) && (o3 > 0) != (o4 > 0);
}