Geometric Intersection
Bentley-Ottmann Sweep
The Bentley-Ottmann algorithm (1979) computes all intersection points among line segments in time and space.
Algorithm
Sweep line: a vertical line moves left to right across the plane. At any position, the segments intersecting the sweep line are ordered by -coordinate.
Data structures:
- Event queue (priority queue, ordered by -coordinate): stores left endpoints, right endpoints, and intersection points
- Status structure (balanced BST): stores the segments currently crossing the sweep line, ordered by their -coordinate at the sweep line position
Events:
- Left endpoint of segment : insert into the status structure. Check for intersections with its immediate neighbors above and below
- Right endpoint of segment : remove from the status structure. Check for intersection between 's former neighbors (they are now adjacent)
- Intersection point of segments : report the intersection. Swap and in the status structure (their order reverses). Check each with its new neighbor for future intersections
Correctness invariant: an intersection is discovered as an event only when the two segments become adjacent in the status structure. This happens before their actual intersection point (since any "separating" segment must have entered and left between them earlier).
Complexity Analysis
- events total
- Each event processed in time (BST operations)
- Total: time, space for the event queue (can be reduced to by discovering intersections lazily)
Space optimization: only store the "next" intersection between adjacent pairs; delete stale events when pairs are separated. This keeps the event queue at size.
Degeneracies
The original algorithm assumes general position. Practical issues:
- Overlapping segments: treat as a single segment or handle explicitly
- Multiple segments through one point: process all events at the same -coordinate simultaneously; handle the ordering carefully
- Vertical segments: perturb (conceptually rotate slightly) or handle as a special event spanning a -range
- Three segments meeting at a point: the event queue may contain duplicate intersection events; deduplicate
Relation to Arrangement Construction
Bentley-Ottmann computes the arrangement of line segments. The arrangement has vertices, edges, and faces. The sweep implicitly constructs the trapezoidal decomposition.
Red-Blue Intersection
Given two sets of segments (red and blue), find all red-blue intersection pairs (ignoring red-red and blue-blue intersections).
Applications: overlay of two maps/layers, Boolean operations on polygons (edges from different polygons).
Algorithms:
- Modified Bentley-Ottmann: only check intersections between differently colored adjacent segments. Same complexity
- Balaban's algorithm (1995): using a more sophisticated hierarchical approach. Optimal but complex
- Segment tree approach: insert one color into a segment tree, query with the other.
For the special case where both sets are non-crossing (e.g., edges of two simple polygons), algorithms achieving are simpler.
Rectangle Intersection
Axis-Aligned Rectangle Intersection
Given axis-aligned rectangles, find all intersecting pairs.
Sweep line approach:
- Create events at left and right edges of each rectangle
- Sweep left to right; maintain active rectangles (those whose vertical interval is "alive")
- At each left edge: query active rectangles for those overlapping the new rectangle's -interval; insert the new rectangle
- At each right edge: remove the rectangle
Using an interval tree or segment tree for the active set: time.
Clarkson-Shor randomized: expected without the sweep.
Rectangle Enclosure
Find all pairs where one rectangle contains another. Reduces to dominance queries: rectangle contains iff , , and similar for top/bottom. This is a 4D dominance problem, solvable in .
Measure of the Union
Compute the area of the union of rectangles.
Sweep line + segment tree: sweep vertically, maintain the total length of the covered portions of the sweep line using a segment tree with "count" annotations. time.
Klee's measure problem: generalization to dimensions. is the best known for (Chan, 2013). The problem is closely related to depth computation and has connections to circuit complexity lower bounds.
Minkowski Sums
The Minkowski sum of sets and is:
Convex Polygons
If has vertices and has vertices (both convex):
- is convex with at most vertices
- Algorithm: merge the sorted edge sequences of and (sort by angle), yielding the edge sequence of in time
Simple Polygons
If is a simple polygon with vertices and is convex with vertices:
- Decompose into convex pieces (e.g., triangulation: pieces)
- Compute Minkowski sum of each piece with : total
- Take the union of all resulting polygons: via arrangement computation
For two non-convex polygons: complexity can be in the worst case (tight for simple polygons).
Applications
- Collision detection: object collides with iff the origin lies in (Minkowski difference). This reduces collision to a point-in-polygon test
- Robot motion planning: configuration space obstacle is the Minkowski sum of the obstacle with the robot's reflection through the origin
- Morphological operations: dilation in image processing is Minkowski sum with a structuring element; erosion is Minkowski difference
- Offsetting: Minkowski sum with a disk gives the offset curve/polygon
Boolean Operations on Polygons
Compute union, intersection, difference, and symmetric difference of polygonal regions.
Weiler-Atherton Algorithm
Classic algorithm for polygon clipping (intersection/union of two simple polygons):
- Find all intersection points between edges of the two polygons
- At each intersection, record entry/exit status (entering or leaving the other polygon)
- Traverse the boundary, switching between polygon boundaries at intersection points:
- For intersection: follow the clip polygon when entering, subject polygon when exiting
- For union: follow the outer boundaries
Handles multiple components and holes with careful bookkeeping. where is the number of intersections.
Sweep-Line Approach (Nef Polygons)
Represent polygons as Nef polygons (closed under all Boolean operations, including complement):
- Compute the arrangement of all edges from both polygons
- Label each face of the arrangement with its membership in and
- Extract faces satisfying the Boolean condition (e.g., for intersection: faces in both and )
This is the approach used in CGAL. Handles all degeneracies, multiple components, holes, and unbounded faces. Complexity: for the arrangement, where is the number of edge intersections.
Greiner-Hormann Algorithm
Simple algorithm for polygon clipping that handles arbitrary simple polygons (including concave):
- Insert intersection points into both polygon edge lists
- Mark intersections as entering/exiting
- Traverse, collecting output polygons by alternating between input polygons at intersections
Simpler than Weiler-Atherton but struggles with degeneracies (T-intersections, overlapping edges). The Foster-Overfelt variant addresses many degenerate cases.
Vatti's Algorithm
Used in the Clipper library. Handles complex polygons with self-intersections:
- Sweep-line based
- Processes horizontal spans ("scanbeams")
- Supports union, intersection, difference, XOR
- Handles holes, self-intersecting polygons, and floating-point coordinates
The Clipper library (Angus Johnson) and Clipper2 are widely used practical implementations.
Polygon Overlay and Map Operations
Map overlay: given two planar subdivisions (maps), compute their overlay (arrangement of all edges from both maps, with faces inheriting attributes from both input maps).
Algorithm: essentially the Bentley-Ottmann sweep adapted for planar subdivisions:
- Compute all edge intersections between the two maps
- Split edges at intersection points
- Build the combined DCEL (doubly-connected edge list)
- Assign face attributes by traversing adjacencies
time. This is a core operation in geographic information systems (GIS).
Segment Intersection Counting and Reporting Lower Bounds
Counting: given segments, determine the number of intersections.
- lower bound (even for detecting if any intersection exists, via sorting reduction)
- algorithm exists for counting (without reporting) --- uses multi-level partition trees (Agarwal, 1990)
Reporting: lower bound. The Bentley-Ottmann algorithm nearly matches this (off by a factor on ).
Optimal: Balaban (1995) achieves for reporting, but the algorithm is significantly more complex than Bentley-Ottmann.
3D Intersection Problems
Triangle-Triangle Intersection
Two triangles in 3D can intersect in a point, a segment, or overlap (coplanar). The Moller algorithm tests intersection in using orientation predicates:
- Test if all vertices of triangle 1 are on the same side of triangle 2's plane (and vice versa)
- If not, compute the intersection of each triangle with the other's plane (a line segment)
- Test if the two segments overlap
3D Segment-Triangle Intersection
Ray-triangle intersection is fundamental to ray tracing. Moller-Trumbore algorithm: compute barycentric coordinates of the intersection point via Cramer's rule in . The point is inside the triangle iff all barycentric coordinates are in .
Bounding Volume Hierarchies (BVH)
For efficient intersection testing among many objects:
- AABB trees: axis-aligned bounding box hierarchy. Fast overlap test (coordinate comparisons only), but loose fit for rotated objects
- OBB trees: oriented bounding boxes. Tighter fit, more expensive overlap test
- Sphere trees: simplest overlap test, but poor fit for elongated objects
- BVH construction: top-down (split by median/SAH), bottom-up (agglomerative), or spatial (binning). Surface area heuristic (SAH) optimizes expected ray traversal cost
SAH cost model: where are the probabilities of hitting the left/right child (proportional to surface area). Minimized by greedy splitting.