/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.sfu6HMlcMHo/rev.1512
*
* authors:
* Zach Denton
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
// This sketch builds on a prior work, "Undulating Pixel Grid", created by Zach Denton
// http://studio.sketchpad.cc/sp/pad/view/ro.9lU9zD6n-JpSi/rev.366
// See also: http://en.wikipedia.org/wiki/Rule_30
int width = 100;
int height = 100;
int cell_width = 5;
int[][] grid = new int[height][width];
color alive = color(random(50), random(255), random(255), 100);
color dead = color(200);
void setup() {
background(dead);
// size(width * cell_width, height * cell_width);
size(500, 500);
smooth();
noStroke();
reset();
grid[0][(width / 2) - 1] = 1;
update();
}
void update() {
// don't start processing until a live cell is found
seen = false;
for (int y=0; y < height; y++) {
for (int x=0; x < width; x++) {
if (x > 0) {
left = grid[y][x-1];
if (left > 1) {
left = 1;
}
} else {
left = 0;
}
if (x < width - 1) {
right = grid[y][x+1];
if (right > 1) {
right = 1;
}
} else {
right = 0;
}
center = grid[y][x];
pattern = str(left) + str(center) + str(right);
if (pattern == '111') {
next = 0;
} else if (pattern == '110') {
next = 0;
} else if (pattern == '101') {
next = 0;
} else if (pattern == '100') {
next = 1;
} else if (pattern == '011') {
next = 1;
} else if (pattern == '010') {
next = 1;
} else if (pattern == '001') {
next = 1;
} else {
next = 0;
}
if (y < height - 1) {
grid[y+1][x] = next;
}
}
}
}
void reset() {
for (y=0; y < height; y++) {
for (x=0; x < width; x++) {
grid[y][x] = 0;
}
}
}
void draw() {
if (mousePressed == true) {
mousePressed();
}
for (int y=0; y < height; y++) {
for (int x=0; x < width; x++) {
if (grid[y][x] >= 1) {
strokeWeight(2);
fill(255);
stroke(alive);
} else {
noStroke();
fill(dead);
}
rect(x * cell_width, y * cell_width, cell_width, cell_width);
}
}
}
void mousePressed() {
if (mouseButton == LEFT) {
x = int(mouseX / cell_width);
y = int(mouseY / cell_width);
if (grid[y][x] == 0) {
grid[y][x] = 1;
} else {
grid[y][x] = 0;
}
} else if (mouseButton == CENTER) {
reset();
} else {
update();
}
}