Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    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);
    
    }
    Thanks!

    Nicky

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    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


  •  

    Posting Permissions

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