我需要在JUNG中的一个顶点周围画一个圆。圆由顶点定义为圆心和给定的半径r。
发布于 2011-01-13 21:52:45
大概是这样吧,我猜。这将给出给定radius的圆的点数。要调整点的分辨率,请根据需要将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图,而是一个网络/图形框架。因此,您所需要的就是使用提供的布局之一在圆圈中布局您的点。CircleLayout和KKLayout似乎做到了这一点,尽管当有很多节点时,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 tutorial中的东西。还有其他类型的图表,但我不确定有什么不同。
您也可以使用可以获取(x,y)顶点的StaticLayout,然后使用我的原始代码来绘制点,但这对于JUNG框架来说就不那么优雅了。然而,这取决于您的需求。
https://stackoverflow.com/questions/4680434
复制相似问题