Algorithms · Visualized
Geometry: Line Sweep
Computational Geometry
Points, polygons, and hulls — geometry made computational.
Line Sweep is a technique (not a single algorithm) where an imaginary vertical (or horizontal) line moves across the plane from left to right, processing geometric events (like the start or end of an interval/shape) in sorted order as the line crosses them. Time: O(n log n) average, O(n log n) worst; space: O(n) — event list and active-state structure.
Big-O: Cross products and sweeping tell left/right and inside/outside.
Line Sweep
Computational GeometryLine Sweep is a technique (not a single algorithm) where an imaginary vertical (or horizontal) line moves across the plane from left to right, processing geometric events (like the start or end of an interval/shape) in sorted order as the line crosses them. It converts many 2D geometry problems into simpler 1D event-processing problems.
How Line Sweep Works
- Identify all 'events' relevant to the problem — for example, the left and right edges of rectangles, or the start/end points of intervals.
- Sort all events by their x-coordinate (the position where the sweep line would encounter them).
- Move the sweep line from left to right, processing events strictly in this sorted order.
- Maintain an active data structure (like a balanced tree, heap, or count) representing the current state of everything the sweep line is currently crossing.
- Update the answer to the problem whenever relevant events occur (e.g. an overlap starts, a maximum count is reached).
Line Sweep Worked Example
Input: {intervals: [[1, 5], [2, 6], [8, 10]]}
Convert to events: (1,'start'),(2,'start'),(5,'end'),(6,'end'),(8,'start'),(10,'end'). Sweeping left to right: at x=1 active count=1. At x=2 active count=2 (peak). At x=5 count=1. At x=6 count=0. At x=8 count=1. At x=10 count=0. This sweep reveals the maximum overlap was 2, occurring between x=2 and x=5.
def merge_overlap_rectangles(rects):
# sweep events over x: (x, kind(+1 start / -1 end), y1, y2)
events = []
for x1, y1, x2, y2 in rects:
events.append((x1, 1, y1, y2))
events.append((x2, -1, y1, y2))
events.sort()
active = []
prev_x = events[0][0]
def y_coverage():
if not active:
return 0
active.sort()
total = 0
cur_l, cur_r = active[0]
for y1, y2 in active[1:]:
if y1 > cur_r:
total += cur_r - cur_l
cur_l, cur_r = y1, y2
else:
cur_r = max(cur_r, y2)
return total + cur_r - cur_l
area = 0
for x, kind, y1, y2 in events:
area += (x - prev_x) * y_coverage()
active.append((y1, y2)) if kind == 1 else active.remove((y1, y2))
prev_x = x
return areaBehind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Line Sweep Time & Space Complexity
When to Use Line Sweep
- Finding overlapping rectangles, intervals, or shapes; skyline problems; and as the underlying technique behind Closest Pair of Points and Convex Hull variants.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Keep the momentum going
Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.