/** * click 5 times to trigger another scrunchie */ int NUM_PARTICLES = 128; int NUM_ATTRACTORS = 5; Particle[] particle; Attractor[] attractor; float damp = 0.95; float accel = 5.0; int numDead = 0; int numAttractors = 0; class Particle { float x,y,vx,vy; boolean die; Particle(float x, float y) { this.x = x; this.y = y; vx = random(-accel/2,accel/2); vy = random(-accel/2,accel/2); } void step() { if (die) return; for (int i = 0; i < attractor.length; i++) { float d2 = sq(attractor[i].x-x) + sq(attractor[i].y-y); if (d2 > accel) { vx += accel * (attractor[i].x-x) / d2; vy += accel * (attractor[i].y-y) / d2; } else { die = true; numDead++; } } x += vx; y += vy; vx *= damp; vy *= damp; } void draw() { stroke(die ? 255 : 0,2); curveVertex(x,y); } } class Attractor { float x,y; Attractor() { x = random(width); y = random(height); } Attractor(float x,float y) { this.x = x; this.y = y; } } void setup() { size(600,400,P3D); attractor = new Attractor[NUM_ATTRACTORS]; particle = new Particle[NUM_PARTICLES]; scatter(); } void draw() { if (numAttractors == attractor.length) { scale(0.9); translate(width/20.0,height/20.0); for (int j = 0; j < particle.length; j++) { particle[j].step(); } if (numDead < particle.length) { beginShape(LINE_LOOP); for (int j = 0; j < particle.length; j++) { particle[j].draw(); } endShape(); } } } void mouseReleased() { println("mouse released"); if (numAttractors == attractor.length) { numAttractors = 0; background(255); } attractor[numAttractors] = new Attractor(mouseX,mouseY); numAttractors++; if (numAttractors == attractor.length) { scatter(); } } void scatter() { background(255); numDead = 0; for (int i = 0; i < particle.length; i++) { float x,y,prop; if (i < particle.length/4.0) { prop = (float)i/((float)particle.length/4.0); x = width*prop; y = 0; } else if (i < particle.length/2.0) { prop = (float)(i-(particle.length/4.0))/((float)particle.length/4.0); x = width; y = (float)height*prop; } else if (i < 3.0*particle.length/4.0) { x = (float)width-((float)width*(float)(i-(particle.length/2.0))/((float)particle.length/4.0)); y = height; } else { x = 0; y = (float)height-((float)height*((float)(i-(3.0*particle.length/4.0))/((float)particle.length/4.0))); } particle[i] = new Particle(x,y); } }