A nicer Porcessing sketch

The following is a nicer, colour version of the rectangles sketch for processing:

//the size of the screen side (square shape)
int complete_size = 400;

//maximum number of rectangles per side
int maxrects = 8;
//minumum number of rectangles
int minrects = 4;

//probability of colour change
float probability = 1; // from 0 to 100

int rects = maxrects;

//colours of the rectangles
int[ ][ ][ ] rectvals = new int[rects][rects][3];

void setup()
{
  size(complete_size,complete_size);
  background(0);
  initializerects();
}

void draw()
{
   if(random(100) < 0.5)
   rects = (int)(random(minrects,maxrects));

  int rectsize = complete_size / rects;
  for(int i =0; i < rects; i++)
  {
    for(int j =0; j < rects; j++)
    {
      fill(cycleVals(i,j,0),cycleVals(i,j,1),cycleVals(i,j,2));
      rect(i*rectsize,j*rectsize,rectsize,rectsize);
    }
  }
}

void initializerects()
{
  for(int i =0; i < rects; i++)
  {
    for(int j =0; j < rects; j++)
    {
      rectvals[i][j][0] = (int)(random(255));
      rectvals[i][j][1] = (int)(random(255));
      rectvals[i][j][2] = (int)(random(255));
    }
  }
}

//cycle the coulour
//i is the row index
//j is the column index
int cycleVals(int i,int j, int colorbase)
{
  if(random(100) < probability)
  {
   rectvals[i][j][colorbase] = (int)(random(255));
  }

  rectvals[i][j][colorbase]++;

  if(rectvals[i][j][colorbase] ==255)
     rectvals[i][j][colorbase] = (-254);

  return abs( rectvals[i][j][colorbase] );
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s