Problem:
Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
![Rectangle Area](https://leetcode.com/static/images/problemset/rectangle_area.png)
Assume that the total area is never beyond the maximum possible value of int
Credits:
Special thanks to @mithmatt for adding this problem, creating the above image and all test cases.
Answer:
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
//计算分离的两块长方形面积area1和area2
int area1 = Math.abs((C-A)*(D-B));
int area2 = Math.abs((E-G)*(F-H));
if(B>H || D<F || C<E || A>G) return area1+area2;
//确定叠加区域的两个X和两个Y
int maxX = Math.min(C,G);
int minX = Math.max(A,E);
int maxY = Math.min(D,H);
int minY = Math.max(B,F);
//计算叠加区域面积
int area = (maxX-minX)*(maxY-minY);
return area1+area2-area;
}
}