簡體   English   中英

JUNG2:如何畫圓?

[英]JUNG2: How to draw a circle?

我需要在JUNG的頂點周圍畫一個圓。 該圓由頂點定義為中心和給定半徑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圖,而是網絡/圖形框架。 因此,您所需要做的就是使用提供的布局之一將點布置成圓形。 CircleLayoutKKLayout似乎可以解決問題,盡管對於有許多節點的情況, 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM