首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JUNG2:如何画圆?

JUNG2:如何画圆?
EN

Stack Overflow用户
提问于 2011-01-13 21:13:46
回答 1查看 1K关注 0票数 0

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-13 21:52:45

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

代码语言:javascript
复制
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会给出奇怪的结果。下面是完整的示例代码:

代码语言:javascript
复制
//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 tutorial中的东西。还有其他类型的图表,但我不确定有什么不同。

您也可以使用可以获取(x,y)顶点的StaticLayout,然后使用我的原始代码来绘制点,但这对于JUNG框架来说就不那么优雅了。然而,这取决于您的需求。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4680434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档