xxxxxxxxxx
int n = 1024;
int[] type = new int[n];
float[] x = new float[n];
float[] y = new float[n];
float[] vx = new float[n];
float[] vy = new float[n];
float[] ax = new float[n];
float[] ay = new float[n];
float[] nx = new float[n];
float[] ny = new float[n];
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
void setup() {
fullScreen();
fill(255, 32);
reset();
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
void draw() {
noStroke();
rect(0, 0, width, height);
for (int a = 0; a < n; a++) {
ax[a] = 0;
ay[a] = 0;
float closestDistance = 123456;
int interactableType = 0;
if (type[a] == 1) interactableType = 3;
else if (type[a] == 2) interactableType = 1;
else if (type[a] == 3) interactableType = 2;
for (int b = 0; b < n; b++) {
if (type[b] == interactableType) {
float dx = x[a] - x[b];
float dy = y[a] - y[b];
float d = dx*dx + dy*dy;
float f = cos(sqrt(d) * 0.05) / d;
f = constrain(f, -1, 1);
ax[a] += f * dx;
ay[a] += f * dy;
if (d < closestDistance) {
closestDistance = d;
nx[a] = x[b];
ny[a] = y[b];
}
}
}
vx[a] = vx[a] * 0.99 + ax[a];
vy[a] = vy[a] * 0.99 + ay[a];
}
for (int i = 0; i < n; i++) {
x[i] += vx[i];
y[i] += vy[i];
x[i] = constrain(x[i], 0, width);
y[i] = constrain(y[i], 0, height);
if (type[i] == 1) stroke(0);
else if (type[i] == 2) stroke(32, 0, 64);
else if (type[i] == 3) stroke(0, 64, 128);
line(x[i], y[i], nx[i], ny[i]);
}
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
void reset() {
for (int i = 0; i < n; i++) {
type[i] = (int) random(3) + 1;
x[i] = random(width);
y[i] = random(height);
vx[i] = 0;
vy[i] = 0;
}
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
void mousePressed() {
reset();
}