# Polygons

**URL:** <https://forum.kirupa.com/t/polygons/201991>\
**Category:** open source\
**Created:** [September 26, 2006, 8:50pm UTC](https://forum.kirupa.com/t/polygons/201991 "2006-09-26T20:50:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [September 26, 2006, 8:50pm UTC](https://forum.kirupa.com/t/polygons/201991/1 "2006-09-26T20:50:02Z")

</div>

Two functions to draw polygons:

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

```auto
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](http://img235.imageshack.us/my.php?image=polygonqt1.swf)

**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:

```auto
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)

```

[Example](http://img217.imageshack.us/my.php?image=regularpolygonoq1.swf)  
[URL=“[http://img235.imageshack.us/my.php?image=polygonqt1.swf](http://img235.imageshack.us/my.php?image=polygonqt1.swf)”]
