-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculateEmptyArea.js
84 lines (74 loc) · 2.55 KB
/
calculateEmptyArea.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function intersection(containerx,containery,containerHeight,containerWidth,childx,childy,childHeight,childWidth){
containerleftx = containerx
containerlefty = containery
containerrightx = containerx+containerWidth
containerrighty = containery+containerHeight
childleftx = childx
childlefty = childy
childrightx = childx+childWidth
childrighty = childy+childHeight
intersect = true
if(containerrightx < childleftx)intersect = false
else if(containerrighty < childlefty)intersect = false
if(childrightx < containerleftx) intersect = false
else if(childrighty < containerlefty)intersect = false
var x5="",
y5="",
x6="",
y6="";
if(intersect === true){
x5 = Math.max(childleftx, containerleftx);
y5 = Math.max(childlefty, containerlefty);
x6 = Math.min(childrightx, containerrightx);
y6 = Math.min(containerrighty, childrighty);
area = (x6-x5)*(y6-y5)
return ((containerHeight*containerWidth) - area)
}
return (containerHeight*containerWidth);
}
function calculateEmptyArea(input){
var containerx = input['container']['coordinate']['X'],
containery = input['container']['coordinate']['Y'],
containerHeight = input['container']['height'],
containerWidth = input['container']['width'],
child = "",
childx =0,
childy =0,
childHeight =0,
childWidth =0,
childRadius=0;
for(var key in input){
if(key === 'circle'){
child = "circle";
childx = input[key]['center']['X'];
childy = input[key]['center']['Y'];
childRadius = input[key]['radius']
}
else if (key=="square"){
child = "square"
childx = input[key]['coordinate']['X']
childy = input[key]['coordinate']['Y']
childWidth = input[key]['width']
}else{
child = key
childx = input[key]['coordinate']['X']
childy = input[key]['coordinate']['Y']
childHeight = input[key]['height']
childWidth = input[key]['width']
}
}
if(child === "rectangle"){
check = intersection(containerx,containery,containerHeight,containerWidth,childx,childy,childHeight,childWidth);
}else if(child === 'square'){
check = intersection(containerx,containery,containerHeight,containerWidth,childx,childy,childWidth,childWidth);
}else {
var blx = childx - childRadius;
var bly = childy - childRadius;
check = intersection(containerx,containery,containerHeight,containerWidth,blx,bly,2*childRadius,2*childRadius);
}
console.log(check);
return check;
}
module.exports={
calculateEmptyArea
}