JUNG2: Come disegnare un cerchio?
Domanda
Ho bisogno di disegnare un cerchio intorno ad un vertice in JUNG . Il cerchio è definito dal vertice come centro e un determinato raggio r.
Soluzione
Qualcosa di simile, credo. Questo vi darà punti per cerchio con data radius
. Per regolare la risoluzione dei punti di cambiamento x+=0.01
ad un valore più grande / piccolo, se necessario. Per spostare il centro del cerchio di un punto arbitrario (p,q)
, basta aggiungerlo al (x,y)
, che è 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
}
Modifica : Sembra che Jung non è davvero un XY-plot, ma un quadro di rete / grafico. Quindi tutto ciò che serve è al layout i punti in un cerchio utilizzando uno dei layout forniti. CircleLayout
e KKLayout
sembrano fare il trucco, anche se CircleLayout
dà strani risultati quando ci sono molti nodi. Ecco il codice di esempio completo:
//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);
Ho scelto SparseMultiGraph
perché è quello che era in JUNG esercitazione . Ci sono altri tipi di grafici, ma non sono sicuro quale sia la differenza.
Si potrebbe anche usare un StaticLayout
che può prendere vertici (x,y)
, quindi utilizzare il mio codice originale per tracciare i punti, ma che non sarebbe altrettanto elegante per quadro JUNG. Dipende da cosa siano le vostre esigenze, però.