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 Geometry

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. It converts many 2D geometry problems into simpler 1D event-processing problems.

How Line Sweep Works

  1. Identify all 'events' relevant to the problem — for example, the left and right edges of rectangles, or the start/end points of intervals.
  2. Sort all events by their x-coordinate (the position where the sweep line would encounter them).
  3. Move the sweep line from left to right, processing events strictly in this sorted order.
  4. Maintain an active data structure (like a balanced tree, heap, or count) representing the current state of everything the sweep line is currently crossing.
  5. 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.

Geometry: Line SweepO(n log n)
Slide a vertical line and count overlapping intervals.
0
[1, 5]
1
[2, 6]
2
[8, 10]
sweepXnull
overlaps0
maxOverlaps0
Step 1 / 8
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Line Sweep: slide a vertical line left→right and count how many intervals cover it.
Python · Geometry Algorithms in Code
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 area

Behind 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

BestO(n log n) — dominated by sorting events
AverageO(n log n)
WorstO(n log n)
SpaceO(n) — event list and active-state structure

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.