您的位置:首页 > 其它

Rectangle Area

2015-06-27 19:53 288 查看
Description:

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.

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
Rectangle rect1(A,C,B,D);
Rectangle rect2(E,G,F,H);

int areaRect1 = (rect1.right-rect1.left)*(rect1.top-rect1.bottom);
int areaRect2 = (rect2.right-rect2.left)*(rect2.top-rect2.bottom);

int maxLeft = max(rect1.left, rect2.left);
int minRight = min(rect1.right, rect2.right);
int maxBottom = max (rect1.bottom, rect2.bottom);
int minTop = min(rect1.top, rect2.top);
int x = minRight-maxLeft;
int y = minTop-maxBottom;
if ( x > 0 && y > 0)
return areaRect1+areaRect2-x*y;
else
return areaRect1+areaRect2;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: