-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathQuadtree.js
192 lines (166 loc) · 5.04 KB
/
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
function Point(x, y){
this.x = x;
this.y = y;
this.col = color(random(255), random(255), random(255));
this.update = function(){
this.x = this.x + random(-1, 1);
this.y = this.y + random(-1, 1);
if(this.x > width) this.x = 1;
else if(this.x < 0) this.x = width - 1;
if(this.y > height) this.y = 1;
else if(this.y < 0) this.y = height -1;
}
this.show = function(){
point(this.x, this.y);
}
}
function Rectangle(x, y, w, h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
// Return true if this rectangle contains point
this.contains = function(point){
return (point.x >= this.x - this.w/2 &&
point.x <= this.x + this.w/2 &&
point.y > this.y - this.h/2 &&
point.y < this.y + this.h/2);
}
// Return true if this rectangle overlap range
this.intersects = function(range){
return !(range.x + range.w/2 < this.x - this.w/2 ||
range.y + range.h/2 < this.y - this.h/2 ||
range.x - range.w/2 > this.x + this.w/2 ||
range.y - range.h/2 > this.y + this.h/2 );
}
}
function Circle(x, y, r){
this.x = x;
this.y = y;
this.r = r;
this.contains = function(point){
return (dist(point.x, point.y, this.x, this.y) < this.r);
}
this.intersects = function(range){
var xDist = Math.abs(range.x - this.x);
var yDist = Math.abs(range.y - this.y);
// radius of the circle
var r = this.r;
var w = range.w;
var h = range.h;
var edges = dist(xDist, yDist, w, h);
// no intersection
if (xDist > (r + w) || yDist > (r + h))
return false;
// intersection within the circle
if (xDist <= w || yDist <= h)
return true;
// intersection on the edge of the circle
return edges <= this.rSquared;
}
}
function QuadTree(boundary, capacity){
/*Axis-aligned bounding box stored as a center with half-dimensions
to represent the boundaries of this quad tree*/
this.boundary = boundary;
// Arbitrary constant to indicate how many elements can be stored in this quad tree
this.capacity = capacity;
// Points in this quad tree
this.points = [];
// This Quad tree is already have children ?
this.divided = false;
// Create children for this Quad tree
this.subdivice = function(){
var x = this.boundary.x;
var y = this.boundary.y;
var w = this.boundary.w/2;
var h = this.boundary.h/2;
// Create boundary for all four children
var tl = new Rectangle(x - w/2, y - h/2, w, h);
var tr = new Rectangle(x + w/2, y - h/2, w, h);
var bl = new Rectangle(x - w/2, y + h/2, w, h);
var br = new Rectangle(x + w/2, y + h/2, w, h);
// Children
this.topLeft = new QuadTree(tl, this.capacity);
this.topRight = new QuadTree(tr, this.capacity);
this.botLeft = new QuadTree(bl, this.capacity);
this.botRight = new QuadTree(br, this.capacity);
this.divided = true;
}
// Insert a point into the QuadTree
this.insert = function(newPoint){
// Ignore objects that do not belong in this quad tree
if(!this.boundary.contains(newPoint)){
return false;
}
// If there is space in this quad tree, add the object here
if(this.points.length < this.capacity){
this.points.push(newPoint);
return true;
}
/* Otherwise, subdivide and then add the point
to whichever node will accept it*/
if(!this.divided){
this.subdivice();
}
if (this.topLeft.insert(newPoint) || this.topRight.insert(newPoint) ||
this.botLeft.insert(newPoint) || this.botRight.insert(newPoint)) {
return true;
}
/* Otherwise, the point cannot be inserted for some
unknown reason (this should never happen)*/
return false;
}
// Find all points that inside range
this.query = function(range, pointInRange){
// Prepare an array of results
if (!pointInRange) {
pointInRange = [];
}
// Automatically abort if the range does not intersect this quad
if (!range.intersects(this.boundary)) {
return pointInRange;
}
// Check objects at this quad level
for (var i = 0; i < this.points.length; i++) {
if (range.contains(this.points[i])) {
pointInRange.push(this.points[i]);
}
}
// Add the points from the children if there are already have children
if (this.divided) {
this.topLeft.query(range, pointInRange);
this.topRight.query(range, pointInRange);
this.botLeft.query(range, pointInRange);
this.botRight.query(range, pointInRange);
}
return pointInRange;
}
this.show = function(showWhat) {
if(showWhat != 'points at mouse'){
stroke(100);
if (showWhat == 'points' || showWhat == 'grid+points'){
strokeWeight(4);
for (var i = 0; i < this.points.length; i++) {
this.points[i].show();
}
}
if (showWhat == 'grid' || showWhat == 'grid+points'){
noFill();
if(this.points.length > 0){
stroke(255);
strokeWeight(2);
} else strokeWeight(1);
var newPos = ConvertXY(this.boundary.x, this.boundary.y);
rect(newPos.x, newPos.y, this.boundary.w, this.boundary.h);
//rect(this.boundary.x, this.boundary.y, this.boundary.w, this.boundary.h);
}
if (this.divided) {
this.topLeft.show(showWhat);
this.topRight.show(showWhat);
this.botLeft.show(showWhat);
this.botRight.show(showWhat);
}
}
}
}