axis‐aligned rectangles intersection
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
For axis-aligned rectangles, intersection testing is much simpler than for rotated shapes. Because the sides stay parallel to the axes, you can decide the 2D overlap question by checking overlap on the x axis and on the y axis separately.
That makes the implementation short and reliable. The main design choice is not the math but the definition: do touching edges count as intersection, or do you require positive overlapping area?
Represent the Rectangle Clearly
A good representation stores the left, bottom, right, and top boundaries.
Before using the rectangle, make sure the values are normalized so left <= right and bottom <= top. If the inputs come from dragging in a UI or from unsanitized geometry data, normalization should happen once up front.
Detect Non-Overlap First
The easiest logic is to ask when rectangles definitely do not intersect:
- one is entirely left of the other
- one is entirely right of the other
- one is entirely above the other
- one is entirely below the other
If none of those cases is true, the rectangles intersect.
This version treats edge-touching as not intersecting because it uses <=.
Compute the Overlapping Rectangle
If you want the actual intersection area, compute the overlapping bounds directly:
The result is the rectangle shared by both inputs, or None if there is no positive-area overlap.
Decide Whether Edge Touching Counts
Some applications treat a shared edge or corner as an intersection. Others do not.
If touching should count, change the strictness of the separating conditions:
This is not a minor cosmetic detail. It changes collision behavior, layout behavior, and spatial query results, so the rule should be explicit in the API.
Why the 1D View Works
On the x axis, a rectangle is just an interval. The same is true on the y axis. Two axis-aligned rectangles intersect in 2D exactly when their intervals overlap on both axes.
That is why you do not need line-segment intersection logic, corner-inclusion checks, or polygon algorithms here. The axis alignment removes all of that complexity.
Normalize Inputs When Needed
If a rectangle might be defined with reversed corners, normalize it once:
This avoids subtle bugs where seemingly correct intersection code produces nonsense because the input contract was violated.
Common Pitfalls
A common mistake is forgetting to define whether touching edges count as intersection. Another is using corner-containment logic instead of interval overlap checks; that approach can miss valid cases where neither rectangle contains a corner of the other. Developers also sometimes skip coordinate normalization and then blame the intersection code for invalid input. For axis-aligned rectangles, the interval-based method is both simpler and more reliable.
Summary
- Axis-aligned rectangle intersection reduces to overlap checks on the
xandyaxes. - The cleanest implementation detects non-overlap first and negates it.
- The overlap rectangle comes from
maxof the lower bounds andminof the upper bounds. - Be explicit about whether touching edges count.
- Normalize rectangle coordinates if the input source may produce reversed corners.

