The core tool is the orientation of an ordered triple of points A, B, C: turning from A→B→C, do you turn left, turn right, or stay straight? It is computed with a single cross product,
orient(A,B,C)=(Bx−Ax)(Cy−Ay)−(By−Ay)(Cx−Ax),
whose sign — positive, negative, or exactly zero — is all that matters. Positive means counter-clockwise, negative means clockwise, and zero means A, B, C are collinear.
- The general test. Segments AB and CD cross (as open segments, ignoring touching endpoints for now) exactly when C and D lie on opposite sides of line AB, and A and B lie on opposite sides of line CD. In signs: orient(A,B,C) and orient(A,B,D) must differ, and orient(C,D,A) and orient(C,D,B) must differ.
- Why it's exact. Every quantity involved is a sum of products of the input coordinates — no division, no square root, no trigonometry. With integer or fixed-precision input the sign is computed exactly, which is exactly why this test, not the "solve for the intersection point" approach, is the standard building block in real geometry engines.
- The collinear special case. If any orientation comes out to zero, the two points are exactly on the line, and the general rule above cannot be trusted alone. The segments might overlap along a line, touch at a single endpoint, or miss each other entirely while collinear — this needs an explicit on-segment bounding-box check (C must lie between A and B) layered on top.
- Cost. Four orientation computations, four comparisons, and — only when a zero appears — a handful of coordinate comparisons. That is O(1) time and O(1) space per pair of segments, independent of how large the coordinates are.
This primitive is also exactly what powers the sweep line in Segment Intersection: scaling the same yes/no test to many segments at once is what turns an O(n2) pairwise scan into an output-sensitive algorithm.
Comments
Loading comments...