-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathrectangle-area.js
32 lines (30 loc) · 930 Bytes
/
rectangle-area.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 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.
//
//
// Credits:Special thanks to @mithmatt for adding this problem, creating the above image and all test cases.
/**
* @param {number} A
* @param {number} B
* @param {number} C
* @param {number} D
* @param {number} E
* @param {number} F
* @param {number} G
* @param {number} H
* @return {number}
*/
var computeArea = function(A, B, C, D, E, F, G, H) {
var width = (Math.min(C, G) - Math.max(A, E));
var height = (Math.min(D, H) - Math.max(B, F));
var cover = width * height;
if (G <= A || D <= F || B >= H || E >= C) {
cover = 0;
}
return ((C - A) * (D - B)) + ((G - E) * (H - F)) - cover;
};