Dynamic Polygon Generation
6
Generate polygons dynamically with JavaScript. Change the number of sides and watch the shape update.
function generatePolygon(numSides) {
const points = [];
for (let i = 0; i < numSides; i++) {
const angle = (i * 360 / numSides - 90) * (Math.PI / 180);
const x = 50 + 40 * Math.cos(angle);
const y = 50 + 40 * Math.sin(angle);
points.push(`${x}% ${y}%`);
}
return `polygon(${points.join(", ")})`;
}