CA ca;
int scale = 5;
void setup() {
size(960, 540);
colorMode(HSB, 360, 100, 100);
smooth();
int[] rule = {0, 1, 0, 1, 1, 0, 1, 0};
ca = new CA(rule);
}
void draw() {
ca.render();
ca.generate();
if (ca.finished()) {
saveFrame("######.png");
background(0, 0, 100);
scale = (int)random(2, 20);
ca.cells = new int[width/ scale];
ca.randomize();
ca.restart();
}
}
class CA {
int[] cells;
int generation;
int[] rule;
CA(int[] _rule) {
rule = _rule;
cells = new int[width/ scale];
restart();
}
CA() {
scale = 1;
cells = new int[width/scale];
randomize();
restart();
}
void setRules(int[] _rule) {
rule = _rule;
}
void randomize() {
for (int i = 0; i < 8; i++) {
rule[i] = (int)random(2);
}
}
void restart() {
for (int i = 0; i < cells.length; i++) {
cells[i] = 0;
}
cells[cells.length/2] = 1;
generation = 0;
}
void generate() {
int[] next = new int[cells.length];
for (int i = 1; i < cells.length - 1; i++) {
int left = cells[i-1];
int current = cells[i];
int right = cells[i+1];
next[i] = rules(left, current, right);
}
cells = next;
generation++;
}
int rules(int left, int current, int right) {
String s = "" + left + current + right;
int index = Integer.parseInt(s, 2);
return rule[index];
}
boolean finished() {
if (generation > height / scale) {
return true;
} else {
return false;
}
}
void render() {
for (int i = 0; i < cells.length; i++) {
if (cells[i] == 1) {
fill(0, 0, 0);
} else {
fill(0, 0, 100);
}
stroke(0, 0, 0);
rect(i * scale, generation * scale, scale, scale);
}
}
}