A Pen by Martijn Kunstman on CodePen.
Created
March 20, 2021 23:13
-
-
Save martijnkunstman/7c9e51bf0fbb0e38151a469b3f7078fd to your computer and use it in GitHub Desktop.
Sierpiński triangle - Cellular automata : rule 90
This file contains hidden or 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
| 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; | |
| } |
This file contains hidden or 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
| <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.3.0/p5.min.js"></script> |
This file contains hidden or 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
| <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