On this page
You have two line segments, AB and CD, given only by their endpoints. Do they cross? The answer needs no slopes, no division and no special case for vertical lines: one formula, used four times.
What is the orientation test?
For three points A, B and P, the orientation is the sign of a 2D cross product:
orient(A, B, P) = (xB − xA)(yP − yA) − (yB − yA)(xP − xA)
The curve orientation page gives this as the form of the orientation determinant commonly used for numerical reasons: a positive value means counterclockwise, a negative value means clockwise, and it is non-zero only when the points are not collinear.
Positive: P is to the left of the directed line from A to B (a counterclockwise turn).
Negative: P is to the right (a clockwise turn).
Zero: A, B and P lie on one line.
How do you test if two line segments intersect?
The rule, as cp-algorithms states it: A and B must not lie on the same side of line CD, and C and D must not lie on the same side of line AB, which you check with a couple of cross products. As steps:
Compute d1 = orient(C, D, A), d2 = orient(C, D, B), d3 = orient(A, B, C) and d4 = orient(A, B, D).
If d1 and d2 have strictly opposite signs, and d3 and d4 have strictly opposite signs, the segments cross at one point inside both.
If all four are zero, the segments lie on one line. The same page says it is then enough to check whether their projections on the x axis and the y axis overlap.
If some of them are zero, each zero marks an endpoint lying on the other segment's line. The segments touch if such an endpoint also sits between the other segment's endpoints in both x and y.
Otherwise, they do not meet.
A worked example with coordinates
Take A(0, 0), B(8, 4), C(8, 1) and D(0, 9).
Where do C and D sit relative to AB? Here B − A = (8, 4).
orient(A, B, C) = 8 × 1 − 4 × 8 = −24, so C is to the right.
orient(A, B, D) = 8 × 9 − 4 × 0 = +72, so D is to the left.
Where do A and B sit relative to CD? Here D − C = (−8, 8).
orient(C, D, A): A − C = (−8, −1), so (−8)(−1) − 8(−8) = +72.
orient(C, D, B): B − C = (0, 3), so (−8)(3) − 8(0) = −24.
Both pairs have opposite signs, so the segments cross. Integer inputs give exact signs, and the cp-algorithms code works in integer arithmetic.
How do you find the intersection point with t and u?
Write each segment with a parameter: AB is A + t·r with r = B − A, and CD is C + u·s with s = D − C. The line–line intersection page uses this form and states that there is an intersection if 0 ≤ t ≤ 1 and 0 ≤ u ≤ 1.
Set A + t·r = C + u·s and take the cross product of both sides with s, where a × b means ax·by − ay·bx. Since s × s = 0, the u term drops out.
t = ((C − A) × s) / (r × s)
u = ((C − A) × r) / (r × s)
In our example, r = (8, 4), s = (−8, 8) and C − A = (8, 1).
r × s = 8 × 8 − 4 × (−8) = 96.
(C − A) × s = 8 × 8 − 1 × (−8) = 72, so t = 72/96 = 0.75.
(C − A) × r = 8 × 4 − 1 × 8 = 24, so u = 24/96 = 0.25.
The point is A + 0.75·r = (6, 3). Check: C + 0.25·s = (6, 3).
If r × s = 0, the segments are parallel and the formula divides by zero; use the collinear check in step 3 of the rule instead.
Where is the segment intersection test used?
Checking every pair of n segments takes n(n − 1)/2 tests. Jon Bentley and Thomas Ottmann published a sweep-line method in IEEE Transactions on Computers in 1979, and the Bentley–Ottmann algorithm reports all k crossings among n segments in O((n + k) log n) time.
The same sign decides each left or right turn in our Graham scan convex hull walk-through, and in ray casting for point in polygon each ray-edge check is a segment crossing test.
In JEE coordinate geometry it is the familiar same-side-of-a-line question, answered by comparing the signs of ax + by + c. If those signs feel shaky, the Fermi Concept Library has a lesson for every concept in mathematics and computing, each with its place in the map. To practise, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.
Try it now: replace D with (5, 2) and run the four signs. You should find that the segments do not cross.



