Find the total area covered by and rectilinear rectangles in a 2D plane.
Each rectangle was defined by its bottom left corner and top right corner as shown in the figure.
Assume the total area is never beyond the maximum possible value of int.
Idea: Need to calculate the area of the square cover, the key is to calculate the area of the coincident area, using the following calculation method;
On the left side of the coincident area (bottom) (right, top)
left = Math.max (A, E);
Bottom = Math.max (B, F);
The calculations for right and top are complex, and two square disjoint situations need to be considered, such as: [( -2,-2) (2,2)],[(3,3)],
right = Math.max (Math.min (C, G), left);
top = Math.max (Math.min (D, H), bottom);
The code is as follows:
Public intComputearea (intAintBintCintDintEintFintGintH) {intleft =Math.max (A, E); intright =Math.max (Math.min (C, G), left); intBottom =Math.max (B, F); inttop =Math.max (Math.min (D, H), bottom); return(G-E) * (h-f) + (c-a) * (d-b)-(right-left) * (top-bottom); }
Leetcode 223-rectangle Area