geometry
rectangles
overlap
spatial analysis
computational geometry

How much do two rectangles overlap?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Understanding the overlap between two rectangles is a fundamental problem in computational geometry, with applications ranging from computer graphics to geographic information systems and collision detection in video games. This article explains how to determine the overlap area between two axis-aligned rectangles, provides formulas, and walks through concrete examples.

Defining Axis-Aligned Rectangles

An axis-aligned rectangle has edges parallel to the coordinate axes. Each rectangle is defined by its top-left and bottom-right coordinates:

  • Rectangle A: (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2)
  • Rectangle B: (x3,y3)(x_3, y_3) to (x4,y4)(x_4, y_4)

Where x1<x2x_1 < x_2, y1<y2y_1 < y_2 for Rectangle A, and x3<x4x_3 < x_4, y3<y4y_3 < y_4 for Rectangle B.

Detecting Overlap

For two rectangles to overlap, their projections on both the x-axis and the y-axis must intersect:

  • In the x-dimension: max(x1,x3)<min(x2,x4)\max(x_1, x_3) < \min(x_2, x_4)
  • In the y-dimension: max(y1,y3)<min(y2,y4)\max(y_1, y_3) < \min(y_2, y_4)

If both conditions hold, the rectangles overlap. If either condition fails, they do not.

Calculating the Overlapping Area

When the rectangles do overlap, the intersection forms another rectangle whose corners are:

  • Left: max(x1,x3)\max(x_1, x_3)
  • Right: min(x2,x4)\min(x_2, x_4)
  • Top: max(y1,y3)\max(y_1, y_3)
  • Bottom: min(y2,y4)\min(y_2, y_4)

The overlapping area is:

Aoverlap=max(0,min(x2,x4)max(x1,x3))×max(0,min(y2,y4)max(y1,y3))A_{\text{overlap}} = \max(0, \min(x_2, x_4) - \max(x_1, x_3)) \times \max(0, \min(y_2, y_4) - \max(y_1, y_3))

The max(0,)\max(0, \ldots) terms ensure the result is zero when the rectangles do not overlap, making this a single formula that handles both cases.

Code Implementation

python
1def overlap_area(x1, y1, x2, y2, x3, y3, x4, y4):
2    """Calculate overlap area of two axis-aligned rectangles."""
3    dx = max(0, min(x2, x4) - max(x1, x3))
4    dy = max(0, min(y2, y4) - max(y1, y3))
5    return dx * dy

Worked Examples

Example 1: No Overlap

  • Rectangle A: (1,1)(1, 1) to (2,2)(2, 2)
  • Rectangle B: (3,3)(3, 3) to (4,4)(4, 4)

Check x-overlap: max(1,3)=3\max(1, 3) = 3 and min(2,4)=2\min(2, 4) = 2. Since 3>23 > 2, there is no overlap.

Result: Aoverlap=0A_{\text{overlap}} = 0

Example 2: Partial Overlap

  • Rectangle A: (1,1)(1, 1) to (4,4)(4, 4)
  • Rectangle B: (2,2)(2, 2) to (6,6)(6, 6)

The intersection rectangle is (2,2)(2, 2) to (4,4)(4, 4):

Aoverlap=(42)×(42)=4A_{\text{overlap}} = (4 - 2) \times (4 - 2) = 4

Example 3: Full Containment

  • Rectangle A: (0,0)(0, 0) to (10,10)(10, 10)
  • Rectangle B: (2,3)(2, 3) to (5,7)(5, 7)

The intersection is just Rectangle B itself:

Aoverlap=(52)×(73)=12A_{\text{overlap}} = (5 - 2) \times (7 - 3) = 12

Computing Intersection over Union (IoU)

In computer vision and object detection, the Intersection over Union metric is the standard way to quantify how much two bounding boxes overlap:

IoU=AoverlapA1+A2Aoverlap\text{IoU} = \frac{A_{\text{overlap}}}{A_1 + A_2 - A_{\text{overlap}}}

where A1A_1 and A2A_2 are the areas of the two rectangles. IoU ranges from 0 (no overlap) to 1 (identical rectangles).

Use Cases

Computer Graphics

In rendering pipelines, clipping algorithms need to detect visible regions by calculating the overlapping areas of bounding boxes. Only the overlapping portion needs to be rasterized.

Collision Detection

Physics engines use bounding box overlap as a fast first pass before more expensive polygon-level collision checks. If the bounding boxes do not overlap, the objects cannot collide.

Geographic Information Systems (GIS)

Overlap calculations determine intersecting land parcels, administrative regions, or sensor coverage areas for spatial analysis.

Summary

ConceptFormula or Condition
Overlap existsmax(x1,x3)<min(x2,x4)\max(x_1,x_3) < \min(x_2,x_4) AND max(y1,y3)<min(y2,y4)\max(y_1,y_3) < \min(y_2,y_4)
Overlap areamax(0,min(x2,x4)max(x1,x3))×max(0,min(y2,y4)max(y1,y3))\max(0, \min(x_2,x_4) - \max(x_1,x_3)) \times \max(0, \min(y_2,y_4) - \max(y_1,y_3))
IoUAoverlapA1+A2Aoverlap\frac{A_{\text{overlap}}}{A_1 + A_2 - A_{\text{overlap}}}

Determining the overlap between two rectangles is both practical and elegant. The formula relies on simple min/max operations, runs in O(1)O(1) time, and generalizes naturally to higher dimensions by adding one overlap check per axis.


Course illustration
Course illustration

All Rights Reserved.