Graph drawing algorithm
-
25-05-2021 - |
Question
I have an undirected graph on matris by vertex adjacency relations like that;
/* a b c d
* a -1 0 1 1
* b 0 -1 1 1
* c 1 1 -1 1
* d 1 1 1 -1
*
*/
int G[4][4] = {{-1, 0, 1, 1},
{ 0,-1, 1, 1},
{ 1, 1,-1, 1},
{ 1, 1, 1,-1}};
I want to draw this graph on cordinate system. What's the algorithm that gives each vertex position (x,y) by any method(force-directed, spring vs)? I just ask the pseudocode, not any library or software to draw. Thanks.
La solution 2
Here is the well-decribed algorithm with as3. I solved the my problem. Thanks. http://blog.ivank.net/force-based-graph-drawing-in-as3.html
Autres conseils
Here's a circle graph layout modified from the prefuse library:
void layoutPoints(int rows, int cols, Point **coordinates, Rectangle maxSize)
{
int nn = rows * cols;
int width = maxSize.width;
int height = maxSize.height;
int centerX = maxSize.x + (width / 2);
int centerX = maxSize.y + (width / 2);
int radius = 0.45 * (height < width ? height : width);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < width; j++)
{
double angle = (2 * M_PI * i) / nn;
double x = cos(angle) * radius + centerX;
double y = sin(angle) * radius + centerY;
coordinates[i][j].x = round(x);
coordinates[i][j].y = round(y);
}
}
}
You could change this to use floats or doubles if you needed to as well.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow