Determine if two rectangles overlap each other?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Checking whether two rectangles overlap is a standard problem in geometry, UI layout, and game collision systems. The cleanest way to solve it is not to look for overlap directly, but to test the simpler cases where overlap is impossible.
Assume Axis-Aligned Rectangles
The common version of this problem assumes each rectangle is axis-aligned, meaning its sides are parallel to the x-axis and y-axis. A rectangle can be stored as four values:
- left x-coordinate
- bottom y-coordinate
- right x-coordinate
- top y-coordinate
For example, (0, 0, 4, 3) means the rectangle spans from x 0 to 4 and y 0 to 3.
The Non-Overlap Rule
Two axis-aligned rectangles do not overlap if at least one of these is true:
- one is completely to the left of the other
- one is completely above the other
That leads to a compact implementation:
This prints True because the rectangles share positive area.
Why the Comparison Operators Matter
The choice between < and <= determines whether touching edges count as overlap.
Using <= means touching at an edge or corner does not count as overlap:
That definition is common in collision detection where overlap means shared area. If your problem considers touching as a hit, replace the <= checks with <.
Normalize Input First If Needed
Real data is not always well-formed. Sometimes coordinates arrive in the wrong order. A small normalization step makes the function safer:
That avoids subtle bugs caused by rectangles defined from arbitrary drag directions.
A JavaScript Version
The same logic translates directly to other languages:
Once you understand the geometry, the language becomes almost irrelevant.
Extending the Idea
This rectangle test is often a building block. You can use it for:
- hit testing in drawing tools
- broad-phase collision checks in games
- window and widget layout calculations
- spatial filtering before more expensive geometry work
Because the algorithm uses only a few comparisons, it is extremely fast and easy to run at scale.
Common Pitfalls
The biggest pitfall is not defining whether touching counts as overlap. Different applications want different answers, so choose the comparison operators intentionally.
Another common issue is inconsistent coordinate conventions. Some systems use top-left plus width and height instead of two corners. Convert those values into a consistent rectangle representation before testing.
Developers also forget to normalize coordinates when rectangles can be created in reverse order. That can turn a correct algorithm into a wrong result.
Finally, this logic assumes axis-aligned rectangles. Rotated rectangles need a more advanced intersection test.
Summary
- The easiest way to test rectangle overlap is to check the cases where overlap is impossible.
- For axis-aligned rectangles, compare left, right, top, and bottom boundaries directly.
- Decide whether touching edges count as overlap before choosing
<or<=. - Normalize input coordinates if rectangle corners may arrive in arbitrary order.
- This simple test is fast and widely useful in graphics, layout, and collision systems.

