xxxxxxxxxx
50
/** @type {Segment} */
let segment;
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
segment = new Segment(createVector(5, 0));
}
function draw() {
background(220);
translate(width / 2, height / 2);
segment.show();
}
function mousePressed() {
let angle = 0;
const original = segment.copy();
let loop = setInterval(() => {
segment = original;
const { vertices } = segment;
const rotated = vertices.map((v) => v.copy().rotate(angle)).reverse();
segment = new Segment(vertices, rotated);
angle += HALF_PI / 5;
if (angle > HALF_PI) clearInterval(loop);
}, 100);
}
class Segment {
/** @param {p5.Vector} v */
constructor(v) {
this.vertices = v;
}
/** @returns {Segment} */
copy() {
return new Segment(this.vertices.map((v) => v.copy()));
}
show() {
for (const v of this.vertices) {
line(0, 0, v.x, v.y);
translate(v.x, v.y);
}
}
}