/** * Cosmos - by Howie Spielman. * Modified from Conway's Game of Life by Mike Davis. * * The "LIFE" Algorithm: * A lit point turns off if there are fewer than * two or more than three surrounding lit points. * An unlit point turns on if there are exactly three * lit neighbors. The 'density' parameter determines * how much of the board will start out lit. * * Cosmos adds color based on the age of the point. * Click the mouse and new stars are born. */ int sx, sy; float density = 0.12; int[][][] world; int generations = 0 ; int pressX = 0 ; int pressY = 0 ; int releaseX = 0 ; int releaseY = 0 ; void setup() { size(500, 310, P2D); frameRate(10); sx = width; sy = height; world = new int[sx][sy][3]; // Set random cells to 'on' for (int i = 0; i < sx * sy * density; i++) { world[(int)random(sx)][(int)random(sy)][1] = 1; } } void draw() { background(0); boolean setCursor = false ; generations++ ; //if ( generations > 120 ) // delay(80) ; boolean randLife = ( generations >= 400 && generations % 100 == 0 ) ; // 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] == 1) // Change recommended by The.Lucky.Mutt if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1)) { // Show color for a newborn, white when young, light blue in middle, and dark blue in old age int rgb = #FFFFDD ; // default color for "young" if ( world[x][y][2] < 0 ) // newborn rgb = #CC9933 ; else if ( world[x][y][0] != 1 ) // newborn rgb = #DD3333 ; else if ( world[x][y][2] >= 250 ) rgb = #001188 ; else if ( world[x][y][2] >= 30 ) rgb = #003399 ; else if ( world[x][y][2] > 6 ) rgb = #0088FF ; world[x][y][0] = 1; set(x, y, rgb); } 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+1) { for (int y = 0; y < sy; y=y+1) { int count = neighbors(x, y); if (count == 3 && world[x][y][0] == 0) { world[x][y][1] = 1; world[x][y][2] = 0; } else if ((count < 2 || count > 3) && world[x][y][0] == 1) { world[x][y][1] = -1; world[x][y][2] = 0; } else world[x][y][2] = world[x][y][2] + 1; if ( mouseX == x && mouseY == y ) { world[x][y][1] = 1; world[x][y][2] = 0; } else if ( randLife && world[x][y][1] != 1 ) { if ( random(1.0) >= 0.99825 ) { world[x][y][1] = 1; world[x][y][2] = 0; } } } } if ( releaseX > 0 && releaseY > 0 ) { PVector v1 = new PVector (pressX, pressY, 0) ; PVector v2 = new PVector (releaseX, releaseY, 0) ; int rad = v1.dist(v2) ; if ( rad < 30 ) rad = 30 ; else rad = Math.floor(rad) ; int startX = constrain (pressX-rad, 0, sx-1) ; int startY = constrain (pressY-rad, 0, sy-1) ; int endX = constrain (pressX+rad, 0, sx-1) ; int endY = constrain (pressY+rad, 0, sy-1) ; for ( int x = startX; x<=endX; x++ ) { for ( int y = startY; y<=endY; y++ ) { float r = random(1.0) ; v2 = new PVector (x, y, 0) ; int vdist = Math.abs ( v1.dist(v2) ) ; if ( r <= density && vdist <= rad ) { world[x][y][1] = 1; world[x][y][2] = -1; } } } releaseX = 0 ; releaseY = 0 ; } } void mousePressed() { pressX = mouseX ; pressY = mouseY ; } void mouseReleased() { releaseX = mouseX ; releaseY = mouseY ; } //void mouseDragged() //{ // pressX = mouseX ; // pressY = mouseY ; //} // Count the number of adjacent cells 'on' int neighbors(int x, int y) { return world[(x + 1) % sx][y][0] + world[x][(y + 1) % sy][0] + world[(x + sx - 1) % sx][y][0] + world[x][(y + sy - 1) % sy][0] + world[(x + 1) % sx][(y + 1) % sy][0] + world[(x + sx - 1) % sx][(y + 1) % sy][0] + world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] + world[(x + 1) % sx][(y + sy - 1) % sy][0]; }