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:
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:
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
maxandmin. - 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.

