 # Polygons

Two functions to draw polygons:

1) Draws a polygon that is inscribed into an ellipse:

``````function drawInscribedPolygon(mc:MovieClip, n:Number, xr:Number, yr:Number, x:Number, y:Number, ps:Number):Void {
if(ps == undefined) {
ps = Math.PI / (n % 4 ? 2 : 4);
}
for(var t:Number = 0; t < 2 * Math.PI; t += 2 * Math.PI / n) {
var ts:Number = t - ps;
mc[t ? "lineTo" : "moveTo"](x - Math.cos(ts) * xr, y + Math.sin(ts) * yr);
}
mc.lineTo(x - Math.cos(ps) * xr, y + Math.sin(-ps) * yr);
}
//drawInscribedPolygon(mc:MovieClip, n:Number, xr:Number, yr:Number, x:Number, y:Number, [ps:Number]):Void
//mc - The movie clip in which to draw the polygon
//n - The number of sides of the polygon
//xr - The length of the semi-horizontal axis of the circumscribed ellipse ellipse
//yr - The length of the semi-vertical axis of the circumscribed ellipse
//x - The x centre of the circumscribed ellipse
//y - the y centre of the circumscribed ellipse
//ps - The phase shift or angular offset of the inscribed polygons' vertices (ie the rotation)
``````

Example

2) Draw’s a polygon that is equilangular and equilateral (aka a regular polygon). This is also a polygon inscribe in a ellipse, but the ellipse is a circle with the radius defined by the side length of the polygon:

``````function drawRegularPolygon(mc:MovieClip, n:Number, s:Number, x:Number, y:Number, ps:Number):Void {
if(ps == undefined) {
ps = Math.PI / (n % 4 ? 2 : 4);
}
var r:Number = 0.5 * s * (1 / Math.sin(Math.PI / n));
for(var t:Number = 0; t < 2 * Math.PI; t += 2 * Math.PI / n) {
var ts:Number = t - ps;
mc[t ? "lineTo" : "moveTo"](x - Math.cos(ts) * r, y + Math.sin(ts) * r);
}
mc.lineTo(x - Math.cos(ps) * r, y + Math.sin(-ps) * r);
}
//drawRegularPolygon(mc:MovieClip, n:Number, s:Number, x:Number, y:Number, [ps:Number]):Void
//mc - The movie clip in which to draw the polygon
//n - The number of sides of the polygon
//s - The side lengths of the polygon
//x - The x centre of the circumscribed circle
//y - the y centre of the circumscribed circle
//ps - The phase shift or angular offset of the polygons' vertices (ie the rotation)
``````