题
我需要在一个顶点周围绘制一个圆 荣格. 。圆由顶点定义为中心和给定的半径r。
解决方案
我想这样的事情。这将为您提供圆圈的积分 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
因为那就是里面的 荣格教程. 。还有其他类型的图形,但我不确定有什么区别。
您也可以使用 StaticLayout
可以接受 (x,y)
顶点,然后使用我的原始代码来绘制这些要点,但这对于荣格框架来说并不那么优雅。但是,取决于您的要求。
不隶属于 StackOverflow