JUNG2:如何画圆?

发布于 2024-10-12 04:02:01 字数 105 浏览 0 评论 0原文

我需要在 JUNG 中围绕顶点画一个圆。圆由顶点作为圆心和给定的半径 r 定义。

I need to draw a circle around a vertex in JUNG. The circle is defined by the vertex as center and a given radius r.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笔一画续写前缘 2024-10-19 04:02:01

我猜是这样的。这将为您提供具有给定半径的圆的分数。要调整点的分辨率,请根据需要将 x+=0.01 更改为更大/更小的值。要将圆心移动到任意点(p,q),只需将其添加到(x,y)即可,即plot(x+p,y) +q);

double radius = 3;
for (double x = -radius; x <= radius; x += 0.01) {
    double y = Math.sqrt(radius * radius - x * x);
    plot(x, y);//top half of the circle
    plot(x, -y);//bottom half of the circle
}

编辑:看来 JUNG 并不是真正的 XY 图,而是一个网络/图形框架。因此,您所需要做的就是使用提供的布局之一将点布局在一个圆圈中。 CircleLayoutKKLayout 似乎可以解决问题,尽管 CircleLayout 在有很多节点时会给出奇怪的结果。这是完整的示例代码:

//Graph holder
Graph<Integer, String> graph = new SparseMultigraph<Integer, String>();

//Create graph with this many nodes and edges
int nodes = 30;
for (int i = 1; i <= nodes; i++) {
    graph.addVertex(i);
    //connect this vertext to vertex+1 to create an edge between them.
    //Last vertex is connected to the first one, hence the i%nodes
    graph.addEdge("Edge-" + i, i, (i % nodes) + 1);
}

//This will automatically layout nodes into a circle.
//You can also try CircleLayout class
Layout<Integer, String> layout = new KKLayout<Integer, String>(graph);
layout.setSize(new Dimension(300, 300)); 

//Thing that draws the graph onto JFrame
BasicVisualizationServer<Integer, String> vv = new BasicVisualizationServer<Integer, String>(layout);
vv.setPreferredSize(new Dimension(350, 350)); // Set graph dimensions

JFrame frame = new JFrame("Circle Graph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);

我选择了 SparseMultiGraph 因为这就是 JUNG 教程。还有其他类型的图表,但我不确定有什么区别。

您还可以使用可以采用 (x,y) 顶点的 StaticLayout ,然后使用我的原始代码来绘制点,但这对于 JUNG 框架来说不太优雅。不过,取决于您的要求。

Something like this, I guess. This will give you points for circle with given radius. To adjust resolution of points change x+=0.01 to a bigger/smaller value as needed. To move circle centre to an arbitrary point (p,q), just add it to (x,y), that is plot(x+p,y+q);.

double radius = 3;
for (double x = -radius; x <= radius; x += 0.01) {
    double y = Math.sqrt(radius * radius - x * x);
    plot(x, y);//top half of the circle
    plot(x, -y);//bottom half of the circle
}

EDIT: It appears that JUNG is not really an XY-plot but a network/graph framework. So all you need is to layout your points in a circle using one of provided layouts. CircleLayout and KKLayout seem to do the trick, though CircleLayout gives strange results for when there are many nodes. Here's complete sample code:

//Graph holder
Graph<Integer, String> graph = new SparseMultigraph<Integer, String>();

//Create graph with this many nodes and edges
int nodes = 30;
for (int i = 1; i <= nodes; i++) {
    graph.addVertex(i);
    //connect this vertext to vertex+1 to create an edge between them.
    //Last vertex is connected to the first one, hence the i%nodes
    graph.addEdge("Edge-" + i, i, (i % nodes) + 1);
}

//This will automatically layout nodes into a circle.
//You can also try CircleLayout class
Layout<Integer, String> layout = new KKLayout<Integer, String>(graph);
layout.setSize(new Dimension(300, 300)); 

//Thing that draws the graph onto JFrame
BasicVisualizationServer<Integer, String> vv = new BasicVisualizationServer<Integer, String>(layout);
vv.setPreferredSize(new Dimension(350, 350)); // Set graph dimensions

JFrame frame = new JFrame("Circle Graph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(vv);
frame.pack();
frame.setVisible(true);

I have picked SparseMultiGraph because that's what was in JUNG tutorial. There are other types of graphs, but I am not sure what the difference is.

You could also use a StaticLayout that can take (x,y) vertices, then use my original code to plot the points, but that would not be as elegant for JUNG framework. Depends on what your requirements are, however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文