// Original game of life algorithm // // 1. Death: if the count is less than 2 or greater than 3, the current cell is switched off. // 2. Survival: if (a) the count is exactly 2, or (b) the count is exactly 3 and the current cell is on, the current cell is left unchanged. // 3. Birth: if the current cell is off and the count is exactly 3, the current cell is switched on. int width = 320, height = 320, cellSize = 30; int offset = height; boolean offsetDirection = true; int maxCells = (width * height) / cellSize; Cell[][] cells = new Cell[width / cellSize][height / cellSize]; void setup() { size(width, height); framerate(24); noStroke(); background(0); maxCells = 0; // Populate the game's board using a random variable for (int y = 0; y < (height / cellSize); y++) { for (int x = 0; x < (width / cellSize); x++, maxCells++) { cells[y][x] = new Cell(); cells[y][x].set_x(x * cellSize); cells[y][x].set_y(y * cellSize); cells[y][x].set_size(cellSize); if (random(1) > 0.85) { cells[y][x].set_state_on(); } else { cells[y][x].set_state_off(); } cells[y][x].draw_cell(); } } } void loop() { if (mousePressed == true) { setup(); } if (offsetDirection) { offset--; } else { offset++; } if (offset == 0 || offset == height) { offsetDirection = !offsetDirection; } for (int y = 0; y < (height / cellSize); y++) { for (int x = 0; x < (width / cellSize); x++, maxCells++) { int infected = 0; cells[y][x] = cells[y][x]; if (x > 0 && x < (width / cellSize) - 1 && y > 0 && y < (height / cellSize) - 1) { for (int sy = -1; sy <= 1; sy++) { for (int sx = -1; sx <= 1; sx++) { if (cells[y + sy][x + sx].get_state()) { infected++; } } } if (infected < 2 || infected > 3) { // Death cells[y][x].set_state_off(); } else if (cells[y][x].get_state() == false && infected == 3) { // Birth cells[y][x].set_state_on(); } cells[y][x].draw_cell(); } } } inversion(); blur(1); } class Cell { int x, y, cellSize; boolean state; Cell() { } void set_state_on() { this.state = true; } void set_state_off() { this.state = false; } void set_x(int x) { this.x = x; } void set_y(int y) { this.y = y; } void set_size(int cellSize) { this.cellSize = cellSize; } boolean get_state() { return this.state; } int get_x() { return this.x; } int get_y() { return this.y; } void draw_cell() { if (this.get_state()) { fill(175, 175, 202); } else { fill(102, 153, 102); } ellipse(this.x, this.y, this.cellSize, this.cellSize); } } // I found this here: http://www.florianjenett.de/p55/toxi_seb_blur_applet/toxi_seb_blur.pde // FULL RGB DIFFUSION FILTER // based on code by SEB // www.seb.cc void blur(int tt) { for (int ttt = 0; ttt <= tt; ttt++) { int R,G,B,left,right,top,bottom; int c,cl,cr,ct,cb; int w1=width-1; int h1=height-1; int index=0; for (int y=0; y0) ? -width : h1*width; bottom=(y==h1) ? -h1*width : width; for (int x=0; x0) ? -1 : w1; right=(x>16 & 255) + (cr>>16 & 255) + (c>>16 & 255) + (ct>>16 & 255) + (cb>>16 & 255)) / 5; G=((cl>>8 & 255) + (cr>>8 & 255) + (c>>8 & 255) + (ct>>8 & 255) + (cb>>8 & 255)) / 5; B=((cl & 255) + (cr & 255) + (c & 255) + (ct & 255) + (cb & 255)) / 5; pixels[index++]=(R<<16)+(G<<8)+B; } } } } // This is modified code from here: http://incubator.quasimondo.com/processing/inversion.pde void inversion() { int i = 0, d, xn, yn, dx, dy; boolean mx, my; int xc = offset; int yc = offset; int k = offset * 250; for (int y = 0; y < height; y++) { dy = y - yc; for (int x = 0; x < width; x++) { dx = x - xc; d = dx * dx + dy * dy; if (d > 0) { xn = xc + (k * dx) / d; yn = yc + (k * dy) / d; } else { xn=xc; yn=yc; } mx = false; while (xn < 0) { xn+=width; mx=!mx; } while ( xn >= width) { xn -= width; mx = !mx; } my = false; while (yn < 0) { yn += height; my = !my; } while (yn >= height) { yn -= height; my = !my; } if (mx) { xn = width - 1 - xn; } if (my) { yn = height - 1 -yn; } pixels[i++] = pixels[xn + yn * width]; } } }