April showers bring May flowers

#1

Hit Enter to regenerate the scene. The resulting images wrap, so if you make them smaller, you can tile with them.

int framesToDraw = 120;
int rectSize = 1;
int nSets = 7;
// set stringiness from 0.0 to 1.0
//   higher gives more variation, but creates more isolated islands
float stringiness = 0.3;
boolean showBorders = false;
float seed;

int nWide, nHigh, nCells;
int[] cells;
int[] front;
int[] age;
int nFront;
int[] next;
int stepsPerFrame;

int choices[] = new int[4];

void drawCell( int ic, int iSet, int iAge ) {
  if( iSet == 0 ) 
    fill( 0, 0, 1 );
  else
    fill( ( iSet*0.618+seed+iAge/2724.0) % 1.0, 
          ((iSet*0.7173) % 1.0) * 0.5 + 0.5, 
          (((iSet*0.8143+2.*seed) % 1.0) * 0.6 + 0.7) * (((iAge/(356.0*log(iSet+1)))%0.7)+0.3) );
  rect( (ic % nWide) * rectSize, (ic / nWide) * rectSize, rectSize, rectSize );
}

void setup() {
  size(1920,1080);
//  fullScreen( P2D, SPAN );
  doIt();
}

void doIt() {
  seed = random(1.0);
  background(0);
  colorMode( HSB, 1, 1, 1 );
  noStroke();

  nWide = width/rectSize;
  nHigh = height/rectSize;
  nCells = nWide*nHigh;
  stepsPerFrame = nCells / framesToDraw;
  cells = new int[nCells];
  front = new int[nCells];
  age = new int[nCells];
  next = new int[] { 1, nWide, nCells-1, nCells-nWide };
  
  // find nSets count of unique starting cells
  front[0] = (int)random(nCells);
  cells[ front[0] ] = 1;
  age[ front[0] ] = 0;
  drawCell( front[0], 1, 0 );
  for( int iSet=1; iSet<nSets; iSet++ ) {
    do {
      front[iSet] = (int)random(nCells);
    } while( cells[ front[iSet] ] != 0 );
    cells[ front[iSet] ] = iSet + 1;
    age[ front[iSet] ] = 0;
    drawCell( front[iSet], iSet+1, 0 );
  }
  nFront = nSets;
}

void draw() {
  for( int iStep=0; nFront > 0 && iStep < stepsPerFrame; iStep++ ) {
    int iFront = nFront - 1;
    int s = 1;
    if( random(1.0) >= stringiness ) {
      float r = random(1.0);
      iFront = (int)(nFront*r*r*r*r);
      s = 3;
    }
    int iCell = front[ iFront ];
    int nChoices = 0;
    for( int iDir=0; iDir<4; iDir++ ) {
      int ioc = (iCell + next[iDir]) % nCells;
      if( cells[ ioc ] == 0 )
        choices[ nChoices++ ] = ioc;
      else if( showBorders && (iDir == 0 || iDir == 1) && 
               cells[ ioc ] != cells[ iCell ] ) {
        drawCell( ioc, 0, 0 );
      }
    }
    if( nChoices > 0 ) {
      int ioc = choices[ (int)random(nChoices) ];
      cells[ ioc ] = cells[ iCell ];
      age[ ioc ] = age[ iCell ] + s;
      front[ nFront++ ] = ioc;
      drawCell( ioc, cells[ioc], age[ioc] );
    }
    if( nChoices < 2 ) {
      front[ iFront ] = front[ nFront-1 ];
      nFront--;
    }
  }
}

void keyPressed() { 
  if( key == ESC ) exit();
  if( key == ENTER ) doIt();
}
5 Likes