[英]JUNG2: How to draw a circle?
我猜是这样的。 这将为您提供给定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教程中的内容 。 还有其他类型的图,但是我不确定有什么区别。
您还可以使用可以获取(x,y)
顶点的StaticLayout
,然后使用我的原始代码绘制点,但是对于JUNG框架而言,这不是那么优雅。 但是,取决于您的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.