geometry
algorithm
computational geometry
intersections
axis-aligned rectangles

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.

python
1from dataclasses import dataclass
2
3@dataclass
4class Rect:
5    left: float
6    bottom: float
7    right: float
8    top: float

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.

python
1from dataclasses import dataclass
2
3@dataclass
4class Rect:
5    left: float
6    bottom: float
7    right: float
8    top: float
9
10
11def intersects(a: Rect, b: Rect) -> bool:
12    return not (
13        a.right <= b.left or
14        b.right <= a.left or
15        a.top <= b.bottom or
16        b.top <= a.bottom
17    )
18
19
20r1 = Rect(1, 1, 5, 4)
21r2 = Rect(3, 2, 7, 6)
22print(intersects(r1, r2))

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:

python
1from typing import Optional
2
3
4def intersection(a: Rect, b: Rect) -> Optional[Rect]:
5    left = max(a.left, b.left)
6    right = min(a.right, b.right)
7    bottom = max(a.bottom, b.bottom)
8    top = min(a.top, b.top)
9
10    if left >= right or bottom >= top:
11        return None
12
13    return Rect(left, bottom, right, top)
14
15
16print(intersection(r1, r2))

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:

python
1def intersects_touching_counts(a: Rect, b: Rect) -> bool:
2    return not (
3        a.right < b.left or
4        b.right < a.left or
5        a.top < b.bottom or
6        b.top < a.bottom
7    )

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:

python
1def normalize(rect: Rect) -> Rect:
2    return Rect(
3        left=min(rect.left, rect.right),
4        bottom=min(rect.bottom, rect.top),
5        right=max(rect.left, rect.right),
6        top=max(rect.bottom, rect.top),
7    )

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 x and y axes.
  • The cleanest implementation detects non-overlap first and negates it.
  • The overlap rectangle comes from max of the lower bounds and min of the upper bounds.
  • Be explicit about whether touching edges count.
  • Normalize rectangle coordinates if the input source may produce reversed corners.

Course illustration
Course illustration

All Rights Reserved.