Quickreference: display elements in a circle

How to display elements in a circle? A small function to do that is:

public static function circle(items:Array, center:Point, distance:Number = -1, initialAngle:Number = 0):void {
var step:Number = (2* Math.PI) / items.length;
var angle:Number = initialAngle;
for each (var d:DisplayObject in items) {
if (distance < 0) distance = d.height+10 / Math.sin(step);
d.x = center.x + distance * Math.cos(angle);
d.y = center.y + distance * Math.sin(angle);
angle += step;
}
}

Parameters are:

  • items: the items to be displayed a a circle
  • center: the center of the circle
  • distance: the distance from the center
  • initialAngle: the angle of the first item. Zero means that the item wil be aligned orizontally with the center, on his right.

Here’s an example:

One Response

  1. Silma:

    Interesting… I’m not that into flash or actionscript, but I have a friend or two who might find this useful. Also, myself in the future, why not.

    P.s: ciao Carlo :)

Do you want to leave a comment?