Determine if two rectangles overlap each other?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- Determine non-convex hull of collection of line segments
- Determine points within a given radius algorithm
- Determine the combinations of making change for a given amount
- Determining Floating Point Square Root
- Determining if a sphere intersects an object or not
- Determining if two rays intersect
- deterministic and reversible permutation function to store a message
- Difference between 2 numbers

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.