Actionscript 3 Designer Toolkit

More design, less programming

Actionscript 3 Designer Toolkit header image 2

Drawing regular polygons: the Polygon Class

January 18th, 2010 · No Comments · AS3 Custom Classes

The Polygon class is a tool for drawing regular polygons with three or more sides. Parameters include the radius ( distance from the center of the polygon to any vertex), the number of sides, line weight and color and fill color.

The program below renders the pattern of hexagons displayed above.

import com.dtk.Polygon;
 
// init x, y position var's
var xPos:uint;
var yPos:uint = 30;
 
// loop through rows
for ( var i:uint=0; i<5; i++ )
{
	// if i is odd, indent first polygon
	if ( i%2 == 0 )
	{
		xPos = 30;
	}
	else
	{
		xPos = 65;
	}
 
	// loop through columns
	for ( var j:uint=0; j<5; j++ )
	{
		// draw 6-sided polygon with radius of 20px
		var h:Polygon = new Polygon( 20, 6, 1, 0x666666, 0xdddddd );
		h.x = xPos;
		h.y = yPos;
		h.rotation = 30;
		addChild( h );
 
		// increment x position
		xPos += 70;
	}
	// increment y position
	yPos += 20;
}

Download files: Polygon.zip (146)

If you found this post helpful please consider a Retweet.  Thanks…

Post to Twitter

Tags: ··

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment

Spam Protection by WP-SpamFree