Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save martijnkunstman/db9cd5eb4e9397d11fdfb8ba283efe6f to your computer and use it in GitHub Desktop.
Sierpiński triangle - Chaos game
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);
}
}
<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