Как-то так, наверное.Это даст вам очки за круг с данным 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 .Существуют и другие типы графиков, но я не уверен, в чем разница.
Вы также можете использовать StaticLayout
, который может принимать (x,y)
вершин, затем использовать мой исходный код для построения точек, ноэто не было бы так элегантно для JUNG Framework.Однако зависит от ваших требований.