[LeetCode][JavaScript]Rectangle Area

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.

https://leetcode.com/problems/rectangle-area/

 

 


 

 

总面积减去重叠的面积,可能没有重叠。

 1 /**
 2  * @param {number} A
 3  * @param {number} B
 4  * @param {number} C
 5  * @param {number} D
 6  * @param {number} E
 7  * @param {number} F
 8  * @param {number} G
 9  * @param {number} H
10  * @return {number}
11  */
12 var computeArea = function(A, B, C, D, E, F, G, H) {
13     var result = (C - A) * (D - B) + (G - E) * (H - F);
14     var topRightX = Math.min(C, G);
15     var topRightY = Math.min(D, H);
16     var bottomLeftX = Math.max(A, E);
17     var bottomLeftY = Math.max(B, F);
18     if(topRightX >= bottomLeftX && topRightY >= bottomLeftY){
19         return result - (topRightX - bottomLeftX) * (topRightY - bottomLeftY);
20     }
21     return result;
22 };

 

posted @ 2015-06-10 23:24  `Liok  阅读(257)  评论(0编辑  收藏  举报