xxxxxxxxxx
69
let distance = 180;
let nextAngle;
let numPoints = 6;
function setup() {
createCanvas(400, 400);
createButton("Save Canvas").mousePressed(save);
createButton("Increment").mousePressed(() => {
numPoints++;
resetSketch();
});
createButton("Decrement").mousePressed(() => {
numPoints--;
resetSketch();
});
}
function calcX(angle) {
return sin(angle) * distance;
}
function calcY(angle) {
return cos(angle) * distance;
}
function resetSketch() {
background(220);
let angle = 0;
let points = [];
for (let i = 0; i <= numPoints; i++) {
angle += TWO_PI / numPoints;
points.push(angle);
}
for (let index = 0; index <= points.length; index++) {
fill("red");
angle = points[index];
noStroke();
// circle(calcX(angle), calcY(angle), 10);
// Connect the adjacent points with line
if (index == points.length) {
nextAngle = points[0];
} else {
nextAngle = points[index + 1];
}
stroke("black");
line(calcX(angle), calcY(angle), calcX(nextAngle), calcY(nextAngle));
// Connect all other points with line
for (let i2 = index; i2 <= points.length; i2++) {
if (i2 == index) {
continue;
}
a1 = points[index];
a2 = points[i2];
line(calcX(a1), calcY(a1), calcX(a2), calcY(a2));
}
}
text(numPoints, -width/2 + 5, -height/2+15);
}
function draw() {
translate(width / 2, height / 2);
resetSketch();
noLoop();
}