Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-20-2008, 08:08 PM   PM User | #1
nickyfraggle
Regular Coder

 
Join Date: Mar 2006
Posts: 196
Thanks: 27
Thanked 2 Times in 2 Posts
nickyfraggle is an unknown quantity at this point
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
nickyfraggle is offline   Reply With Quote
Old 04-21-2008, 05:13 AM   PM User | #2
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
Quote:
Originally Posted by nickyfraggle View Post
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
__________________
You never have to change anything you got up in the middle of the night to write. -- Saul Bellow
shyam is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:44 AM.


Advertisement
Log in to turn off these ads.