Rectangle Area
Source
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.
java
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);
if(overlap(A, C, E, G)>0 && overlap(B, D, F, H)>0){
total -=overlap(A, C, E, G)*overlap(B, D, F, H);
}
return total;
}
public int overlap(int s1, int e1, int s2, int e2){
if(e1<s2 || e2<s1){
return 0;
}
return Math.min(e1, e2) - Math.max(s1, s2);
}
}