1 2 3 4 5 6 7 8 |
MovieClip.prototype.drawPolygon = function(sides,size){ var p = Math.PI/180; var r = 360/sides; var s = size/2; var n = -r/2; this.moveTo(Math.sin(p*n)*s,Math.cos(p*n)*s); for(;n<360;n+=r)this.lineTo(Math.sin(n*p)*s,Math.cos(n*p)*s); } |

I figured this out while trying to grok how a circle is drawn. After smashing my face into my monitor for a couple months, it occured to me that virtually every equilateral shape is a circle with less sides. Imagine trying to draw a circle when all you have is three sticks… you get a triangle. The same thing with four sticks results in a square.

The end result was the above function: pass the number of sides (3 for triange, 4 for square, 8 for an octagon, etc… up to 360 for a circle), and a size, and you”ll get back a perfect polygon. Notice the lack of switch statements or if/else logic; the function attempts to draw a circle with as many passes as are defined in the size argument, so the natural product is the shape produced.

for(;n<360;n+=r)this.lineTo(Math.sin(n*p)*s,Math.cos(n*p)*s);

not sure about this line for as2

It’s been a long time since I wrote this, but AFAIK it should work. Is there a specific issue you’re describing?

sorry no problem i misread the code but worked it out anyway thx

No problem; glad you got it working.