Use ShortestPathAlgorithm from Gruphi to interact with path finding

This commit is contained in:
2021-07-11 16:36:15 +02:00
parent 9c9dd9313c
commit 451bf6394d
3 changed files with 81 additions and 1 deletions

View File

@ -0,0 +1,16 @@
package de.oshgnacknak.gruphi;
import h07.algebra.Monoid;
public class DoubleAddition implements Monoid<Double> {
@Override
public Double zero() {
return 0.0;
}
@Override
public Double add(Double a, Double b) {
return a + b;
}
}

View File

@ -1,6 +1,7 @@
package de.oshgnacknak.gruphi; package de.oshgnacknak.gruphi;
import h07.graph.DirectedGraph; import h07.graph.DirectedGraph;
import h07.graph.Path;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
@ -10,6 +11,8 @@ import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
class GruphiFrame extends JFrame { class GruphiFrame extends JFrame {
@ -18,6 +21,7 @@ class GruphiFrame extends JFrame {
private static Image loadNugget() { private static Image loadNugget() {
try { try {
//noinspection ConstantConditions
return ImageIO.read(Gruphi.class.getResourceAsStream("/nugget.png")); return ImageIO.read(Gruphi.class.getResourceAsStream("/nugget.png"));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -36,6 +40,8 @@ class GruphiFrame extends JFrame {
private final Vector cameraVel = new Vector(0, 0); private final Vector cameraVel = new Vector(0, 0);
private Node selected = null; private Node selected = null;
private Node startNode = null;
private Map<Node, Path<Node, Double>> paths = null;
private boolean running = true; private boolean running = true;
private boolean nuggets = false; private boolean nuggets = false;
@ -100,6 +106,9 @@ class GruphiFrame extends JFrame {
case KeyEvent.VK_DELETE: case KeyEvent.VK_DELETE:
case KeyEvent.VK_BACK_SPACE: { case KeyEvent.VK_BACK_SPACE: {
if (selected != null) { if (selected != null) {
if (selected == startNode) {
clearPaths();
}
graph.removeNode(selected); graph.removeNode(selected);
selected = null; selected = null;
} }
@ -128,6 +137,10 @@ class GruphiFrame extends JFrame {
nuggets = !nuggets; nuggets = !nuggets;
} break; } break;
case KeyEvent.VK_P: {
generatePaths();
} break;
default: break; default: break;
} }
} }
@ -173,6 +186,22 @@ class GruphiFrame extends JFrame {
}); });
} }
private void generatePaths() {
clearPaths();
if (selected != null) {
startNode = selected;
startNode.color = Color.GREEN;
selected = null;
var algo = Objects.requireNonNull(
gruphi.getShortestPathsAlgorithm(),
"Did you supply a path finding implementation");
paths = algo.shortestPaths(graph, startNode, new DoubleAddition(), Comparable::compareTo);
}
}
public void onMousePressed(int button, Vector v) { public void onMousePressed(int button, Vector v) {
switch (button) { switch (button) {
case MouseEvent.BUTTON1: { case MouseEvent.BUTTON1: {
@ -204,6 +233,10 @@ class GruphiFrame extends JFrame {
selected = n; selected = n;
selected.radius *= 1.3; selected.radius *= 1.3;
selected.color = Color.RED; selected.color = Color.RED;
if (selected == startNode) {
clearPaths();
}
}); });
} break; } break;
@ -232,10 +265,16 @@ class GruphiFrame extends JFrame {
private void clearGraph() { private void clearGraph() {
selected = null; selected = null;
clearPaths();
for (var node : graph.getAllNodes()) { for (var node : graph.getAllNodes()) {
graph.removeNode(node); graph.removeNode(node);
} }
} }
private void clearPaths() {
startNode = null;
paths = null;
}
private Optional<Node> findClickedNode(Vector v) { private Optional<Node> findClickedNode(Vector v) {
return graph.getAllNodes() return graph.getAllNodes()
@ -254,8 +293,14 @@ class GruphiFrame extends JFrame {
private void drawNodes(Drawable d) { private void drawNodes(Drawable d) {
d.strokeWeight(1); d.strokeWeight(1);
d.fill(Color.WHITE); d.fill(Color.WHITE);
drawConnections(d);
drawConnections(d);
drawCells(d);
drawPath(d);
}
private void drawCells(Drawable d) {
for (var node : graph.getAllNodes()) { for (var node : graph.getAllNodes()) {
d.fill(node.color); d.fill(node.color);
d.ellipse(node.pos.x, node.pos.y, node.radius * 2, node.radius * 2); d.ellipse(node.pos.x, node.pos.y, node.radius * 2, node.radius * 2);
@ -267,6 +312,24 @@ class GruphiFrame extends JFrame {
} }
} }
private void drawPath(Drawable d) {
if (selected == null || paths == null || !paths.containsKey(selected)) {
return;
}
var path = paths.get(selected);
d.fill(Color.GREEN);
d.strokeWeight(2);
Node prev = null;
for (var node : path) {
if (prev != null) {
d.line(prev.pos.x, prev.pos.y, node.pos.x, node.pos.y);
}
prev = node;
}
}
private void drawConnections(Drawable d) { private void drawConnections(Drawable d) {
for (var node : graph.getAllNodes()) { for (var node : graph.getAllNodes()) {
for (var child : graph.getChildrenForNode(node)) { for (var child : graph.getChildrenForNode(node)) {

View File

@ -5,6 +5,7 @@ public class GruphiMain {
public static void main(String[] args) { public static void main(String[] args) {
var gruphi = new GruphiBuilder() var gruphi = new GruphiBuilder()
// .setDirectedGraphFactory(someFactory) // .setDirectedGraphFactory(someFactory)
// .setShortestPathsAlgorithm(somePathFinder)
.createGruphi(); .createGruphi();
var frame = new GruphiFrame(gruphi); var frame = new GruphiFrame(gruphi);