int dimx = 600; int dimy = 400; int sweep = 0; int ncrawlers = 250; int bg = 255; Crawler[] crawlers= new Crawler[ncrawlers]; void setup() { size(dimx, dimy); for ( int i = 0; i < ncrawlers; i++ ) { crawlers[i] = new Crawler( random (0, dimx-1), random(0, dimy-1) ,random(0, 2.0*PI), random ( 0.8, 1.2 ) ); } background(bg); } void loop () { for ( int i = 0; i < ncrawlers; i++ ) { crawlers[i].go(); } trackm(); int div = int( dimy / 10 ); for ( int j = 0; j < 10; j ++ ) { bsweep(sweep + j * div ); } sweep = ( sweep + 1 ) % ( div ); } color mix ( color ac , color bc, float f ) { if ( f > 1.0 ) return bc; if ( f < 0.0 ) return ac; int r,g,b; r = int(red(ac) + (red(bc) - red(ac)) * f); g = int(green(ac) +(green(bc) - green(ac)) * f); b = int(blue(ac) + (blue(bc) - blue(ac)) * f); return color (r, g, b); } void bsweep(int y) { for ( int i = 0 ; i < dimx; i ++ ) { int y2 = (y+1) % dimy; color ac = get(i, y); color bc = get(i, y2); color rc = mix ( mix ( ac, bc, 0.2 ), color(bg,bg,bg), 0.15 ); set ( i, y, rc ) ; } } void trackm() { if ( mousePressed == true ) { for ( int i = 0; i < ncrawlers; i++ ) { crawlers[i].seek ( mouseX, mouseY ); } } } class Crawler { float x, y, vx, vy; float ang, v, angv; boolean bouncing; Crawler ( float nx, float ny, float nang, float nv ) { x = nx; y = ny; ang = nang; v = nv; vx = v * cos ( ang ); vy = v * sin ( ang ); angv = random ( -0.02, 0.02 ); bouncing = false; } void go () { x += vx; y += vy; bounce(); tpoint ( int(x), int(y), #000000, 0.3 ); } void seek(int rx, int ry) { float dang = atan2 ( ry - y , rx - x ); while ( dang > ang + PI ) dang -= TWO_PI; while ( dang < ang - PI ) dang += TWO_PI; if ( abs(dang-ang) > 0.166 * PI ) { angv = 0.04 * ( dang - ang ); } } void bounce() { int inbound = 1; if ( x < 0 || x > dimx ) { vx *= -1; ang = atan2(vy, vx); inbound = 0; } if ( y < 0 || y > dimy ) { vy *= -1; ang = atan2(vy, vx); inbound = 0; } float on = red ( get(int(x),int(y))); if ( inbound == 1 && bouncing == false && on < 200 ) { ang = atan2(vy, vx); v = random ( 0.7, 1.4 ); if ( on < 96 ) { ang += PI; ang += random ( -0.3, 0.3 ); angv = random ( -0.02, 0.02 ); tpoint ( int(x), int(y), #40FFFF, 0.7 ); } else { angv *= -1.05; tpoint ( int(x), int(y), #FF4040, 0.7 ); } bouncing = true; } else { bouncing = false; } ang += angv; vx = v * cos (ang); vy = v * sin (ang); } } // translucent point // thanks to j.tarbell void tpoint(int x1, int y1, color myc, float a) { if ((x1>=0) && (x1=0) && (y1=1.0) { set ( x1, y1, myc ) ; } else { int r, g, b; color c; c = get(x1, y1); r = int(red(c) + (red(myc) - red(c)) * a); g = int(green(c) +(green(myc) - green(c)) * a); b = int(blue(c) + (blue(myc) - blue(c)) * a); color nc = color(r, g, b); set( x1 ,y1, nc); } } } // O