{{ message }}

Instantly share code, notes, and snippets.

# mbostock/.block

Last active Feb 8, 2016
Cellular automata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 license: gpl-3.0 redirect: https://beta.observablehq.com/@mbostock/1d-cellular-automaton

From MathWorld: “A cellular automaton is a collection of ‘colored’ cells on a grid of specified shape that evolves through a number of discrete time steps according to a set of rules based on the states of neighboring cells.”

This example explores binary, nearest-neighbor, one-dimensional automata, of which there are 256 (28) possible rules. All 256 rules are arranged in a grid, by column and then by row. The top-left corner is rule 0; the bottom-right corner is rule 255. Zoom in to see more of any particular rule.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 onmessage = function(e) { var z0 = Math.max(0, 4 - e.data.zoom), z1 = Math.max(0, e.data.zoom - 4), w = e.data.size.x >> z0, h = e.data.size.y >> z0, n = 1 << z0, col = e.data.column >> z1 << z0, row = e.data.row >> z1 << z0, data = e.data.data = [], state = []; for (var j = 0, y = 0; j < n; j++, y += h) { for (var i = 0, x = 0; i < n; i++, x += w) { draw((j | row) | ((i | col) << 4), x, y); } } function draw(r, x, y) { for (var i = 0; i < w; i++) { state[i] = ~~(Math.random() * 2); } for (var j = 0; j < h; j++) { var p = state.slice(), k = 4 * (e.data.size.x * (j + y) + x); for (var i = 0; i < w; i++) { data[k++] = data[k++] = data[k++] = 255 * state[i]; data[k++] = 255; state[i] = (r >> (p[i - 1] << 2 | p[i] << 1 | p[i + 1])) & 1; } } } postMessage(e.data); };
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

to join this conversation on GitHub. Already have an account? Sign in to comment