-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataStructures.js
140 lines (125 loc) · 4 KB
/
DataStructures.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
class QuadTreeNode
{
constructor(position, data)
{
this.position = position;
this.data = data;
}
}
class QuadTree
{
constructor(boundary, n)
{
this.boundary = boundary;
this.capacity = n;
this.points = [];
this.divided = false;
}
subdivide()
{
let x = this.boundary.position.x;
let y = this.boundary.position.y;
let w = Math.floor(this.boundary.width / 2) + 1;
let h = Math.floor(this.boundary.height / 2) + 1;
let nePos = new Vector2D(x + w , y - h);
let ne = new Rectangle({position : nePos, width : w, height : h});
this.northeast = new QuadTree(ne, this.capacity);
let nwPos = new Vector2D(x - w, y - h);
let nw = new Rectangle({position : nwPos, width : w, height : h});
this.northwest = new QuadTree(nw, this.capacity);
let sePos = new Vector2D(x + w , y + h);
let se = new Rectangle({position : sePos, width : w, height : h});
this.southeast = new QuadTree(se, this.capacity);
let swPos = new Vector2D(x - w, y + h);
let sw = new Rectangle({position : swPos, width : w, height : h});
this.southwest = new QuadTree(sw, this.capacity);
this.divided = true;
}
insert(point)
{
if (!this.boundary.contains(point.position))
return false;
if(this.points.length < this.capacity)
{
this.points.push(point);
return true;
}
else
{
if (!this.divided)
this.subdivide();
if(this.northeast.insert(point))
return true;
else if(this.northwest.insert(point))
return true;
else if(this.southeast.insert(point))
return true;
else if(this.southwest.insert(point))
return true;
}
}
query(range, found)
{
if (!found)
found = [];
if (!this.boundary.intersects(range))
return;
else
{
for (let p of this.points)
{
if (range.contains(p.position))
found.push(p.data);
}
if(this.divided)
{
this.northwest.query(range, found);
this.northeast.query(range, found);
this.southwest.query(range, found);
this.southeast.query(range, found);
}
}
return found;
}
display(render, camera, options)
{
if(options === undefined){options = {};}
if(options.displayBoundary)
this.boundary.display(render, camera);
if(!options.avoidPoints)
{
let pts = this.query(camera.getShape()) || [];
for(let p of pts)
{
if(typeof p.data.display === "function")
{
p.data.display(render, camera);
}
else if(camera.getShape().contains(p.position))
{
let sPos = camera.mapToScreen(p.position);
render._pInst.point(sPos.x, sPos.y);
}
}
}
if (this.divided)
{
this.northeast.display(render, camera, options);
this.northwest.display(render, camera, options);
this.southeast.display(render, camera, options);
this.southwest.display(render, camera, options);
}
}
get length()
{
let count = this.points.length;
if (this.divided)
{
count += this.northwest.length;
count += this.northeast.length;
count += this.southwest.length;
count += this.southeast.length;
}
return count;
}
}