other exercise 1

int sx, sy;
float density =0.1;
int[][][] world;
int d=10;

void setup() {
  size(1000, 600, P2D);
  frameRate(10);
  sx = width;
  sy = height;
  world = new int[sx][sy][2];
 
  
  // Set random cells to ‘on’
  for (int i = 0; i < sx * sy * density; i+=d) {
    world[d*int(random(sx/d))][d*int(random(sy/d))][1] = 1;
  }
}

void draw()
{
background(0);
for(int a=5;a<sx;a+=d){
    stroke(20);
    line(a,0,a,sy);
    line(0,a,sx,a);
  }
   // Drawing and update cycle
  for (int x = 0; x < sx; x=x+d) {
    for (int y = 0; y < sy; y=y+d) {
      //if (world[x][y][1] == 1)
      // Change recommended by The.Lucky.Mutt
      if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1))
      {
        world[x][y][0] = 1;
   for (int i = -d/2; i < d/2; i++) {
    for(int j = -d/2; j < d/2; j++) {
    set(x+i, y+j, #ffff35);
  }
}
      }
     
      if (world[x][y][1] == -1)
      {
        world[x][y][0] = 0;
      }
      world[x][y][1] = 0;
    }
  }
  // Birth and death cycle
  for (int x = 0; x < sx; x=x+d) {
    for (int y = 0; y < sy; y=y+d) {
      int count = neighbors(x, y);
      if (count == 3 && world[x][y][0] == 0)
      {
        world[x][y][1] = 1;
      }
      if ((count < 2 || count > 3) && world[x][y][0] == 1)
     {
        world[x][y][1] = -1;
      }
    }
  }
}

// Count the number of adjacent cells ‘on’
int neighbors(int x, int y)
{
  return world[(x + d) % sx][y][0] +
         world[x][(y + d) % sy][0] +
         world[(x + sx – d) % sx][y][0] +
         world[x][(y + sy – d) % sy][0] +
         world[(x + d) % sx][(y + d) % sy][0] +
         world[(x + sx – d) % sx][(y + d) % sy][0] +
         world[(x + sx – d) % sx][(y + sy – d) % sy][0] +
         world[(x + d) % sx][(y + sy – d) % sy][0];
}
        
 
void mouseClicked() {
  int a=int(mouseX);
  int b=int(mouseY);
  if (a%10<=5 && b%10<=5 ){
world[a-a%10][b-b%10][0]=1;
  }
  if (a%10<=5 && b%10>5 ){
world[a-a%10][b-b%10+10][0]=1;
  }
  if (a%10>5 && b%10<=5 ){
world[a-a%10+10][b-b%10][0]=1;
  }
  if (a%10>5 && b%10>5 ){
world[a-a%10+10][b-b%10+10][0]=1;
  }
}

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