码迷,mamicode.com
首页 > 其他好文 > 详细

Rectangle Area

时间:2015-06-27 21:18:52      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

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.

技术分享

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

Code:

技术分享
 1 int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
 2             Rectangle rect1(A,C,B,D);
 3             Rectangle rect2(E,G,F,H);
 4             
 5             int areaRect1 = (rect1.right-rect1.left)*(rect1.top-rect1.bottom);
 6             int areaRect2 = (rect2.right-rect2.left)*(rect2.top-rect2.bottom);
 7             
 8             int maxLeft = max(rect1.left, rect2.left);
 9             int minRight = min(rect1.right, rect2.right);
10             int maxBottom = max (rect1.bottom, rect2.bottom);
11             int minTop = min(rect1.top, rect2.top);
12             int x = minRight-maxLeft;
13             int y = minTop-maxBottom;
14             if ( x > 0 && y > 0)
15                 return areaRect1+areaRect2-x*y;
16             else
17                 return areaRect1+areaRect2;
18     }
View Code

 

Rectangle Area

标签:

原文地址:http://www.cnblogs.com/happygirl-zjj/p/4604553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!