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.
————————————————————————————————————————
不废话,直接上代码,题目是让求总面积,无非是overlap不overlap情况,基于overlap rect的判断,简单之极。
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int total = (C-A)*(D-B) + (G-E)*(H-F);
//A,B -> bottomleft C,D topright; E,F bottomleft, G,H topright;
if(C <= E || G <= A)
return total;
if(B >= H || F>= D)
return total;
int i = Math.max(A,E);
int j = Math.min(C,G);
int p = Math.max(B,F);
int q = Math.min(D,H);
return total - (p-q) * (i-j);
}
}