Hi... This is a more of a theory question, so... I don't have any code to post and I 'm not asking for any. I'm supposed to display a directed graph and with circular nodes and straight line edges. I have to do it with the help of an adjacency matrix. An example of such,
1 2 3
1 0 1 0
2 0 0 1
3 1 0 0
I have chosen SFML for the graphical display and I have no problem converting the above matrix into a displayable graph consisting of circles as nodes and lines as edges. I, however, have one small problem. My program has functions to add and remove nodes and their edges. Based on that, my graph has to be updated accordingly. When the user adds or removes nodes, the program will update the graph. When the graph is updated, I have to make it in such a way that, no matter how many nodes are there, they should be all equidistant to each other. For example, if the user adds three nodes, the three circles should be in the form of an equilateral triangle. If he adds one more node later or adds four nodes at the start, the circles should be in the form of a square. Similarly, pentagon, hexagon, etc. You get the picture. Now, my question is... How would I write the code such that the window draws them equidistant to each other? Is there a predefined function or should I work out some extra logic on it? Please clarify this for me. Thank you.
(http://graphviz-dev.appspot.com/get_preview?id=45534976-bb4b-4e2f-9018-39fa1a15d6ac) (http://graphviz-dev.appspot.com/get_preview?id=105a224e-8c9d-4e59-a0e0-80dfd91ef072)
UPDATE: Sorry for the double post, but here's an update. I took Jesper's suggestion and looked on how to create the circular path using the circle equations he linked above. For any point on the circumference,
x = a + r(cos t)
y = b + r(sin t)
where (x,y) is the point, (a,b) is the center, t is the angle with X-axis and r is the radius.
I used these equations and based on them, I wrote the code to draw the nodes on the circular path. And it worked like a charm. ;D
Here's the code if anyone else wants to do anything similar:
Window.clear(sf::Color(255, 255, 255));
std::vector<sf::CircleShape> Circles;
sf::CircleShape Circle(30);
int n = 10, k = 1;
float X, Y;
for(int i = 0; i < n; i++)
{
Circles.push_back(Circle);
}
for(std::vector<sf::CircleShape>::iterator it = Circles.begin(); it != Circles.end(); it++)
{
it->setFillColor(sf::Color(0, 0, 0));
X = 200.0f + 100 * (float)cos(k * (2 * PI/n));
Y = 200.0f + 100 * (float)sin(k * (2 * PI/n));
it->setOrigin(it->getGlobalBounds().width/2, it->getGlobalBounds().height/2);
it->setPosition(X, Y);
Window.draw(*it);
k++;
}
I know the code is very crude and very messy. But the logic behind it is the same. I only did that to test how the output came. It's better to use loops for many nodes. EDIT: I changed the code to make it more optimal with the help of for loops and STL containers such as vectors. The window size I took was 400x400 so the center is (200, 200). Here's what I got for three nodes and four nodes.
(http://i18.photobucket.com/albums/b142/whitedino/triangle.png)(http://i18.photobucket.com/albums/b142/whitedino/square.png)
Pretty neat, right? At least it serves my purpose. Thanks, Jesper Juhl and eXpl0it3r! For now, I am satisfied with what I got, but please don't close the topic yet. If I hit any more roadblocks with this problem again, I shall ask again. Thank you. Have a nice day! :)