Use and display MinimumSpanningForestAlgorithm results

This commit is contained in:
2021-09-05 22:47:54 +02:00
parent 32c23e115c
commit 40c576280e
2 changed files with 39 additions and 0 deletions

View File

@ -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:

View File

@ -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) {