Skip to content

Instantly share code, notes, and snippets.

@bgola bgola/gliders.pde
Created Dec 13, 2016

Embed
What would you like to do?
int CELL_SIZE = 2;
ArrayList tolive;
ArrayList todie;
boolean[][] grid;
int f(int v) {
return (int)v*CELL_SIZE;
}
int g(int v) {
return (int)v/CELL_SIZE;
}
void setup()
{
size(800, 800);
background(255);
noStroke();
grid = new boolean[g(width)][g(height)];
for (int x=0; x<g(width); x++) {
for (int y=0; y<g(height); y++) {
grid[x][y] = false;
}
}
tolive = new ArrayList();
todie = new ArrayList();
for (int i=0; i<random(g(1000), g(100000)); i++) {
glider_down_right((int)random(g(width)-3), (int)random(g(height)-3));
glider_down_left((int)random(g(width)-3), (int)random(g(height)-3));
glider_up_right((int)random(g(width)-3), (int)random(g(height)-3));
glider_up_left((int)random(g(width)-3), (int)random(g(height)-3));
}
/*
glider_down_right(0,0);
glider_down_right(10,0);
glider_down_right(0,10);
glider_down_right(20,0);
glider_down_right(0,20);
glider_down_right(30,0);
glider_down_right(0,30);
//glider_up_left(g(width)-3, g(height)-3);
glider_up_left(g(width)-6,g(height)-3);
glider_up_left(g(width)-3,g(height)-10);
glider_up_left(g(width)-3,g(height)-20);
glider_up_left(g(width)-16,g(height)-3);
glider_up_left(g(width)-3,g(height)-30);
glider_up_left(g(width)-26,g(height)-3);
glider_up_left(g(width)-10, 20);
*/
glider_down_right(0,0);
glider_down_right(10,0);
glider_down_right(0,10);
glider_down_left(g(width)-3,0);
//glider_down_left(g(width)-13,0);
//glider_down_left(g(width)-3,10);
glider_up_right(0,g(height)-3);
//glider_up_right(0,g(height)-13);
//glider_up_right(10,g(height)-3);
glider_up_left(g(width)-3,g(height)-3);
glider_up_left(g(width)-3,g(height)-13);
glider_up_left(g(width)-13,g(height)-3);
color c = colors[(int)random(colors.length)];
live((int)g(width)/2, (int)g(height)/2-2, c);
live((int)g(width)/2+1, (int)g(height)/2-1, c);
live((int)g(width)/2+1, (int)g(height)/2, c);
live((int)g(width)/2-2, (int)g(height)/2-1, c);
live((int)g(width)/2-2, (int)g(height)/2, c);
live((int)g(width)/2, (int)g(height)/2+1, c);
live((int)g(width)/2-1, (int)g(height)/2-2, c);
live((int)g(width)/2-1, (int)g(height)/2+1, c);
}
void glider_down_right(int x, int y) {
color c = colors[(int)random(colors.length)];
live((int)x+1, y, c);
live((int)x+2, y+1, c);
live((int)x, y+2, c);
live((int)x+1, y+2, c);
live((int)x+2, y+2, c);
}
void glider_up_left(int x, int y) {
color c = colors[(int)random(colors.length)];
live((int)x, y, c);
live((int)x+1, y, c);
live((int)x+2, y, c);
live((int)x, y+1, c);
live((int)x+1, y+2, c);
}
void glider_down_left(int x, int y) {
color c = colors[(int)random(colors.length)];
live((int)x+1, y, c);
live((int)x, y+1, c);
live((int)x, y+2, c);
live((int)x+1, y+2, c);
live((int)x+2, y+2, c);
}
void glider_up_right(int x, int y) {
color c = colors[(int)random(colors.length)];
live((int)x+1, y+2, c);
live((int)x+2, y+1, c);
live((int)x, y, c);
live((int)x+1, y, c);
live((int)x+2, y, c);
}
color ncolor(int x, int y) {
int nc = 0;
color c;
float r = 0;
float g = 0;
float b = 0;
for (int i=-1; i<2; i++) {
for (int j=-1; j<2; j++) {
if (i == 0 && j == 0) {
continue;
}
if (isalive(x+i, y+j)) {
if (nc == 0) {
r = red(get(f(x+i), f(y+j)));
} else if(nc == 1) {
g = green(get(f(x+i), f(y+j)));
} else {
b = blue(get(f(x+i), f(y+j)));
}
nc++;
}
}
}
return color(r,g,b);
}
boolean isalive(int x, int y) {
if (x < 0 || x >= g(width) || y < 0 || y >= g(height)) {
return false;
}
return grid[x][y];
}
int nn(int x, int y) {
int n=0;
for (int i=-1; i<2; i++) {
for (int j=-1; j<2; j++) {
if (i == 0 && j == 0) {
continue;
}
if (isalive(x+i, y+j)) {
n++;
if(x == 15 && y == 14) {
}
}
}
}
return n;
}
void process(int x, int y) {
boolean alive = isalive(x,y);
int n = nn(x,y);
ArrayList params = new ArrayList();
int[] xy = {x,y};
params.add(xy);
if (alive && (n < 2 || n > 3)) {
todie.add(params);
} else if (!alive && n == 3) {
color[] c = {ncolor(x,y)};
params.add(c);
tolive.add(params);
}
}
void live(int x, int y, color c) {
fill(c);
cell(x, y);
grid[x][y] = true;
}
void die(int x, int y) {
fill(
red(get(g(x),g(y))),
green(get(g(x),g(y))),
blue(get(g(x),g(y))),
alpha(get(g(x),g(y))) - 60);
cell(x, y);
grid[x][y] = false;
}
void cell(int x, int y) {
rect(f(x), f(y), CELL_SIZE, CELL_SIZE);
}
boolean processa;
void draw()
{
if (processa) {
for (int x=0; x<g(width); x++) {
for (int y=0; y<g(height); y++) {
process(x, y);
}
}
}
for(int i=0; i<tolive.size(); i++) {
ArrayList cell = (ArrayList)tolive.get(i);
int[] xy = (int[])cell.get(0);
color[] c = (color[])cell.get(1);
live(xy[0], xy[1], c[0]);
}
for(int i=0; i<todie.size(); i++) {
ArrayList cell = (ArrayList)todie.get(i);
int[] xy = (int[])cell.get(0);
die(xy[0], xy[1]);
}
tolive = new ArrayList();
todie = new ArrayList();
//for (int i =0; i<100; i++) {
// newone();
//}
}
color b = #1c9dd6;
color g = #2cb360;
color o = #ee8733;
color w = #ffffff;
color colors[] = {b, g, o};
void newone() {
int r = (int)random(3);
//color c = color(random(255), random(255, 255));
color c = colors[(int)random(colors.length)];
live((int)random(g(width)), (int)random(g(height)), c);
}
void keyPressed(){
if (key == ' '){
if(processa) processa = false;
else processa = true;
} else if (key == 's'){
saveFrame("/tmp/capa.jpg");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.