geometry
rectangles
perimeter
area
mathematics

Calculate the perimeter and area of intersecting rectangles?

Master System Design with Codemia

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

Introduction

With two axis-aligned rectangles, the intersection is easy to describe once you know the left, right, bottom, and top edges. The main trick is to separate four different quantities that people often mix together: each rectangle's own area and perimeter, the overlap area, and the perimeter of the union shape.

Represent the Rectangles Clearly

Assume each rectangle is axis-aligned and stored as:

  • 'left'
  • 'bottom'
  • 'right'
  • 'top'

For a valid rectangle, right > left and top > bottom.

The width and height are:

  • 'width = right - left'
  • 'height = top - bottom'

So a single rectangle has:

  • 'area = width * height'
  • 'perimeter = 2 * (width + height)'

Compute the Overlap First

The intersection, if it exists, is also an axis-aligned rectangle. Its edges are:

  • 'left = max(r1.left, r2.left)'
  • 'right = min(r1.right, r2.right)'
  • 'bottom = max(r1.bottom, r2.bottom)'
  • 'top = min(r1.top, r2.top)'

From that, the overlap dimensions are:

  • 'overlapWidth = max(0, right - left)'
  • 'overlapHeight = max(0, top - bottom)'

Then the overlap area is:

  • 'overlapArea = overlapWidth * overlapHeight'

If either dimension is zero, the rectangles do not overlap in area. They may still touch on an edge or at a corner.

Area of the Combined Shape

The union area is the easy part:

unionArea = area1 + area2 - overlapArea

You subtract the overlap once because it was counted twice when you added the two rectangle areas.

Here is a runnable Python example:

python
1from dataclasses import dataclass
2
3
4@dataclass
5class Rect:
6    left: float
7    bottom: float
8    right: float
9    top: float
10
11    @property
12    def width(self) -> float:
13        return self.right - self.left
14
15    @property
16    def height(self) -> float:
17        return self.top - self.bottom
18
19    @property
20    def area(self) -> float:
21        return self.width * self.height
22
23    @property
24    def perimeter(self) -> float:
25        return 2 * (self.width + self.height)
26
27
28def overlap_dimensions(a: Rect, b: Rect) -> tuple[float, float]:
29    overlap_width = max(0.0, min(a.right, b.right) - max(a.left, b.left))
30    overlap_height = max(0.0, min(a.top, b.top) - max(a.bottom, b.bottom))
31    return overlap_width, overlap_height
32
33
34r1 = Rect(1, 1, 4, 5)
35r2 = Rect(2, 3, 5, 6)
36ow, oh = overlap_dimensions(r1, r2)
37overlap_area = ow * oh
38union_area = r1.area + r2.area - overlap_area
39
40print(overlap_area)  # 4.0
41print(union_area)    # 17.0

Perimeter of the Union

This is where bad formulas often appear. The perimeter of the combined outline is not the same as the perimeter of the overlap rectangle, and you do not need to trace every corner by hand for the two-rectangle, axis-aligned case.

For two axis-aligned rectangles, the union perimeter is:

unionPerimeter = perimeter1 + perimeter2 - 2 * overlapWidth - 2 * overlapHeight

Why does that work? Each overlapped horizontal run removes boundary from both rectangles, so it contributes 2 * overlapWidth to the amount you subtract. The same logic applies to the vertical run, giving 2 * overlapHeight.

Using the same example:

  • rectangle 1 perimeter is 14
  • rectangle 2 perimeter is 12
  • overlap width is 2
  • overlap height is 2

So:

14 + 12 - 2 * 2 - 2 * 2 = 18

That is the perimeter of the outside boundary of the union.

We can extend the Python example:

python
union_perimeter = r1.perimeter + r2.perimeter - 2 * ow - 2 * oh
print(union_perimeter)  # 18.0

Perimeter and Area of the Intersection Itself

Sometimes the question is not about the union, but about the overlap rectangle itself. In that case:

  • 'intersectionArea = overlapWidth * overlapHeight'
  • 'intersectionPerimeter = 2 * (overlapWidth + overlapHeight)'

That only applies when both overlap dimensions are positive. If the rectangles only touch on an edge, the overlap area is zero and there is no rectangular intersection with positive area.

Common Pitfalls

The most common mistake is mixing up the intersection perimeter and the union perimeter. They are different quantities and use different formulas.

Another mistake is forgetting to clamp overlap width and height with max(0, ...). Without that step, disjoint rectangles can produce negative dimensions and nonsense results.

People also assume the area formula and perimeter formula have the same subtraction pattern. They do not. Area subtracts the overlap area once, while union perimeter subtracts the overlapped horizontal and vertical boundary lengths.

Finally, these formulas assume axis-aligned rectangles. Rotated rectangles need a different geometric treatment because the overlap is no longer guaranteed to be a rectangle with edges parallel to the axes.

Summary

  • Compute overlap width and height first using max and min.
  • Intersection area is overlapWidth * overlapHeight.
  • Union area is area1 + area2 - overlapArea.
  • Union perimeter for two axis-aligned rectangles is p1 + p2 - 2 * overlapWidth - 2 * overlapHeight.
  • Do not confuse the overlap rectangle's perimeter with the perimeter of the combined outer shape.

Course illustration
Course illustration

All Rights Reserved.