-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
77 lines (59 loc) · 1.67 KB
/
sketch.js
File metadata and controls
77 lines (59 loc) · 1.67 KB
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
let nAttractors = 1000;
let Attractors = [];
let Nodes = [];
function setup() {
createCanvas(windowWidth, windowHeight);
for (let i = 0; i < nAttractors; i++) {
Attractors.push(new Attractor(createVector(random(width), random(height))));
}
Nodes.push(new Node(createVector(width / 2, height / 2), null));
}
function draw() {
background("#1e1e1e");
if (mouseIsPressed) {
Attractors = [];
Nodes = [];
for (let i = 0; i < nAttractors; i++) {
Attractors.push(
new Attractor(createVector(random(width), random(height)))
);
}
Nodes.push(new Node(createVector(width / 2, height / 2), null));
}
Nodes.forEach((node) => node.direction.mult(0));
let parentNodes = new Set();
for (let i = Attractors.length - 1; i >= 0; i--) {
currentAttractor = Attractors[i];
let minDist = Attractor.influenceRadius;
let minNode = null;
Nodes.forEach((node) => {
let dist = currentAttractor.position.dist(node.position);
if (dist < minDist) {
minDist = dist;
minNode = node;
}
});
if (minDist < Attractor.killingRadius) {
Attractors.splice(i, 1);
continue;
}
if (minNode != null) {
parentNodes.add(minNode);
minNode.direction.add(
p5.Vector.sub(currentAttractor.position, minNode.position).normalize()
);
}
}
parentNodes.forEach((node) => {
let newNode = new Node(
p5.Vector.add(
node.position,
p5.Vector.mult(node.direction.normalize(), Node.segmentLenght)
),
node
);
Nodes.push(newNode);
});
Attractors.forEach((attractor) => attractor.draw(p5));
Nodes.forEach((node) => node.draw(p5));
}