Rectangles
Overlapping Rectangles
Geometry
Computational Geometry
Algorithm

Given a set of rectangles, do any overlap?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Understanding Rectangle Overlap

In computational geometry, one of the classic problems is detecting whether a set of rectangles overlap. This problem has diverse applications in computer graphics, geographic information systems, and collision detection in gaming, to name a few. In this article, we delve into the logic and techniques to determine if any two rectangles from a given set overlap.

Rectangle Representation

To determine overlapping rectangles, we first need a way to represent them. A simple and efficient method is to define each rectangle by its bottom-left and top-right vertices. Formally, a rectangle can be represented as:

• Rectangle RR: (xleft,ybottom,xright,ytop)(x_{\text{left}}, y_{\text{bottom}}, x_{\text{right}}, y_{\text{top}})

where: • $x_\{\text\{left\}\}$ and $x_\{\text\{right\}\}$ are the x-coordinates of the left and right sides of the rectangle, respectively. • $y_\{\text\{bottom\}\}$ and $y_\{\text\{top\}\}$ are the y-coordinates of the bottom and top sides of the rectangle, respectively.

Conditions for Overlapping

Rectangles $ R1 $ and $ R2 $ do not overlap if any of the following conditions are true:

  1. R1.xright<=R2.xleftR1.x_{\text{right}} <= R2.x_{\text{left}} : R1R1 is completely to the left of R2R2.
  2. R2.xright<=R1.xleftR2.x_{\text{right}} <= R1.x_{\text{left}} : R2R2 is completely to the left of R1R1.
  3. R1.ytop<=R2.ybottomR1.y_{\text{top}} <= R2.y_{\text{bottom}} : R1R1 is completely below R2R2.
  4. R2.ytop<=R1.ybottomR2.y_{\text{top}} <= R1.y_{\text{bottom}} : R2R2 is completely below R1R1.

Conversely, if none of these conditions hold, the rectangles overlap.

Algorithmic Approach

Given a set of n rectangles, the brute-force approach involves checking each pair for overlap. The complexity of this approach is O(n2)O(n^2), which may be inefficient for large datasets.

Optimized Approach

One optimization involves sorting and then scanning:

  1. Sweep Line Algorithm: This is a more sophisticated approach that reduces the complexity by using a sweep line, a conceptual vertical line, that moves from the leftmost to the rightmost point of the rectangle set. As this line "sweeps," events occur at the left and right edges of each rectangle.
  2. Event Points: Treat both left and right edges of the rectangle as event points. Store events in a priority queue, sorted by x-coordinate. For each event: • If it's a left edge, add it to an active set of rectangles. • If it's a right edge, remove it from the active set. • As new rectangles are added, check for overlaps with existing rectangles in the active set.

This reduces the overall time complexity to O(nlogn)O(n \log n).

Example

Consider the following rectangles:

R1:(1,1,3,3)R1: (1, 1, 3, 3)R2:(2,2,4,4)R2: (2, 2, 4, 4)R3:(4,4,5,5)R3: (4, 4, 5, 5)

Analyzing pairwise: • R1R1 overlaps with R2R2 since none of the non-overlapping conditions are met. • $ R1 $ and $ R3 $, as well as $ R2 $ and $ R3 $, do not overlap.

Visualization

To better conceptualize, let's summarize the non-overlapping conditions in a table:

ConditionExplanation
$R1.x_\&#123;\text\&#123;right\&#125;\&#125; \leq R2.x_\&#123;\text\&#123;left\&#125;\&#125;$$ R1 $ is to the left of R2R2
$R2.x_\&#123;\text\&#123;right\&#125;\&#125; \leq R1.x_\&#123;\text\&#123;left\&#125;\&#125;$$ R2 $ is to the left of R1R1
$R1.y_\&#123;\text\&#123;top\&#125;\&#125; \leq R2.y_\&#123;\text\&#123;bottom\&#125;\&#125;$$ R1 $ is below R2R2
$R2.y_\&#123;\text\&#123;top\&#125;\&#125; \leq R1.y_\&#123;\text\&#123;bottom\&#125;\&#125;$$ R2 $ is below R1R1

Edge Cases

Shared Edges: Two rectangles sharing an edge are not considered overlapping unless they enclose a region. • Degenerate Rectangles: Rectangles with zero height or width are line segments and are typically ignored in overlap checks.

Conclusion

Detecting rectangle overlap is a fundamental problem with wide-ranging applications. The choice of approach depends on the dataset size and specific needs, with more efficient algorithms like the sweep line method offering significant speed-ups over naive pair-checking. Understanding the logical underpinnings of overlap helps in both optimizing performance and correctly implementing solutions in applied computing fields.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.