Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for drawing a 5 point star

I'm currently working on a solution for drawing a standard 5-point star on the canvas using JavaScript. I'm part way there but can't figure it out entirely. I'd appreciate any tips or pointers anyone might have.

like image 356
Luke Haas Avatar asked Dec 15 '22 14:12

Luke Haas


1 Answers

I made some changes to the code that Chris posted so it would work for me:

var alpha = (2 * Math.PI) / 10; 
var radius = 12;
var starXY = [100,100]

canvasCtx.beginPath();

for(var i = 11; i != 0; i--)
{
    var r = radius*(i % 2 + 1)/2;
    var omega = alpha * i;
    canvasCtx.lineTo((r * Math.sin(omega)) + starXY[0], (r * Math.cos(omega)) + starXY[1]);
}
canvasCtx.closePath();
canvasCtx.fillStyle = "#000";
canvasCtx.fill();

Hope it helps...

like image 149
Nycholas Weissberg Avatar answered Dec 29 '22 04:12

Nycholas Weissberg