Skip to content

Instantly share code, notes, and snippets.

function open_segments_intersect(a1, a2, b1, b2) {
if ((max(a1.x, a2.x) <= min(b1.x, b2.x)) || // a is left of b
(max(b1.x, b2.x) <= min(a1.x, a2.x)) ||
(max(a1.y, a2.y) <= min(b1.y, b2.y)) || // a is above b (in coordinates where (0,0) is topleft)
(max(b1.y, b2.y) <= min(a1.y, a2.y))
) {return false;}
else if ((vcross(vsub(b1,a1),vsub(a2,a1)) * vcross(vsub(b2,a1),vsub(a2,a1)) < 0) && // replace these with <= to make the segments act closed.
(vcross(vsub(a1,b1),vsub(b2,b1)) * vcross(vsub(a2,b1),vsub(b2,b1)) < 0)) {return true;}
else {return false;}
}