tags:

views:

58

answers:

2

Hi all:

I am working on a simple game in Java, everything goes fine, except I have a situation where I need to evenly spread out nodes on the circumference of a circle.

For instance, if there are exactly 4 nodes, then one would go straight to the north, one to the south, one to the east and one to the west. Of course there could be an "offset", i.e., the first node would go slightly away from the north, but the 90 degree angle between two adjacent nodes and the center of circle must be kept.

Below is the code I got so far.

private void randomizePositions(Set<Node> set)
{
    Random rand = new Random();
    for(Node n : set)
    {
        n.x = XCENTER + rand.nextDouble() * MINRADIUS;
        n.y = YCENTER + rand.nextDouble() * MINRADIUS;
        System.out.println("for node : " + n.lbl + " x = " + n.x + " y = " +n.y);            
    }
}

Yes, the positions for the nodes in the set are randomized, as shown in the test output:

================================================

~/Desktop/applet $ javac Graph.java

~/Desktop/applet $ appletviewer Graph.java

for node : Bird_Food x = 200.97455004134613 y = 205.08056219463253

for node : Groceries x = 204.4727596409387 y = 206.26252504672223

for node : Fish_Food x = 203.22828002758823 y = 202.30400672172576

for node : Pet_Food x = 208.8749664329499 y = 203.43454377979435

for node : Dog_Food x = 207.72724954244495 y = 202.9273879239392

for node : Cat_Food x = 209.55574149936908 y = 209.61827066724257

================================================

So I am wondering how could I calculate the positions correctly, given only the total number of nodes in a set? Any idea?

Many thanks to the suggestions in advance.

+4  A: 

Spacing nodes on a circle in a uniform way is equivalent to setting the angular position of the nodes uniformly. Given N nodes, you can compute the angle for each node as some multiple of 2*pi/N radians, and simply convert the angle into a position on the circle using trigonometry...

Mark E
@Mark: emmm... this is definitely a mathmatical approach :) Thought about this but I just wasn't sure if there would be a better way...
Michael Mao
...or 360/n degrees between each node.
Tony Ennis
+1  A: 

Same idea posted by Mark comment, but in Java (lots of room for improving this code) :

public static List<Node> question4002160(int n, double r){
        List<Node> resp = new ArrayList<Node>();
        double radius = rand.nextDouble() * r;
        double angleInRadians = rand.nextDouble()*(2*Math.PI/n);
        double nangleInRadians = angleInRadians;
        Node node = new Node();
        node.x = XCENTER + Math.cos( angleInRadians ) * radius;
        node.y = YCENTER + Math.sin( angleInRadians ) * radius;
        resp.add(node);
        for(int i=0; i<n-1; i++){
                nangleInRadians += angleInRadians;
                radius = rand.nextDouble() * r;
                node = new Node();
                node.x = XCENTER + Math.cos( nangleInRadians ) * radius;
                node.y = YCENTER + Math.sin( nangleInRadians ) * radius;
                resp.add(node);
        }

        return resp;
    }
xboard