Rectangle Area
From LeetCode. Union area of 2 rectangles. Solve the geometry problem "Rectangle Area".
Examples
Input: [0,0,2,2,2,3,4,5]
Output: 8
Input: [3,0,5,2,5,3,7,5]
Output: 8
Hints
Use inclusion-exclusion: union area = area(A) + area(B) - area(overlap). Compute each rectangle's area from its width and height.
If the rectangles overlap, the overlap region is itself a rectangle. Its width = max(0, min(ax2, bx2) - max(ax1, bx1)), and height = max(0, min(ay2, by2) - max(ay1, by1)).
If max(ax1, bx1) >= min(ax2, bx2) or max(ay1, by1) >= min(ay2, by2) on either axis, overlap area is 0 and the union is simply the sum of both areas.
Related Problems
Rectangle Area
**From LeetCode.** Union area of 2 rectangles. Solve the geometry problem "Rectangle Area".