Algorithm to detect intersection of two rectangles?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In computational geometry, detecting the intersection of two rectangles is a fundamental problem with applications in computer graphics, geographic information systems, and robotics, among other fields. This article delves into the algorithmic approach to determine whether two rectangles intersect, augmented by examples, technical explanations, and a summary table.
Rectangle Representation
Before discussing the intersection algorithm itself, we need to understand how rectangles are typically represented in a 2D coordinate system. A standard approach is to represent a rectangle by its lower-left and upper-right corners:
- Rectangle A: (Ax1, Ay1) for the lower-left corner and (Ax2, Ay2) for the upper-right corner.
- Rectangle B: (Bx1, By1) for the lower-left corner and (Bx2, By2) for the upper-right corner.
For both rectangles, it is assumed that:
- Ax1 < Ax2 and Ay1 < Ay2
- Bx1 < Bx2 and By1 < By2
Intersection Detection Algorithm
The intuitive approach to ascertain the overlap of two rectangles can be encapsulated in an algorithm that primarily checks if one rectangle is positioned entirely to the left, right, above, or below the other rectangle:
Explanation
- Horizontal Non-overlap:
The conditionAx2 < Bx1checks if Rectangle A is completely to the left of Rectangle B, whileBx2 < Ax1checks if Rectangle B is completely to the left of Rectangle A. - Vertical Non-overlap:
The conditionAy2 < By1checks if Rectangle A is completely below Rectangle B, andBy2 < Ay1checks if Rectangle B is completely below Rectangle A.
If neither of the horizontal or vertical non-overlap conditions holds, the rectangles must intersect.
Example
Consider the following rectangles:
- Rectangle A: (1, 1), (4, 4)
- Rectangle B: (3, 3), (5, 5)
Using the algorithm, we find:
Ax2 (4) >= Bx1 (3)andBx2 (5) >= Ax1 (1): No horizontal gap.Ay2 (4) >= By1 (3)andBy2 (5) >= Ay1 (1): No vertical gap.
Since neither rectangle is completely detached from the other, they intersect.
Computational Complexity
The algorithm executes in constant time, , as it merely involves simple comparisons — making it extremely efficient for real-time applications.
Special Cases
- Touching Edges or Corners: The algorithm also successfully detects intersection when rectangles only touch at edges or corners, as these scenarios do not render any of the non-overlap conditions true.
Applications
- Collision Detection in Gaming: Efficiently detect intersections of game elements (e.g., player entities and obstacles).
- Window Systems in GUI: Determine overlapping windows for rendering decisions.
- Geographic Information Systems (GIS): Identify overlapping geographic plots.
Summary Table
| Key Point | Description |
| Representation | Rectangles defined by two opposite vertices. |
| Algorithm Steps | Check for horizontal and vertical non-overlap conditions. |
| Complexity | , efficient for real-time applications. |
| Edge Cases | Handles touching edges/corners as intersections. |
| Applications | Game development, GUI systems, GIS, etc. |
In summary, detecting rectangle intersections is straightforward and computationally trivial, enabling its integration into various applications that demand real-time geometric computations.

