xxxxxxxxxx
67
/*
----- Coding Tutorial by Patt Vira -----
Name: Chaos Game
Video Tutorial: https://youtu.be/0jX3eOrv8x8
Connect with Patt: @pattvira
https://www.pattvira.com/
----------------------------------------
*/
let vertices = [];
let pt; let count = 0;
let countMax = []; let lerpVal = [];
let index = 2;
function setup() {
createCanvas(400, 400);
background(220);
angleMode(DEGREES);
countMax = [3, 5, 6, 7, 8, 9, 10];
lerpVal = [0.5, 0.618, 0.667, 0.692, 0.707, 0.742, 0.764];
for (let i=0; i<countMax[index]; i++) {
let angle = 360/countMax[index] * i;
let r = width/2;
let x = width/2 + r*cos(angle);
let y = height/2 + r*sin(angle);
vertices[i] = createVector(x, y);
}
// vertices[0] = createVector(width/2, 0);
// vertices[1] = createVector(0, height);
// vertices[2] = createVector(width, height);
pt = createVector(random(0, width), random(0, height));
}
function draw() {
for (let i=0; i<100; i++) {
// if (count == countMax) {
noFill();
beginShape();
for (let i=0; i<vertices.length; i++) {
vertex(vertices[i].x, vertices[i].y);
}
endShape(CLOSE);
fill(0);
point(pt.x, pt.y);
let vt = random(vertices);
pt.x = lerp(pt.x, vt.x, lerpVal[index]);
pt.y = lerp(pt.y, vt.y, lerpVal[index]);
}
// }
}
// function mousePressed() {
// if (count < countMax) {
// vertices.push(createVector(mouseX, mouseY));
// }
// count += 1;
// }