-
Notifications
You must be signed in to change notification settings - Fork 1
/
skyline.js
90 lines (87 loc) · 3.85 KB
/
skyline.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
85
86
87
88
89
90
(function(){
skyline.build = function(buildings){
var result = [],
numBuildings = buildings.length,
emptySkyline = true;
for(var i = 0; i < numBuildings; i++){
var building = buildings[i];
if(building[0] === building[1]){
//ignore buildings of zero width
continue;
}
var x1InsertIndex = skyline.utils.array.binarySearch(building[0],result);
var x2InsertIndex = skyline.utils.array.binarySearch(building[1],result);
if(emptySkyline){
//first building
result[0] = new skyline.Interval(building[0],building[1],building[2]);
emptySkyline = false;
} else if(x1InsertIndex < 0 && x2InsertIndex < 0){
//no overlap
result.splice(-x2InsertIndex, 0, new skyline.Interval(building[0],building[1],building[2]));
} else if(x1InsertIndex >= 0 && x2InsertIndex < 0){
//single edge overlap
var splitInterval = result[x1InsertIndex];
var i1 = new skyline.Interval(splitInterval.x1,building[0],splitInterval.h);
var i2 = new skyline.Interval(building[0],splitInterval.x2,Math.max(splitInterval.h, building[2]));
var i3 = new skyline.Interval(splitInterval.x2,building[1],building[2]);
if(i1.x1 === i1.x2){
//get rid of leading 0 area intervals
result[x1InsertIndex] = i2;
result.splice(x1InsertIndex + 1, 0, i3);
} else if(i2.x1 === i2.x2){
//get rid of 0 area intervals caused by touching buildings
result[x1InsertIndex] = i1;
result.splice(x1InsertIndex + 1, 0, i3);
} else {
result[x1InsertIndex] = i1;
result.splice(x1InsertIndex + 1, 0, i2);
result.splice(x1InsertIndex + 2, 0, i3);
}
} else if(x1InsertIndex >= 0 && x2InsertIndex >= 0 && x1InsertIndex === x2InsertIndex){
//multisplit single building
var splitInterval = result[x1InsertIndex];
var i1 = new skyline.Interval(splitInterval.x1,building[0],splitInterval.h);
var i2 = new skyline.Interval(building[0],building[1],Math.max(splitInterval.h, building[2]));
var i3 = new skyline.Interval(building[1],splitInterval.x2,splitInterval.h);
if(i1.x1 === i1.x2 && i3.x1 === i3.x2){
//building ontop of building
result[x1InsertIndex] = i2;
} else if(i1.x1 === i1.x2){
//get rid of leading 0 area intervals
result[x1InsertIndex] = i2;
result.splice(x1InsertIndex + 1, 0, i3);
} else if(i3.x1 === i3.x2){
//get rid of trailing 0 area intervals
result[x1InsertIndex] = i1;
result.splice(x1InsertIndex + 1, 0, i2);
} else {
result[x1InsertIndex] = i1;
result.splice(x1InsertIndex + 1, 0, i2);
result.splice(x1InsertIndex + 2, 0, i3);
}
} else if(x1InsertIndex >= 0 && x2InsertIndex > 0){
//multisplit multibuildings
var splitInterval1 = result[x1InsertIndex];
var i11 = new skyline.Interval(splitInterval1.x1,building[0],splitInterval1.h);
var i12 = new skyline.Interval(building[0],splitInterval1.x2,Math.max(splitInterval1.h, building[2]));
result[x1InsertIndex] = i11;
var splitInterval2 = result[x2InsertIndex];
var i21 = new skyline.Interval(splitInterval2.x1,building[1],splitInterval1.h);
var i22 = new skyline.Interval(building[1],splitInterval2.x2,Math.max(splitInterval2.h, building[2]));
result[x2InsertIndex] = i21;
result.splice(x1InsertIndex + 1, 0, i12);
result.splice(x2InsertIndex + 2, 0, i22);
}
}
return result;
}
skyline.findArea = function(buildings){
var intervals = skyline.build(buildings),
numIntervals = intervals.length,
area = 0;
for(var i = 0; i < numIntervals; i++ ){
area += intervals[i].area();
}
return area;
};
})();