dmschenk
dmschenk

Reputation: 389

Dynamically spacing numbers around a circle

I'm trying to figure out how to dynamically place numbers around a circle (similar to a clock face) but dynamically so if the number of numbers around the circle is 5 or 27.. they would space out correctly.

I found some code (below) that looked like it might help but I'm having trouble implementing it. I don't know how I actually tie this back to the circle and numbers.

Any help would be much appreciated. Thanks

function getNPointsOnCircle( center:Point, radius:Number, n:Number = 10 ) : Array

{

var alpha:Number = Math.PI * 2 / n;
var points:Array = new Array( n );

var i:int = -1;
while( ++i < n )
{
    var theta:Number = alpha * i;
    var pointOnCircle:Point = new Point( Math.cos( theta ) * radius, Math.sin( theta ) * radius );
    points[ i ] = center.add( pointOnCircle );
}

return points;

}

Upvotes: 3

Views: 492

Answers (1)

Allan
Allan

Reputation: 3314

That code works perfectly. This is how to use it:

var center:Point = new Point(100,100);
var radius = 100;
var n = 10


var p:Array = getNPointsOnCircle( center, radius, n)


var myContainer:Sprite = new Sprite();
myContainer.graphics.lineStyle(1);

for (var k = 0; k <p.length;k++)
{
    myContainer.graphics.drawCircle(p[k].x,p[k].y,5);
}

addChild(myContainer);

Upvotes: 3

Related Questions