A Flash Developer Resource Site

# Thread: trying to find coordinates for points around a circle

1. ## trying to find coordinates for points around a circle

How do I find the x,y coordinates for nNumber of equally spaced points around the circumference of a circle?  Reply With Quote

2. 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;    }  ```  Reply With Quote

3. Thanks, worked great!  Reply With Quote

4. hi, could any one tell me how i could run this in java as when i compile it, it throws an error.  Reply With Quote

5. This is a Flash forum. Try asking Originally Posted by hafunui How do I find the x,y coordinates for nNumber of equally spaced points around the circumference of a circle?
in a Java forum.  Reply With Quote

6. 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.  Reply With Quote

7. The formula for a circle:

(x-h)^2+(y-k)^2=r^2

(h,k) is the center

http://board.flashkit.com/board/show...56#post4241856  Reply With Quote

8. 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....

PHP 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;          }  }  ```
Then I'm using this to call it in my .fla

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 {  ```  Reply With Quote

9. you need to put static method definitions in a class definition, so wrap the method in "public class YourClass { }" then reference via YourClass.getNPointsOnCircle.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap 