Skip to content

Commit 589cd83

Browse files
author
Chris Hallberg
authored
Merge branch 'master' into test-size-to-length
2 parents 050ca7a + 2db3c49 commit 589cd83

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

examples/visualize_qtree/sketch.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ function setup() {
2020

2121
function draw() {
2222
background(0);
23-
show(qtree);
2423

25-
// let range = new Rectangle(mouseX, mouseY, 25, 25);
2624
let range = new Circle(mouseX, mouseY, 64);
25+
show(qtree, range);
26+
stroke("pink");
2727
ellipse(range.x, range.y, range.r * 2);
2828

2929
let points = qtree.query(range);
@@ -32,31 +32,37 @@ function draw() {
3232
strokeWeight(4);
3333
point(p.x, p.y);
3434

35-
let neighbors = qtree.closest(new Point(p.x, p.y), 8, 128);
36-
stroke(0, 255, 0, 50);
37-
strokeWeight(1);
38-
for (let n of neighbors) {
39-
line(p.x, p.y, n.x, n.y);
35+
if (mouseIsPressed) {
36+
let neighbors = qtree.closest(new Point(p.x, p.y), 8);
37+
stroke(0, 255, 0, 50);
38+
strokeWeight(1);
39+
for (let n of neighbors) {
40+
line(p.x, p.y, n.x, n.y);
41+
}
4042
}
4143
}
4244
}
4345

44-
function show(qtree) {
45-
stroke(255);
46+
function show(qtree, range) {
4647
noFill();
4748
strokeWeight(1);
4849
rectMode(CENTER);
50+
stroke(255, 41);
51+
if (range.intersects(qtree.boundary)) {
52+
stroke(255);
53+
}
4954
rect(qtree.boundary.x, qtree.boundary.y, qtree.boundary.w * 2, qtree.boundary.h * 2);
5055

56+
stroke(255);
57+
strokeWeight(2);
5158
for (let p of qtree.points) {
52-
strokeWeight(2);
5359
point(p.x, p.y);
5460
}
5561

5662
if (qtree.divided) {
57-
show(qtree.northeast);
58-
show(qtree.northwest);
59-
show(qtree.southeast);
60-
show(qtree.southwest);
63+
show(qtree.northeast, range);
64+
show(qtree.northwest, range);
65+
show(qtree.southeast, range);
66+
show(qtree.southwest, range);
6167
}
6268
}

quadtree.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,15 @@ class QuadTree {
297297
}
298298

299299
if (typeof maxDistance === "undefined") {
300-
// A circle that contains the entire QuadTree
300+
// A circle as big as the QuadTree's boundary
301301
const outerReach = Math.sqrt(
302302
Math.pow(this.boundary.w, 2) + Math.pow(this.boundary.h, 2)
303303
);
304304
// Distance of query point from center
305305
const pointDistance = Math.sqrt(
306306
Math.pow(point.x, 2) + Math.pow(point.y, 2)
307307
);
308-
// One QuadTree size away from the query point
308+
// Together, a circle that encompasses the whole QuadTree
309309
maxDistance = outerReach + pointDistance;
310310
}
311311

@@ -321,8 +321,10 @@ class QuadTree {
321321
if (points.length === count) {
322322
return points; // Return when we hit the right size
323323
} else if (points.length < count) {
324+
// Grow
324325
inner = radius;
325326
} else {
327+
// Shrink
326328
outer = radius;
327329
limit --;
328330
}

0 commit comments

Comments
 (0)