// Cellular Automata, Life Cycle // by Tarmle // based on 'Cellular Automata 1' by Mike Davis // A simple set of rules that produce hightly // complex orginization: three adjacent cells will // produce a fourth (if there is not already a // cell present), cells have a limited life span // that is shortened by proximity of other cells. // Created 5 April 2005 int sx, sy; float density = 0.1; int span = 255; // Life Range int drop = 2; // Age Decriment int cage = 242; // Minimum Reproductive Span int csize = 2; // Cell Size int[][][] world; void setup() { size(300, 300); framerate(20); sx = int(width/csize); sy = int(height/csize); world = new int[sx][sy][2]; filler(sx,sy); } void loop() { background(255); // User input cycle if (mousePressed == true) { for (int x = 0; x < sx; x=x+1) { for (int y = 0; y < sy; y=y+1) { world[x][y][1] = 0; } } filler(width/csize,height/csize); } // Drawing and update cycle for (int x = 0; x < sx; x=x+1) { for (int y = 0; y < sy; y=y+1) { if (world[x][y][1] > 0) { noStroke(); fill(255 - world[x][y][1]); rect(x*csize, y*csize, csize, csize); } world[x][y][0] = world[x][y][1] - drop; if (world[x][y][0] > span) {world[x][y][0] = span;} world[x][y][1] = 0; } } // Birth and death cycle for (int x = 1; x < sx-1; x=x+1) { for (int y = 1; y < sy-1; y=y+1) { int count = neighbors(x, y); if (count == 3 && world[x][y][0] < cage) { world[x][y][1] = span; } else if (world[x][y][0] > 0) { world[x][y][1] = world[x][y][0] - (drop*count); } } } } // Fill random cells void filler(int sx, int sy) { for (int i = 0; i < sx * sy * density; i++) { world[(int)random(sx)][(int)random(sy)][1] = span; } } // Count the number of adjacent cells 'on' int neighbors(int x, int y) { int adj = 0; for (int ax = x-1; ax <= x+1; ax++) { for (int ay = y-1; ay <= y+1; ay++) { if (world[ax][ay][0] > cage) {adj++;} } } if (world[x][y][0] > cage) {adj--;} return adj; }