ExamplesBy LevelBy TopicLearning Paths
839 Fundamental

Sweep Line Algorithm

Functional Programming

Tutorial Video

Text description (accessibility)

This video demonstrates the "Sweep Line Algorithm" functional Rust example. Difficulty level: Fundamental. Key concepts covered: Functional Programming. Many geometric problems — computing area of union of rectangles, finding all intersecting segment pairs, computing Voronoi diagrams — can be solved efficiently by imagining a vertical line sweeping left-to-right through the plane. Key difference from OCaml: | Aspect | Rust | OCaml |

Tutorial

The Problem

Many geometric problems — computing area of union of rectangles, finding all intersecting segment pairs, computing Voronoi diagrams — can be solved efficiently by imagining a vertical line sweeping left-to-right through the plane. The sweep line processes "events" (start/end of intervals, segment crossings) in sorted x-order, maintaining a data structure of active elements. This reduces 2D problems to a sequence of 1D operations. The sweep line technique is the basis for computational geometry algorithms used in GIS, VLSI design, and computer graphics.

🎯 Learning Outcomes

  • • Model the sweep as a priority queue of events sorted by x-coordinate
  • • Maintain an active set of elements currently crossed by the sweep line
  • • Process start events (add to active set) and end events (remove from active set)
  • • Apply to: area of union of rectangles, all-segments intersection detection, closest pair
  • • Understand event-driven programming: only process significant x-positions, not every pixel
  • Code Example

    #![allow(clippy::all)]
    //! Placeholder

    Key Differences

    AspectRustOCaml
    Event typeenum EventTypeVariant type
    Event sortsort_by on Vec<Event>List.sort
    Active setBTreeSet for O(log n) opsSet.Make balanced BST
    Area accumulationf64 variable in loopMutable ref or fold
    Dispatchmatch event.event_typematch event_type with
    Coordinate typef64 or i64float or int

    OCaml Approach

    OCaml uses a variant type for events and List.sort for the event queue. The active set is a Set.Make balanced BST. OCaml's match event_type with Start -> ... | End -> ... cleanly dispatches. The area accumulation uses a mutable float ref or a fold over events. OCaml's float_of_int handles mixed int/float coordinates. The sweep function is naturally recursive over the sorted event list.

    Full Source

    #![allow(clippy::all)]
    //! Placeholder

    Deep Comparison

    OCaml vs Rust: Sweep Line Events

    Overview

    See the example.rs and example.ml files for detailed implementations.

    Key Differences

    AspectOCamlRust
    Type systemHindley-MilnerOwnership + traits
    MemoryGCZero-cost abstractions
    MutabilityExplicit refmut keyword
    Error handlingOption/ResultResult<T, E>

    See README.md for detailed comparison.

    Exercises

  • Implement the area of union of n rectangles using the sweep line in O(n^2) (naive active set) then O(n log^2 n) (segment tree for y-coverage).
  • Find all intersecting pairs among n line segments using the Bentley-Ottmann sweep line algorithm.
  • Implement a sweep line to compute the perimeter of the union of rectangles.
  • Count the maximum number of overlapping intervals at any point using a sweep line over 1D intervals.
  • Extend the rectangle union to 3D: volume of union of axis-aligned boxes using two nested sweeps.
  • Open Source Repos