From 40c576280e9dbbad7670d9eae2dd65abbe007310 Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Sun, 5 Sep 2021 22:47:54 +0200 Subject: [PATCH] Use and display MinimumSpanningForestAlgorithm results --- README.md | 3 ++ .../de/oshgnacknak/gruphi/GruphiFrame.java | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/README.md b/README.md index a5d2291..332e577 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/main/java/de/oshgnacknak/gruphi/GruphiFrame.java b/src/main/java/de/oshgnacknak/gruphi/GruphiFrame.java index c3d57b7..32c47a5 100644 --- a/src/main/java/de/oshgnacknak/gruphi/GruphiFrame.java +++ b/src/main/java/de/oshgnacknak/gruphi/GruphiFrame.java @@ -31,6 +31,8 @@ class GruphiFrame extends JFrame { private final DirectedGraph graph; + private DirectedGraph forest; + private final MazeGenerator 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) {