I have a java component on which I draw 100 by 40 rectangles to represent nodes of a graph. When a new node is added, I would like to be able to position it in a useful position. Currently, I simply choose the next available space on the x-axis. However, this means when I load a graph, I get a large chain of nodes spanning off the view to the right – not ideal!
A better solution may involve adding a node at the point closest to the center-point of the user’s view. I’m sure there are many solutions to this problem I am unable to think of a reasonable way to achieve this.
I have three methods that are relevant to this problem:
positionNode(Node) // which tries to find suitable x,y to place a node
setPos(x,y) // which moves a node to the respective position
findElementAt(x,y) // which returns a node's model if such a node exists at (x,y)
A potential solution may somehow involve spiraling around the center point but I am unable to imagine a good solution for performing this.
Graph rendering is a hard problem which is the subject of a lot of research.
If you want to make the nodes into a sort of a bundle, rather than a line of nodes, the main disadvantage will be that lines coming out of the nodes will get overlapped by the other children.
You can make a square bundle or a round bundle. To make a square bundle, take the square root of the number of nodes and add one. For example, if you have 17 nodes, the sides of the square is 4 + 1 = 5. So you have a 5 x 5 square. Position each node in a cell of the square.
To make a round bundle draw concentric circles. So first node is at center. Then calculate an imaginary circle around that node. Divide the circle into segments by degrees according to how much space you need. Then locate each additional node in the center of each segment.
Note that you might want to overlap nodes. Depending on how your interface works, overlapping might allow you to fit more nodes in.