Skip to content

Instantly share code, notes, and snippets.

@martijnkunstman
Created March 20, 2021 23:13
Show Gist options
  • Select an option

  • Save martijnkunstman/7c9e51bf0fbb0e38151a469b3f7078fd to your computer and use it in GitHub Desktop.

Select an option

Save martijnkunstman/7c9e51bf0fbb0e38151a469b3f7078fd to your computer and use it in GitHub Desktop.
Sierpiński triangle - Cellular automata : rule 90
let width = 700;
let height = 700;
let ruleset = [0, 1, 0, 1, 1, 0, 1, 0];
let data = [];
let x = 0;
function setup() {
createDataStart();
createCanvas(width, height);
}
function draw() {
for (i = 0; i < data.length; i++) {
if (data[i]) {
point(x, i);
}
}
x=x+1;
data = findResult();
}
function createDataStart() {
for (i = 0; i < height; i++) {
data.push(0);
}
data[Math.round(height / 2)] = 1;
}
function findResult() {
let array = [];
for (j = 0; j < height; j++) {
outcome = 0;
if (j == 0) {
element1 = data[height - 1];
} else {
element1 = data[j - 1];
}
element2 = data[j];
if (j == height - 1) {
element3 = data[0];
} else {
element3 = data[j + 1];
}
result = element1 + "-" + element2 + "-" + element3;
if (result == "0-0-0") {
outcome = ruleset[0] ? 0 : 1;
}
if (result == "0-0-1") {
outcome = ruleset[1] ? 0 : 1;
}
if (result == "0-1-0") {
outcome = ruleset[2] ? 0 : 1;
}
if (result == "0-1-1") {
outcome = ruleset[3] ? 0 : 1;
}
if (result == "1-0-0") {
outcome = ruleset[4] ? 0 : 1;
}
if (result == "1-0-1") {
outcome = ruleset[5] ? 0 : 1;
}
if (result == "1-1-0") {
outcome = ruleset[6] ? 0 : 1;
}
if (result == "1-1-1") {
outcome = ruleset[7] ? 0 : 1;
}
array.push(outcome);
}
return array;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.0/p5.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment