// Particle and attractor code by TomC, from: http://www.tom-carden.co.uk/p5/attractors5/applet/ // Blur/fade modes added by John Gilbertson int ALPH = 0x80000000; int frame; int mode=0; int blur=0; int NUM_PARTICLES = 8192; int NUM_ATTRACTORS = 6; float damp = 0.9; float accel = 10.0; //color[] colours = { ALPH | 0x00ffff00 , ALPH | 0x00ff8000, ALPH | 0x00ffff00, ALPH | 0x00ff8000, ALPH | 0x00ffff00, ALPH | 0x00ff8000, ALPH | 0x00ffffff }; //ALPH | 0x00EBFABB, ALPH | 0x00A3AD82, ALPH | 0x00FADFBB, ALPH | 0x00AD9B82, ALPH | 0x00050505, ALPH | 0x00FAFAFA }; color bg = 0x00800000; Particle[] particle; Attractor[] attractor; int irandom(int maxval) { return int(random(maxval)); } class Attractor { float x,y; float dx,dy; Attractor() { x = random(width); y = random(height); dx=random(5)-2; dy=random(5)-2; } void Move() { x+=dx; y+=dy; if(x>width || x<0) { dx=0-dx; } if(y>height || y<0) { dy=0-dy; } } } class Particle { float x,y,vx,vy,px,py; color c; Particle() { init(); } void init() { x = px = random(width); y = py = random(height); vx = random(-accel/2,accel/2); vy = random(-accel/2,accel/2); c=color(128+irandom(128),128+irandom(128),128+irandom(128)); } void step() { 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; } } px = x; py = y; x += vx; y += vy; vx *= damp; vy *= damp; // if (x < 0 || x > width || y < 0 || y > height) { // init(); // } } } BGraphics z; void setup() { size(360,360); z=new BGraphics(width,height); z.background(0,0,0); // framerate(1); background(0,0,0); stroke(255); smooth(); strokeWeight(1); 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 myBackground() { int q=0,r=0,s=0; if(mode==4) { q=int(random(256)); r=int(random(256)); s=int(random(256)); } for(int y=0;y0) n[0]=z.pixels[x-1+y*width]; if(x0) n[2]=z.pixels[x+((y-1)*width)]; if(y>16; gv+=(n[i]&0x0000FF00)>>8; bv+=n[i]&0x000000FF; } rv>>=2; gv>>=2; bv>>=2; rv+=(p&0x00FF0000)>>16; gv+=(p&0x0000FF00)>>8; bv+=(p&0x000000FF); rv>>=1; gv>>=1; bv>>=1; pixels[x+y*width]=(rv<<16)+(gv<<8)+bv; z.pixels[x+y*width]=(rv<<16)+(gv<<8)+bv; } if(mode==0) { int p=z.pixels[x+y*width]; int a=(p&0x00FF0000)>>16; int b=(p&0x0000FF00)>>8; int c=(p&0x000000FF); a>>=1; b>>=1; c>>=1; z.pixels[x+y*width]=(ALPH<<24)+(a<<16)+(b<<8)+c; } if(mode==1) { int p=z.pixels[x+y*width]; int a=(p&0x00FF0000)>>16; int b=(p&0x0000FF00)>>8; int c=(p&0x000000FF); if(a>=8) a-=8; if(b>=8) b-=8; if(c>=8) c-=8; z.pixels[x+y*width]=(a<<16)+(b<<8)+c; } if(mode==2) { z.pixels[x+y*width]=z.pixels[x+y*width]&((q<<16)+(q<<8)+q); } if(mode==3||mode==4) { z.pixels[x+y*width]=z.pixels[x+y*width]&((q<<16)+(r<<8)+s); } } } } void loop() { myBackground(); for (int j = 0; j < particle.length; j++) { z.stroke(particle[j].c); z.line(particle[j].x,particle[j].y,particle[j].px,particle[j].py); particle[j].step(); } for(int i=0; i< attractor.length;i++) { attractor[i].Move(); } frame++; } void keyPressed() { mode++; mode%=6; } void mousePressed() { saveFrame(); // background(128,0,0); for (int i = 0; i < attractor.length; i++) { attractor[i] = new Attractor(); } }