Skip to main content
Participant
December 29, 2011
Answered

Polygon Function

  • December 29, 2011
  • 1 reply
  • 540 views

I'm trying to create a function that draws a polygon with two parameters that are:  number of sides and length of sides. You would input a number for side length and a number for the number of sides. Then it would draw the side length, then turn at a angle based on the number of sides, then draw side length again... until it has finished drawing the polygon based on the number of sides that were put in.

This topic has been closed for replies.
Correct answer kglad

:

function polygonF(sp:Sprite,sides:int,len:int,x:int=0,y:int=0,col:uint=0x000000):void{

    var centralAngle:Number = 2*Math.PI/sides;

    var r:Number = len/2/Math.sin(centralAngle/2);

    with(sp.graphics){

        lineStyle(0,col);

        moveTo(x+r*Math.cos(0),y+r*Math.sin(0));

        for (var i:int = 0; i <= sides; i++) { 

            lineTo (x+r*Math.cos(i*2*Math.PI/sides),y+r*Math.sin(i*2*Math.PI/sides));

        }

    }

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
December 29, 2011

:

function polygonF(sp:Sprite,sides:int,len:int,x:int=0,y:int=0,col:uint=0x000000):void{

    var centralAngle:Number = 2*Math.PI/sides;

    var r:Number = len/2/Math.sin(centralAngle/2);

    with(sp.graphics){

        lineStyle(0,col);

        moveTo(x+r*Math.cos(0),y+r*Math.sin(0));

        for (var i:int = 0; i <= sides; i++) { 

            lineTo (x+r*Math.cos(i*2*Math.PI/sides),y+r*Math.sin(i*2*Math.PI/sides));

        }

    }

}