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.

Assume that the total area is never beyond the maximum possible value of int.
首先检测两个矩形的相交情况,有重叠、边缘相切、无重合。
相交的时候相交面积,取左端最大右端最小,上端最低下端最高。
public static int computeArea(int A, int B, int C, int D, int E, int F, int G,int H)
{
long left=Math.max(A, E);
long right=Math.min(C, G);
long top=Math.min(D, H);
long buttom=Math.max(B, F);
long sum=0;
if(left==right||top==buttom)
return (C-A)*(D-B)+(G-E)*(H-F);
if(right-left<0||top-buttom<0)
sum=0;
else {
sum+=(right-left)*(top-buttom);
}
return (int) ((C-A)*(D-B)+(G-E)*(H-F)-sum);
}