How do I find the x,y coordinates for nNumber of equally spaced points around the circumference of a circle?
Printable View
How do I find the x,y coordinates for nNumber of equally spaced points around the circumference of a circle?
you make use of 2 facts. first, there are 2 pi radians in a circle- so to divide the circle up evenly you find an interval of 2 pi / n. secondly, a point in polar coordinates (radius,angle) can be converted to cartesian coordinates (x,y) by:
( Math.cos( angle ) * radius, Math.sin( angle ) * radius ). The algorithm below adds a center argument so that you can specify the circle's position.
PHP Code:public static 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;
}
Thanks, worked great!
hi, could any one tell me how i could run this in java as when i compile it, it throws an error.
thanks in advance
Hi
Can someone please tell me how I might use this function to actually display something around a circle? say for instance I want to display numbers around the circle.
I see the math and how it works fine but I'm not making the connection of how to implement it to a display.
Please forgive me I'm trying to get a grip on ActionScript..
Any help would be much appreciated.
The formula for a circle:
(x-h)^2+(y-k)^2=r^2
(h,k) is the center
r is the radius
dmschenk. I can help you get a grip:
http://board.flashkit.com/board/show...56#post4241856
newblack, I'm trying to use your code with AS3, but whereas I'm fairly good with AS2 I'm getting the usual confusing errors in AS3!
this is the class I'm creating with your code....
Then I'm using this to call it in my .flaPHP Code:package getNPointsOnCircle
{
// class contents (methods and properties) will go here
public static 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;
}
}
getNPointsOnCircle(0,100,10);
trace("points="+points)
only problem is... getting a nasty error I don't really understand (I'm a beginner with AS3)
PHP Code:getNPointsOnCirlce.as, Line 6 1012: The static attribute may only be used on definitions inside a class
Source: public static function getNPointsONCircle(center:Point, radius:Number, n:Number=10) : Array {
you need to put static method definitions in a class definition, so wrap the method in "public class YourClass { }" then reference via YourClass.getNPointsOnCircle.