// flocks of birds

float x, y, w, h;
int n;

void setup() {
  // setup //
  size( 600,400);
  reset();
}
void reset() {
  // reset
  n=  int ( random(3,10) );
  x=  random(width/2);
  y=  random(height/2);
  w=  random(10,70);
  h=  w/3;
}
void draw() {
  // next frame //  
  background( 200,200,255);
  fill( 255,255, 0);
  flock( n, x,y, w,h );
  x += random(20);
  y=  y +10 - random(20);
}

void flock( int n, float x, float y, float w, float h ) {
  // draw a flock of n birds //
  // Draw first bird //
  float xx=  x;
  float hh=  h * 3;                      // higher (or lower)
  float ww=  w;                           // width gets smaller.
  bird( xx,y,w,h );
  // Now draw pairs, behind this pair.
  xx=  xx - 2*w;               // Next pair behind this pair
  for (int j=1; j<n; j++ ) {
      // Draw next pair of birds. //
      bird( xx,y-hh, w,h );            // up
      bird( xx,y+hh, w,h );            // down
      xx=  xx - 2*w;               // Next pair behind this pair
      ww  *= 0.9;                  // smaller
      hh = hh + h*3;
  }
}  
void bird( float x, float y, float w, float h ) {
  //// draw a bird
  triangle( x,y, x-w,y-h, x-w,y+h );
}
void keyPressed() {
  // keys
  if (key == 'r') reset();
}
    
