223. Rectangle Area

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.

做这道题感觉回到了高中。

var computeArea = function(A, B, C, D, E, F, G, H) {
    var s = (C-A) * (D-B) + (G-E) * (H-F); // areas of the two rectangle
    if(E>=C || F>=D || G<=A || H<=B)    return s; // no overlap
    return s - (Math.min(C,G) - Math.max(A,E)) * (Math.min(D,H) - Math.max(B,F)); //areas of the two rectangle - overlap
};

你可能感兴趣的:(223. Rectangle Area)