// This program has the same structure as the gas molecule one. Some // names have changed to keep them relevant to this new example, // but the most major modifications are that the shape of the // model universe has changed and a few simple rules have been added // to dictate the direction of flight of each bird. // The parameter "eyeSight" defines the field-of-view range of each bird. // The parameter "randomJiggle" determines the magnitude of the random angle // added on to each bird's assessment of the average heading of // its neighbours. // See the end of this program for suggestions on how it could be improved. // Parameters: int numBirds = 500; int flySpeed = 5; int eyeSight = 20; int randomJiggle = 15; // Set-up other variables and matrices int deltaX, deltaY; float angle, meanAngle; // angle in degrees, 0 = straight up int x1,x2,y1,y2; float distance; int [][] flock = new int[numBirds][3]; // Function to set-up the dimensions and colours // of the model universe and the display rate void setup() { size(500,500); stroke(0); background(51); smooth(); framerate(100); flockInit(); // noLoop(); } // Function to draw the state of the simulation every // timestep, with location of each bird depicted as a // small white circle and visual range of one example // bird as an orange circle. void draw() { background(100,149,237); // refresh the screen every timestep fill(204, 102, 50); ellipse(flock[0][0],flock[0][1],2*eyeSight,2*eyeSight); fill(255); for(int i=0; iwidth) { flock[i][0]=0; } if (flock[i][0]<0) { flock[i][0]=width; } if (flock[i][1]>height) { flock[i][1]=0; } if (flock[i][1]<0) { flock[i][1]=height; } } } int meanHeading(int thisBird) { x1=flock[thisBird][0]; y1=flock[thisBird][1]; int [] headingList = new int[0]; for(int i=0; i