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.javafor node : Bird_Food x =
200.97455004134613 y = 205.08056219463253for node : Groceries x =
204.4727596409387 y = 206.26252504672223for node : Fish_Food x =
203.22828002758823 y = 202.30400672172576for node : Pet_Food x =
208.8749664329499 y = 203.43454377979435for node : Dog_Food x =
207.72724954244495 y = 202.9273879239392for 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.
Spacing nodes on a circle in a uniform way is equivalent to setting the angular position of the nodes uniformly. Given
Nnodes, you can compute the angle for each node as some multiple of2*pi/Nradians, and simply convert the angle into a position on the circle using trigonometry…