views:

141

answers:

1

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;

}

+2  A: 

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);
Allan
That's great Thanks so much! I added what you have to the other code and it works fine..... now I just gotta wrap my head around it! :)If I have this right the getNPointsOnCircle function builds the circle with your params, myContainer builds the position markers.Changing myContainer is where I would put code to add numbers or anything else? I think I got it!Thanks again
dmschenk
yep thats it! so if you wanted to number those markers all you would have to do is place them (textfield for example) at those locations in the myContainer sprite. Eg var myText:TextField = new TextField(); myText.text = "1" ;myContainer.addChild(myText);myText.x = //choose one of the array points and assign its xmyText.y = //choose the same array point and its its y
Allan