0

我通过这样的顶点邻接关系在矩阵上有一个无向图;

    /*    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}};

我想在坐标系上绘制这个图。通过任何方法(力导向,弹簧与)给出每个顶点位置(x,y)的算法是什么?我只问伪代码,而不是任何要绘制的库或软件。谢谢。

4

2 回答 2

0

这是从prefuse库修改的圆形图布局:

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);
        }
    }
}

如果需要,您也可以将其更改为使用浮点数或双精度数。

于 2012-03-21T19:07:03.300 回答
0

这是使用 as3 的描述良好的算法。我解决了我的问题。谢谢。http://blog.ivank.net/force-based-graph-drawing-in-as3.html

于 2012-03-23T01:57:50.100 回答