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.

Rectangle Area

Assume that the total area is never beyond the maximum possible value of int.

Runtime: 40ms.

 1 class Solution {
 2 public:
 3     int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
 4         int s1 = (C - A) * (D - B);
 5         int s2 = (G - E) * (H - F);
 6         
 7         int x = min(C, G) <= max(E, A) ? 0 : min(C, G) - max(E, A);
 8         int y = min(D, H) <= max(B, F) ? 0 : min(D, H) - max(B, F); 
 9         
10         return s1 + s2 - x * y;
11     }
12 };

 

posted @ 2015-09-12 05:13  amazingzoe  阅读(99)  评论(0编辑  收藏  举报