xxxxxxxxxx
80
const instanceFn = (p) => {
function automaton(total, ruleValue, color) {
const generations = [];
{
let cells = Array(total).fill(0);
cells[Math.floor(total / 2)] = 1;
generations.push(cells);
}
const ruleSet = ruleValue.toString(2).padStart(8, "0");
function calculateState(a, b, c) {
let neighborhood = "" + a + b + c;
let value = 7 - parseInt(neighborhood, 2);
return parseInt(ruleSet[value]);
}
function step() {
const cells = generations.at(-1);
generations.push(cells.map((state, i) => {
const len = cells.length;
const left = cells[(i - 1 + len) % len];
const right = cells[(i + 1) % len];
return calculateState(left, state, right);
}));
}
function render() {
generations.forEach((cells, j) => {
const y = j * CELL_WIDTH;
cells.forEach((cell, i) => {
const x = i * CELL_WIDTH;
p.fill(color);
if (cell !== 0) p.square(x, y, CELL_WIDTH);
});
});
}
return { generations, ruleValue, render, step };
}
const CANVAS_WIDTH = 600;
const CELL_WIDTH = 16;
const CELL_COUNT = Math.floor(CANVAS_WIDTH / CELL_WIDTH);
let colors;
let automata;
function setup() {
p.createCanvas(CANVAS_WIDTH, 600);
const ruleValues = Array(3)
.fill(0)
.map(() => p.random(255));
const alpha = 255 / ruleValues.length;
colors = [
p.color(0xff, 0, 0, alpha),
p.color(0, 0xff, 0, alpha),
p.color(0, 0, 0xff, alpha),
];
automata = ruleValues.map((val, i) =>
automaton(CELL_COUNT, val, colors[i % colors.length])
);
}
function draw() {
p.clear();
const genCount = automata[0].generations.length;
if (genCount * CELL_WIDTH > p.height) p.translate(0, p.height - genCount * CELL_WIDTH);
for (const automaton of automata) {
automaton.render();
automaton.step();
}
p.describe("Very slow...");
}
p.setup = setup;
p.draw = draw;
};
const INSTANCE_COUNT = 6;
for (let i = 0; i < INSTANCE_COUNT; i++) {
new p5(instanceFn);
}