Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: graphs drawing using coordinates (need to space out what I draw)

1. ## 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);

}```
Thanks!

Nicky

• Originally Posted by nickyfraggle
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.
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

Code:
`R = ((n * r) + ((n-1) * dOff)) / (2 * Math.PI);`
now that you know the value of R you can start calculating the position of the individual surrounding circles

Code:
`delta = (2 * Math.PI) / n;`
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 circles

•

#### Posting Permissions

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