Skip to the workspace
Pebellslearn by building
Computational Geometry  ›  Lesson

Do These Two Routes Cross?

Segment Intersection 28 minFour orientation tests decide most segment crossings, but a special case — two segments overlapping along the same line — needs a bounding-box check, not just a sign comparison

dsa / Do These Two Routes Cross?

Explore each scene at your own pace.

01 · A clean X crossing

A clean X crossing

Do These Two Routes Cross?

Definition
Segment intersection testing decides whether two line segments share a point: they cross when each segment's endpoints lie on opposite sides of the other segment's line, with extra checks for the case where points are collinear and may merely touch or overlap.
In plain words
Two roads cross when each one passes from one side of the other to the opposite side. If they run along the same line, you must also check whether they actually overlap, because being on the same line is not the same as meeting.

In short: two segments p1-p2 and p3-p4 cross if p1 and p2 fall on opposite sides of the line through p3 and p4, AND p3 and p4 fall on opposite sides of the line through p1 and p2. That's four calls to last lesson's orientation and one comparison each way. The naive version of that test has a gap: two segments that run along the same line and overlap give every orientation a 0, which the naive test reads as "no crossing" — wrong. Closing that gap needs one more check: when an orientation comes back 0, look at whether the point actually falls inside the other segment's span.

Two segments crossing in an X, with the four orientation values badged at their corners — d1 and d2 on opposite sides of segment p3-p4, d3 and d4 on opposite sides of segment p1-p2 — and a second pair of segments lying on the same horizontal line, overlapping, with all four badges reading zero and a question mark over them

A clean X crossing

Two Meridian delivery routes are proposed, and the dispatcher wants to know if they cross before either driver sets off:

step through it
1def orientation(a, b, c):
2 cross = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0])
3 if cross > 0:
4 return 1
5 if cross < 0:
6 return -1
7 return 0
8 
9def on_segment(p, a, b):
10 return (min(a[0], b[0]) <= p[0] <= max(a[0], b[0]) and
11 min(a[1], b[1]) <= p[1] <= max(a[1], b[1]))
12 
13def segments_intersect(p1, p2, p3, p4):
14 d1 = orientation(p3, p4, p1)
15 d2 = orientation(p3, p4, p2)
16 d3 = orientation(p1, p2, p3)
17 d4 = orientation(p1, p2, p4)
18 if d1 != d2 and d3 != d4:
19 return True
20 if d1 == 0 and on_segment(p1, p3, p4):
21 return True
22 if d2 == 0 and on_segment(p2, p3, p4):
23 return True
24 if d3 == 0 and on_segment(p3, p1, p2):
25 return True
26 if d4 == 0 and on_segment(p4, p1, p2):
27 return True
28 return False
29 
30route_a = ((0, 0), (4, 4))
31route_b = ((0, 4), (4, 0))
32print(segments_intersect(*route_a, *route_b))

route_a's endpoints land on opposite sides of route_b's line, and route_b's endpoints land on opposite sides of route_a's line too — both conditions hold, so d1 != d2 and d3 != d4 is already True and the function returns on the first check, with no collinear case needed at all.

Scene 1 of 5
Project context and further resources
You're building a piece ofMeridian — service areas & coverage
This piece — segments_intersect(): Tells a dispatcher whether two proposed routes cross, including the case where they run along the same line.
Scenario A dispatcher is about to add a new delivery route and needs to know, before committing it, whether it crosses a route that is already running.
segments_intersect.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
segments_intersect(p1, p2, p3, p4) → boolTrue if segment p1-p2 and segment p3-p4 share any point — a proper crossing, a touching endpoint, or a collinear overlap.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.