-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
46 lines (37 loc) · 971 Bytes
/
sketch.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
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// QuadTree
// https://www.youtube.com/watch?v=z0YFFg_nBjw
// For more:
// https://github.com/CodingTrain/QuadTree
let particles = [];
function setup() {
createCanvas(600, 400);
for (let i = 0; i < 10000; i++) {
particles[i] = new Particle(random(width), random(height));
}
}
function draw() {
background(0);
let boundary = new Rectangle(300, 200, 600, 400);
let qtree = new QuadTree(boundary, 4);
for (let p of particles) {
let point = new Point(p.x, p.y, p);
qtree.insert(point);
p.move();
p.render();
p.setHighlight(false);
}
for (let p of particles) {
let range = new Circle(p.x, p.y, p.r * 2);
let points = qtree.query(range);
for (let point of points) {
let other = point.userData;
// for (let other of particles) {
if (p !== other && p.intersects(other)) {
p.setHighlight(true);
}
}
}
}