xxxxxxxxxx
68
const EXPORT = false; //change to true to export an .svg
const CONVOLUTIONS = 5;
const FRAMES_PER_CONVOLUTION = 60;
let vertices = [];
function setup() {
if (EXPORT) {
createCanvas(900, 900, SVG);
} else {
createCanvas(900, 900);
}
noFill();
stroke(0);
strokeWeight(1);
angleMode(DEGREES);
}
function draw() {
background(255);
drawSpiral();
if (frameCount === CONVOLUTIONS * FRAMES_PER_CONVOLUTION) {
capSpiral(CONVOLUTIONS - 1);
noLoop();
}
beginShape();
let xAverage = 0;
let yAverage = 0;
let index = 0;
for (let v of vertices) {
vertex(v.x, v.y);
xAverage += v.x;
yAverage += v.y;
// ellipseMode(CENTER);
// if (index++ > 40 && index++ % 8 == 0) {
// ellipse(v.x, v.y, 16, 16);
// }
}
ellipse(xAverage / vertices.length, yAverage / vertices.length, 8, 8);
endShape();
if (frameCount === CONVOLUTIONS * FRAMES_PER_CONVOLUTION && EXPORT) {
save();
}
}
//This function draws a spiral using polar coordinates that are converted to cartesian coordinates.
function drawSpiral() {
let x = width >> 1;
let y = height >> 1;
let angle = map(frameCount, 0, FRAMES_PER_CONVOLUTION, 0, 360);
let r = frameCount;
let dx = r * cos(angle); //CAH
let dy = r * sin(angle); //SOH
vertices.push(createVector(x + dx, y + dy));
}
//This functions creates a line between the last and second-to-last convolutions.
function capSpiral(c) {
let x = width >> 1;
let y = height >> 1;
let dx = c * FRAMES_PER_CONVOLUTION * cos(0); //CAH
let dy = c * FRAMES_PER_CONVOLUTION * sin(0); //SOH
vertices.push(createVector(x + dx, y + dy));
}