Use and display MinimumSpanningForestAlgorithm results
This commit is contained in:
@ -51,6 +51,9 @@ Gewichtung für die nächste Kante verkleinern/vergrößern
|
||||
- `O`:
|
||||
Gewichtung für die nächste Kante auf 1 zurücksetzten
|
||||
|
||||
- `F`:
|
||||
Berechne oder verstecke den minimalen Spannbaum (blau)
|
||||
|
||||
## Wenn Knoten ausgewählt (rot)
|
||||
|
||||
- WASD:
|
||||
|
@ -31,6 +31,8 @@ class GruphiFrame extends JFrame {
|
||||
|
||||
private final DirectedGraph<Node, Double> graph;
|
||||
|
||||
private DirectedGraph<Node, Double> forest;
|
||||
|
||||
private final MazeGenerator<Node> mazeGenerator;
|
||||
private final Gruphi gruphi;
|
||||
|
||||
@ -112,6 +114,7 @@ class GruphiFrame extends JFrame {
|
||||
}
|
||||
graph.removeNode(selected);
|
||||
selected = null;
|
||||
forest = null;
|
||||
}
|
||||
} break;
|
||||
|
||||
@ -154,6 +157,10 @@ class GruphiFrame extends JFrame {
|
||||
weight = 1.0;
|
||||
} break;
|
||||
|
||||
case KeyEvent.VK_F: {
|
||||
generateForest();
|
||||
} break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
@ -199,6 +206,19 @@ class GruphiFrame extends JFrame {
|
||||
});
|
||||
}
|
||||
|
||||
private void generateForest() {
|
||||
if (forest != null) {
|
||||
forest = null;
|
||||
return;
|
||||
}
|
||||
|
||||
var algo = Objects.requireNonNull(
|
||||
gruphi.getMinimumSpanningForestAlgorithm(),
|
||||
"Did you supply a spanning forest implementation");
|
||||
|
||||
forest = algo.minimumSpanningForest(graph, Comparable::compareTo, gruphi.getDirectedGraphFactory());
|
||||
}
|
||||
|
||||
private void generatePaths() {
|
||||
clearPaths();
|
||||
|
||||
@ -278,6 +298,7 @@ class GruphiFrame extends JFrame {
|
||||
|
||||
private void clearGraph() {
|
||||
selected = null;
|
||||
forest = null;
|
||||
clearPaths();
|
||||
for (var node : graph.getAllNodes()) {
|
||||
graph.removeNode(node);
|
||||
@ -317,6 +338,21 @@ class GruphiFrame extends JFrame {
|
||||
drawCells(d);
|
||||
|
||||
drawPath(d);
|
||||
drawForest(d);
|
||||
}
|
||||
|
||||
private void drawForest(Drawable d) {
|
||||
if (forest == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
d.strokeWeight(2);
|
||||
d.fill(Color.BLUE);
|
||||
for (var node : forest.getAllNodes()) {
|
||||
for (var child : forest.getChildrenForNode(node)) {
|
||||
d.line(node.pos.x, node.pos.y, child.pos.x, child.pos.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void drawCells(Drawable d) {
|
||||
|
Reference in New Issue
Block a user