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.
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 :
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:
- : is completely to the left of .
- : is completely to the left of .
- : is completely below .
- : is completely below .
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 , which may be inefficient for large datasets.
Optimized Approach
One optimization involves sorting and then scanning:
- 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.
- 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 .
Example
Consider the following rectangles:
• • •
Analyzing pairwise:
• overlaps with 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:
| Condition | Explanation |
$R1.x_\{\text\{right\}\} \leq R2.x_\{\text\{left\}\}$ | $ R1 $ is to the left of |
$R2.x_\{\text\{right\}\} \leq R1.x_\{\text\{left\}\}$ | $ R2 $ is to the left of |
$R1.y_\{\text\{top\}\} \leq R2.y_\{\text\{bottom\}\}$ | $ R1 $ is below |
$R2.y_\{\text\{top\}\} \leq R1.y_\{\text\{bottom\}\}$ | $ R2 $ is below |
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
- Given a sorted array, can we build a sorted array of the sums of all pairs in On2?
- Given a sorted integer array, how may Binary Search trees can be formed from it?
- Given a string, find two identical subsequences with consecutive indexes C
- Given a string of a million numbers, return all repeating 3 digit numbers
- Given a string of numbers and a number of multiplication operators, what is the highest number one can calculate?
- given an array of integers in random order you have to find the minimum number of swaps to convert it to cyclic sorted array
- Given a word, convert it into a palindrome with minimum addition of letters to it
- Given an array, can I find in On the longest range, whose endpoints are the greatest values in the range?

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 courseTrack 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.