Particle[] dots; int count; int[] bright; int iMax; int[] dx; int[] dy; int[] tmpBright; Gradient grad; void setup(){ size(300,300); noStroke(); framerate(30); iMax=width*height; bright=new int[iMax]; tmpBright=new int[iMax]; for (int i=0;i0){ p+=bright[i-1]; c++; } if (x0){ p+=bright[i-width]; c++; } if (y=0 && y>=0 && x>8; } else { p=bright[i]; } pixels[i]=grad.getColor(p); } } void display(){ for (int i=0;i>8; } class Particle { float x,y,nx,ny; int ix,iy,ip; float vx,vy; float speed; float targetSpeed; float angle; float targetAngle; float cx,cy; float MAXSPEED=5; float ENEMY_ZONE=15; float TARGET_ZONE=15; float AVOID_ZONE=5; float BORDER=30; float CONSTRAIN_RADIUS=0; float INNER_CONSTRAIN_RADIUS=100; boolean friendDetected=false; boolean enemyDetected=false; Particle[] targets; Particle[] enemies; Particle(int w,int h){ cx=float(w)/2; cy=float(h)/2; x=random(w); y=random(h); vx=0; vy=0; targetSpeed=speed=0; targetAngle=angle=random(TWO_PI); targets=new Particle[0]; enemies=new Particle[0]; ENEMY_ZONE*=ENEMY_ZONE; TARGET_ZONE*=TARGET_ZONE; AVOID_ZONE*=AVOID_ZONE; CONSTRAIN_RADIUS=min(width,height)/2-BORDER; CONSTRAIN_RADIUS*=CONSTRAIN_RADIUS; } void setXY(int xs,int ys){ x=xs; y=ys; } void addTarget(Particle t){ for (int i=0;iwidth-BORDER || y>height-BORDER){ if (d>CONSTRAIN_RADIUS){ angle=turn(angle,atan2(dy,dx)+HALF_PI/8,.15); speed*=0.95; } else if (dAVOID_ZONE){ dMin=d; pointAngle=atan2(dy,dx); } friendDetected=true; } } enemyDetected=false; dMin=TARGET_ZONE+1; float fleeAngle=angle; for (int i=0;iPI){ aDif=-(TWO_PI-aDif); } else if (aDif<-PI){ aDif=TWO_PI+aDif; } return currentAngle+aDif*percent; } void render(){ if (x<0 || x>=width || y<0 || y>=height || ip<0 || ip>=iMax){ return; } if (enemyDetected || friendDetected){ bright[ip]=255; } else { bright[ip]=0; } } } class Gradient { Marker[] markers; color[] palette; Gradient(color c1,color c2){ markers=new Marker[2]; markers[0]=new Marker(0,c1); markers[1]=new Marker(1,c2); palette=new color[256]; this.updatePalette(); } void addMarker(float p,color c){ Marker[] tempMarkers=new Marker[markers.length+1]; int iAdd=0; for (int i=0;i