xxxxxxxxxx
44
// Daniel Shiffman
// https://github.com/CodingTrain/QuadTree
// https://thecodingtrain.com/CodingChallenges/098.1-quadtree.html
// https://thecodingtrain.com/CodingChallenges/098.2-quadtree.html
// https://thecodingtrain.com/CodingChallenges/098.3-quadtree.html
let particles = [];
function setup() {
createCanvas(600, 400);
for (let i = 0; i < 1000; i++) {
particles[i] = new Particle(random(width), random(height));
}
}
function draw() {
background(0);
let r = new Rectangle(0, 0, width, height);
let quadtree = new QuadTree(r, 4);
for (let p of particles) {
p.move();
p.setHighlight(false);
const point = new Point(p.x, p.y, p);
quadtree.insert(point);
}
for (let p of particles) {
let c = new Circle(p.x, p.y, p.r * 2);
let points = quadtree.query(c);
for (let point of points) {
let other = point.userData;
if (p !== other && p.intersects(other)) {
p.setHighlight(true);
}
}
}
for (let p of particles) {
p.render();
}
}