-
Notifications
You must be signed in to change notification settings - Fork 1
/
capacity_quadtree.js
144 lines (119 loc) · 3.16 KB
/
capacity_quadtree.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class Boundary {
constructor(x, y, w, h) {
this.x1 = x;
this.y1 = y;
this.x2 = w;
this.y2 = h;
}
}
class QuadTree {
initialize(size, capacity = 120) {
const bound = new Boundary(0, 0, size, size);
this._initialize(bound, capacity);
}
_initialize(bound, capacity) {
this.boundary = bound;
this.capacity = capacity;
this.nw = null;
this.ne = null;
this.sw = null;
this.se = null;
this.points = [];
return this;
}
insert(point) {
var b = this.boundary;
if (b.x1 > point.x ||
b.x2 < point.x ||
b.y1 > point.y ||
b.y2 < point.y) {
return false;
}
if (b.x2 - b.x1 < 14 || (this.points.length < this.capacity && this.nw == null)) {
this.points.push(point);
return true;
} else {
if (this.nw == null) {
this.subdivide();
}
return this.nw.insert(point) ||
this.ne.insert(point) ||
this.sw.insert(point) ||
this.se.insert(point);
}
return false;
}
subdivide() {
var b = this.boundary;
var x = b.x1;
var y = b.y1;
var w = b.x2;
var h = b.y2;
var x_mid = (x + w) / 2;
var y_mid = (y + h) / 2;
var nw_boundary = new Boundary(x, y, x_mid, y_mid);
var ne_boundary = new Boundary(x_mid, y, w, y_mid);
var sw_boundary = new Boundary(x, y_mid, x_mid, h);
var se_boundary = new Boundary(x_mid, y_mid, w, h);
this.nw = new QuadTree()._initialize(nw_boundary, this.capacity);
this.ne = new QuadTree()._initialize(ne_boundary, this.capacity);
this.sw = new QuadTree()._initialize(sw_boundary, this.capacity);
this.se = new QuadTree()._initialize(se_boundary, this.capacity);
for (var i = 0; i < this.points.length; i++) {
this.nw.insert(this.points[i]) ||
this.ne.insert(this.points[i]) ||
this.sw.insert(this.points[i]) ||
this.se.insert(this.points[i]);
}
this.points = [];
}
findByRadius(point, radius) {
if (this.nw != null) {
return this.nw.findByRadius(point, radius).concat(
this.ne.findByRadius(point, radius),
this.sw.findByRadius(point, radius),
this.se.findByRadius(point, radius)
);
} else {
if (this._checkIntersectionWithCircle(point, radius, this.boundary)) {
var return_array = [];
for (var i = 0; i < this.points.length; i++) {
if (this.lengthTo(this.points[i], point) <= radius) {
return_array.push(this.points[i]);
}
}
return return_array;
} else {
return [];
}
}
}
_checkIntersectionWithCircle(point, radius, boundary) {
if (boundary.x2 < point.x - radius ||
boundary.x1 > point.x + radius ||
boundary.y1 > point.y + radius ||
boundary.y2 < point.y - radius
) {
return false;
}
return true;
}
lengthTo(
point1, //: Point,
point2 //: Point
) { //: number
const qX = (point1.x - point2.x) ** 2;
const qY = (point1.y - point2.y) ** 2;
return Math.sqrt(qX + qY);
}
clear() {
this.nw = null;
this.ne = null;
this.sw = null;
this.se = null;
this.points = [];
}
}
export {
QuadTree
};