1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
function drawPolygon(centerX, centerY, radius, sides, startAngle) { let angle = startAngle || 0; let points = [] for (let i = 0; i < sides; i++) { x = centerX + radius * Math.sin(angle); y = centerY - radius * Math.cos(angle); angle += 2*Math.PI/sides; points.push({x: x, y: y}); } console.log(points); context.moveTo(points[0].x, points[0].y); for (let i = 1; i < sides; i++) { context.lineTo(points[i].x, points[i].y); } context.closePath(); context.stroke(); }
drawPolygon(100, 150, 100, 3); drawPolygon(300, 150, 100, 4, 45/180*Math.PI); drawPolygon(500, 150, 100, 5); drawPolygon(700, 150, 100, 6, 30/180*Math.PI); drawPolygon(100, 400, 100, 7); drawPolygon(300, 400, 100, 8, 22.5/180*Math.PI); drawPolygon(500, 400, 100, 9); drawPolygon(700, 400, 100, 10, 18/180*Math.PI);
|