A very simple method to construct a fractal inspired by this tweet: twitter.com/centrlpo…/1250172108811927552 by @CentrlPotential.
en.wikipedia.org/wiki/chaos_game
#fractal #chaosgame
Log in to post a comment.
// Chaos Game. Created by Reinder Nijhoff 2020 // @reindernijhoff // // https://turtletoy.net/turtle/f4ef806547 // Canvas.setpenopacity(.1); const turtle = new Turtle(); const numVertices = 5; // min=3, max=7, step=1 const lerpDist = 0.61803398875; // min=0.5, max=0.75, step=0.0000001 const mode = 0; // min=0, max=2, step=1 const neighbor = 1; // min=0, max=3, step=1 // create shape with numCorners vertices const vertices = []; for (let i=0; i<numVertices; i++) { const a = (i+1/4) / numVertices * Math.PI * 2; vertices.push([80*Math.cos(a), 80*Math.sin(a)]); } let randomIndex = 0, newRandomIndex = 0, prevRandomIndex; function neighbors(i, j, n) { return i == (j + n) % numVertices || (i + n) % numVertices == j; } function walk(i) { // choose a random corner of the shape do { newRandomIndex = Math.random()*numVertices|0; } while ( (mode == 1 && neighbors(randomIndex, newRandomIndex, neighbor)) || (mode == 2 && prevRandomIndex == randomIndex && neighbors(randomIndex, newRandomIndex, neighbor)) ); prevRandomIndex = randomIndex; randomIndex = newRandomIndex; // jump turtle somewhere halfway (at lerpDist) between current position of turtle // and the randomly chosen vertex const c = [turtle.x(), turtle.y()]; const n = vertices[newRandomIndex]; turtle.jump([lerpDist*n[0] + (1-lerpDist)*c[0], lerpDist*n[1] + (1-lerpDist)*c[1]]); // draw small circle turtle.circle(.1); return i < 250000; }