Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 2 of 2

04202008, 08:08 PM #1
 Join Date
 Mar 2006
 Posts
 200
 Thanks
 27
 Thanked 2 Times in 2 Posts
graphs drawing using coordinates (need to space out what I draw)
Hello,
I am drawing directed graphs using Java.
At the moment, I'm struggling to work out how to space out the circular nodes I am drawing.
I will be drawing on circle in the center, and several circles around it. The number of circles will vary, so my program takes the number of circles to draw as input, and I need the program to decide how to space them out so they don't overlap. (Within reason, obviously if I had 100 nodes to draw it would be impossible to draw them without overlaps!)
I have a class to draw circles, it takes 2 coordinates as input... the x and y of where to start drawing it, and draws the circle.
I just need to know how to write something that will space out the circles drawn depending on how many their are. I think I need some kind of algorithm but I don't know how to do this.
Any help is really appreciated, or links to anywhere that might help.
Here is my circles class incase it helps.
Code:/* DRAW AND LABEL CIRCLES */ //draw circles. needs input of setName and what the circle is to be labelled as. This is elementLabel. Element is the object returned and might not be needed. xy are Coordinates that say where to start drawing the circle public class draw(Graphics g, String SetName, String elementLabel, element, x, y) { //draw circle MouseXN and YN are both set to 20 to draw a circle of this size g.drawOval(x,y,mouseXN,mouseYN); }
Nicky
04212008, 05:13 AM
#2
the algorithms are pretty simple if you understand your math and trigonometry right...
assuming
n is the number of circles you want to draw
(x, y) is the center of the central circle which has a radius of cr
dOff is the average space that you want between individual surrounding circles
r is the radius of the surrounding circles
the radius of the external circle on which the surrounding circles will be positioned can be calculated as
now that you know the value of R you can start calculating the position of the individual surrounding circlesCode:R = ((n * r) + ((n1) * dOff)) / (2 * Math.PI);
using delta as the increment you can get the polar coordinates for centers for each of the surrounding circles like (R, delta), (R, 2*delta), (R, 3*delta)...you convert the polar coordinates to cartesian coordinates and add offsets for the central circles radius and draw the surrounding circlesCode:delta = (2 * Math.PI) / n;
You never have to change anything you got up in the middle of the night to write.  Saul Bellow