Skip to content

Instantly share code, notes, and snippets.

@jcmckeown
Created May 24, 2017 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcmckeown/cbf5813d5a0e23b2cfe4f3ac894124eb to your computer and use it in GitHub Desktop.
Save jcmckeown/cbf5813d5a0e23b2cfe4f3ac894124eb to your computer and use it in GitHub Desktop.
Rule30IshLifeIsh
PImage nextFrame;
void rule30(){
int j,k, midpt;
background(255);
loadPixels();
midpt = floor(width/2);
set(midpt,0,color(0));
for ( j = 1 ; j < height ; j++ ) {
for ( k = 1 ; k < width-1 ; k++) {
int one, two, three, check;
one = red(pixels[ width * (j-1) + k-1]) < 30 ? 1 : 0 ;
two = red(pixels[ width * (j-1) + k]) < 30 ? 1 : 0 ;
three = red(pixels[ width * (j-1) + k+1]) < 30 ? 1 : 0 ;
check = 4 * one + 2 * two + three;
if ( check < 5 && check > 0 ) {
pixels[ width*j + k] = color(0);
} else { pixels[width*j +k] = color(255); }
}
}
updatePixels();
}
void setup(){
size( 500 , 500 , P2D);
frameRate(15);
nextFrame = new PImage(width, height, RGB);
rule30();
}
int countNear( int x, int y, PImage img ) {
int count = 0;
int i , j ;
img.loadPixels();
for ( i = -1 ; i < 2 ; i++ ){
for ( j = -1 ; j < 2 ; j++ ){
count += ( red(img.pixels[width*(y+i) + x+j ]) < 30 ? 1 : 0 );
}
}
return count;
}
int countVertEdge (int lowOrHigh , int y , PImage img ) {
int count = 0;
int edge = (lowOrHigh > 0 ? width-2 : 0 );
int i , j ;
img.loadPixels();
for ( i = -1 ; i < 2 ; i++){
count += ( red(img.pixels[width*y + edge] ) < 30 ? 1 : 0 );
count += ( red(img.pixels[width*y + edge + 1] ) < 30 ? 1 : 0 );
}
return count;
}
int countHzEdge (int lowOrHigh , int x , PImage img ) {
int count = 0;
int edge = ( lowOrHigh > 0 ? height - 2 : 0 );
int i;
for ( i = -1 ; i < 2 ; i++) {
count += ( red(img.pixels[ width * edge + x ])<30? 1 : 0 );
count += ( red(img.pixels[ width * (edge+1) + x ])<30? 1 : 0 );
}
return count;
}
color lifeUpdate ( int is , int count ) {
color ans = color(0); // we must start somewhere; so assume we'd wake up/persist, and then hope we do survive;
if (is == 1){ if ( count < 3 ) ans = color(255) ; // count fewer than three, less than two neighbors, move on
if ( count > 4 ) ans = color(255) ; // counter more than four, more than three neighbors, move on
} else {
if ( count != 3 ) ans = color(255); // not exactly three neighbours means we stay in bed.
}
return ans;
}
int isPx ( int x , int y , PImage img ) {
img.loadPixels();
return (red(img.pixels[width * y + x]) < 30 ? 1 : 0 );
}
void gameOfLife(PImage page){
int i, j , count , is ;
nextFrame.loadPixels();
page.loadPixels();
for ( j = 1 ; j < height - 1 ; j ++ ) {
is = isPx ( 0 , j , page );
count = countVertEdge(0,j,page) + floor(random(5));
nextFrame.pixels[ width * j ] = lifeUpdate(is,count);
is = isPx ( width-1 , j , page) ;
count = countVertEdge(1,j,page) + floor(random(5));
nextFrame.pixels[ width * (j+1) - 1] = lifeUpdate(is,count);
}
for ( i = 1 ; i < width-1 ; i++ ){
is = isPx( i , 0 , page) ;
count = countHzEdge(0,i,page) + floor(random(5));
nextFrame.pixels[ i ] = lifeUpdate( is , count ) ;
for ( j = 1 ; j < height - 1; j++ ) {
is = isPx ( i , j , page );
count = countNear ( i , j , page );
nextFrame.pixels[ width * j + i ] = lifeUpdate(is,count);
}
is = isPx( i , height-1 ,page);
count = countHzEdge(1,i,page) + floor(random(5));
nextFrame.pixels[ width * ( height-1) + i ] = lifeUpdate(is,count);
}
nextFrame.updatePixels();
}
void draw(){
if ( frameCount > 3 ){ // There's a chance your whole life farm will die if you don't wait patiently a little.
gameOfLife(get());
image(nextFrame,0,0);
}
// If you feel like it...
/* if (frameCount < 500 ) {
saveFrame("rule30Life-###.png");
} */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment