A Pen by Martijn Kunstman on CodePen.
Created
March 20, 2021 23:12
-
-
Save martijnkunstman/db9cd5eb4e9397d11fdfb8ba283efe6f to your computer and use it in GitHub Desktop.
Sierpiński triangle - Chaos game
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 points = [ | |
| { x: width / 2, y: 0 }, | |
| { x: 0, y: height }, | |
| { x: width, y: height } | |
| ]; | |
| let pointNow = { x: width / 2, y: height / 2 }; | |
| /* | |
| let points = [ | |
| { x: 0, y: 0 }, | |
| { x: width, y: 0 }, | |
| { x: 0, y: height }, | |
| { x: width, y: height }, | |
| {x: width/2, y:height/2} | |
| ]; | |
| */ | |
| function setup() { | |
| createCanvas(width, height); | |
| stroke('rgba(0,0,0,0.25)'); | |
| } | |
| function draw() { | |
| for (let i = 0; i < 1000; i++) { | |
| let pointNext = points[Math.floor(Math.random() * points.length)]; | |
| pointNow = { | |
| x: (pointNext.x + pointNow.x) / 2, | |
| y: (pointNext.y + pointNow.y) / 2 | |
| }; | |
| point(pointNow.x, pointNow.y); | |
| } | |
| } |
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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment