user2635152
user2635152

Reputation:

Creating a star shape

I'm trying to reproduce the Chilean flag in FLash via Actionscript. I've got the blue box and the red rectangle down, now all I need to figure out is how to make a accurate start shape to finish it off.

Do you have have any pointers in creating a star shape via Actionscript 3?

My code so far:

//blue box
var blue:Sprite = new Sprite();
addChild(blue);
blue.graphics.lineStyle();
blue.graphics.beginFill(0x0039A6,1);
blue.graphics.moveTo(0,0);
blue.graphics.lineTo(0,320);
blue.graphics.lineTo(320,320);
blue.graphics.lineTo(320,0);

//red rectanle
var red:Sprite = new Sprite();
addChild(red);
red.graphics.lineStyle(); 
red.graphics.beginFill(0xD52B1E,1);
red.graphics.moveTo(0,320);
red.graphics.lineTo(0,640);
red.graphics.lineTo(960,640);
red.graphics.lineTo(960,320);

Chilean flag: enter image description here

Any help would be greatly appreciated.

Upvotes: 0

Views: 427

Answers (1)

Mark Lakata
Mark Lakata

Reputation: 20903

This is not flash, but pseudocode for a star

var outerSize = 10;
var innerSize = 5;
var rotation = 3.141592/2;
var delta = 2*3.141592/5.0;
var xpos = 100;
var ypos = 100;

for(var point=0;point<5;point++)
{
   red.graphics.moveto(outerSize * cos(delta*point + rotation) + xpos, 
                       outerSize * sin(delta*point + rotation) + ypos);
   red.graphics.lineto(innerSize * cos(delta*(point + 0.5) + rotation) + xpos, 
                       innerSize * sin(delta*(point + 0.5) + rotation) + ypos); 
   red.graphics.lineto(outerSize * cos(delta*(point + 1.0) + rotation) + xpos, 
                       outerSize * sin(delta*(point + 1.0) + rotation) + ypos);
}

If you are going to fill the polygon, you probably need to do something else, but here are the coordinates of the polygon.

If you are plotting lots of stars, I suggest precalculating the coordinates and storing them in an array. cos and sin can be expensive operations.

Upvotes: 1

Related Questions