int ALPH = 0x40000000; int NUM_PARTICLES = 6; int NUM_ATTRACTORS = 5; float damp = 0.995; float accel = 1.0; color[] colours = { ALPH | 0x00EBFABB, ALPH | 0x00A3AD82, ALPH | 0x00FADFBB, ALPH | 0x00AD9B82, ALPH | 0x00050505, ALPH | 0x00FAFAFA }; Particle[] particle; Attractor[] attractor; class Attractor { float x,y; Attractor() { x = random(width); y = random(height); } } class Particle { float x,y,vx,vy,px,py; Particle() { x = px = random(width); y = py = random(height); // vx = random(1) > 0.5 ? random(-15,-12) : random(12,15); // vy = random(1) > 0.5 ? random(-15,-12) : random(12,15); vx = random(-accel/2,accel/2); vy = random(-accel/2,accel/2); } void step() { px = x; py = y; for (int i = 0; i < attractor.length; i++) { float d2 = sq(attractor[i].x-x) + sq(attractor[i].y-y); if (d2 > 0.1) { vx += accel * (attractor[i].x-x) / d2; vy += accel * (attractor[i].y-y) / d2; } } x += vx; y += vy; vx *= damp; vy *= damp; } } void setup() { size(400,400); background(255); stroke(255); smooth(); noFill(); attractor = new Attractor[NUM_ATTRACTORS]; for (int i = 0; i < attractor.length; i++) { attractor[i] = new Attractor(); } particle = new Particle[NUM_PARTICLES]; for (int i = 0; i < particle.length; i++) { particle[i] = new Particle(); } } void loop() { for (int j = 0; j < particle.length; j++) { stroke(colours[j%colours.length]); line(particle[j].x,particle[j].y,particle[j].px,particle[j].py); particle[j].step(); } } void mousePressed() { background(255); for (int i = 0; i < attractor.length; i++) { attractor[i] = new Attractor(); } }