Example 1: Auto-Link Nodes
Code
let nodes = [];
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 26; i++) {
nodes.push({ x: random(width), y: random(height), vx: random(-1, 1), vy: random(-1, 1) });
}
}
function draw() {
background(250);
for (const n of nodes) {
n.x += n.vx; n.y += n.vy;
if (n.x < 0 || n.x > width) n.vx *= -1;
if (n.y < 0 || n.y > height) n.vy *= -1;
}
stroke(255, 45, 140, 70);
for (let i = 0; i < nodes.length; i++) {
for (let j = i + 1; j < nodes.length; j++) {
let d = dist(nodes[i].x, nodes[i].y, nodes[j].x, nodes[j].y);
if (d < 80) line(nodes[i].x, nodes[i].y, nodes[j].x, nodes[j].y);
}
}
noStroke();
fill(30);
for (const n of nodes) circle(n.x, n.y, 8);
}Try this: Lower link distance to reveal local clusters.