-
Notifications
You must be signed in to change notification settings - Fork 1
/
quadtree.js
140 lines (135 loc) · 3.39 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
//implemented using "The Coding Train" video on QuadTree
//Daniel Shiffman
class Point {
constructor(x,y, user){
this.x = x;
this.y = y;
this.userData = user;
}
}
class Rectangle {
constructor(x,y,w,h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
contains(point){
return(
point.x >= this.x - this.w &&
point.x <= this.x + this.w &&
point.y >= this.y - this.h &&
point.y <= this.y + this.h
);
}
intersects(range) {
return !(
range.x - range.w > this.x + this.w ||
range.x + range.w < this.x - this.w ||
range.y - range.h > this.y + this.h ||
range.y + range.h < this.y - this.h
);
}
}
class Circle {
constructor(x,y,r){
this.x = x;
this.y = y;
this.r = r;
this.rSq = this.r*this.r;
}
contains(point){
let d = Math.pow(point.x - this.x,2) + Math.pow(point.y - this.y,2);
return d <= this.rSq;
}
intersects(range){
var xDist = Math.abs(range.x - this.x);
var yDist = Math.abs(range.y - this.y);
var r = this.r;
var w = range.w;
var h = range.h;
var edges = Math.pow(xDist - w,2) + Math.pow(yDist - h,2);
if (xDist > r + w || yDist > r + h) return false;
if (xDist <= w || yDist <= h) return true;
return edges <= this.rSq;
}
}
class QuadTree {
constructor(boundary, capacity){
this.boundary = boundary;
this.capacity = capacity;
this.points = [];
this.divided = false;
}
subdivide(){
let x = this.boundary.x;
let y = this.boundary.y;
let w = this.boundary.w / 2;
let h = this.boundary.h / 2;
let ne = new Rectangle(x + w, y - h, w, h);
this.northeast = new QuadTree(ne, this.capacity);
let nw = new Rectangle(x - w, y - h, w, h);
this.northwest = new QuadTree(nw, this.capacity);
let se = new Rectangle(x + w, y + h, w, h);
this.southeast = new QuadTree(se, this.capacity);
let sw = new Rectangle(x - w, y + h, w, h);
this.southwest = new QuadTree(sw, this.capacity);
this.divided = true;
}
insert(point){
if (!this.boundary.contains(point)) {
return false;
}
if (this.points.length < this.capacity) {
this.points.push(point);
return true;
}
if (!this.divided) {
this.subdivide();
}
if (
this.northeast.insert(point) ||
this.northwest.insert(point) ||
this.southeast.insert(point) ||
this.southwest.insert(point)
) {
return true;
}
}
query(range, found){
if (!found) {
found = [];
}
if (!range.intersects(this.boundary)) {
return found;
}
for (let p of this.points) {
if (range.contains(p)) {
found.push(p);
}
}
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;
}
// use for debugging
display(){
if(this.divided){
this.northwest.display();
this.northeast.display();
this.southwest.display();
this.southeast.display();
}
else{
rectMode(CENTER);
noFill();
strokeWeight(0.5);
stroke(255, 100);
rect(this.boundary.x,this.boundary.y,this.boundary.w*2,this.boundary.h*2);
}
}
}