]> git.leonardobizzoni.com Git - astar-visualizer/commitdiff
Finished A*
authorLeonardoBizzoni <leo2002714@gmail.com>
Wed, 30 Jun 2021 09:57:49 +0000 (11:57 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Wed, 30 Jun 2021 09:57:49 +0000 (11:57 +0200)
src/main/java/Main/AStar.java
src/main/java/Main/Map.java
src/main/java/Main/PathfinderUtils.java
target/PathVisualizer-1.0-SNAPSHOT.jar
target/classes/Main/AStar.class
target/classes/Main/Map.class
target/classes/Main/PathfinderUtils.class
target/surefire-reports/Main.AppTest.txt
target/surefire-reports/TEST-Main.AppTest.xml

index 086bc044eaac89a3392e6e529811ba21179b3db4..62aa12861a86a9c7cffc32850bfc7887b26683d6 100644 (file)
@@ -5,6 +5,7 @@ public class AStar{
 
     public AStar(Map map) {
         this.map = map;
+        PathfinderUtils.path.removeAll(PathfinderUtils.path);
     }
 
     public void start() {
@@ -36,6 +37,9 @@ public class AStar{
         if (nextX == PathfinderUtils.startNode.getX() && nextY == PathfinderUtils.startNode.getY())
             return;
         if (nextX == PathfinderUtils.endNode.getX() && nextY == PathfinderUtils.endNode.getY()) {
+            PathfinderUtils.endNode.setParentNode(parent);
+
+            PathfinderUtils.drawPath();
             map.isFinished = true;
             ControlPanel.toggleRunBtn.setText("Clear");
             map.repaint();
index cc962450c636b46df990313cb5374069cdf08e7d..e63983d6cf87626e68f12002a91af345cce0ba8f 100644 (file)
@@ -91,16 +91,24 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi
         menu.renderMenu();
 
         // Draws open nodes
-        g.setColor(Color.white);
+        g.setColor(new Color(69, 133, 136));
         for (Node node : PathfinderUtils.openNodes) {
             g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1);
         }
 
         // Draws closed nodes
-        g.setColor(new Color(132, 255, 138));
+        g.setColor(new Color(77, 77, 77));
         for (Node node : PathfinderUtils.closedNodes) {
             g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1);
         }
+
+        // if path is found start drawing the shortest path
+        if (isFinished) {
+            g.setColor(new Color(250, 189, 47));
+            for (Node node : PathfinderUtils.path) {
+                g.fillRect(node.getX() + 1, node.getY() + 1, size - 1, size - 1);
+            }
+        }
     }
 
     // Drawing on the grid
@@ -113,7 +121,7 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi
 
                 // Checks if start node and end node are the same
                 if (PathfinderUtils.startNode != null && PathfinderUtils.endNode != null) {
-                    if (PathfinderUtils.checkDuplicateNode(PathfinderUtils.startNode, PathfinderUtils.endNode)) {
+                    if (PathfinderUtils.isSameNode(PathfinderUtils.startNode, PathfinderUtils.endNode)) {
                         JOptionPane.showMessageDialog(null, "End node and start node can't be the same node",
                                 "Same node error", JOptionPane.ERROR_MESSAGE);
                         return;
@@ -136,7 +144,7 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi
 
                 // Checks if end node and start node are the same
                 if (PathfinderUtils.startNode != null && PathfinderUtils.endNode != null) {
-                    if (PathfinderUtils.checkDuplicateNode(PathfinderUtils.startNode, PathfinderUtils.endNode)) {
+                    if (PathfinderUtils.isSameNode(PathfinderUtils.startNode, PathfinderUtils.endNode)) {
                         JOptionPane.showMessageDialog(null, "End node and start node can't be the same node!",
                                 "SAME NODE ERROR", JOptionPane.ERROR_MESSAGE);
                         return;
@@ -159,10 +167,10 @@ class Map extends JPanel implements ActionListener, MouseListener, MouseMotionLi
                 Node barrierNode = new Node(posX, posY);
 
                 if(PathfinderUtils.startNode != null)
-                    if(PathfinderUtils.checkDuplicateNode(barrierNode, PathfinderUtils.startNode))
+                    if(PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.startNode))
                         return;
                 if(PathfinderUtils.endNode != null)
-                    if(PathfinderUtils.checkDuplicateNode(barrierNode, PathfinderUtils.endNode))
+                    if(PathfinderUtils.isSameNode(barrierNode, PathfinderUtils.endNode))
                         return;
 
                 PathfinderUtils.barriers.add(barrierNode);
index 2b2744ad4b580b4922cd5ae87585f98610fe39e7..20371d416b345a5ab016f5ee66e97c17b8c5799e 100644 (file)
@@ -7,6 +7,7 @@ public class PathfinderUtils {
     static List<Node> barriers = new ArrayList<>();
     static List<Node> openNodes = new ArrayList<>();
     static List<Node> closedNodes = new ArrayList<>();
+    static List<Node> path = new ArrayList<>();
     static Node startNode, endNode;
 
     // Returns index of barrier if the coordinates are of a barrier node
@@ -24,8 +25,8 @@ public class PathfinderUtils {
         barriers.remove(id);
     }
 
-    static boolean checkDuplicateNode(Node a, Node b) {
-        if(a.getX() == b.getX() && a.getY() == b.getY())
+    static boolean isSameNode(Node a, Node b) {
+        if (a.getX() == b.getX() && a.getY() == b.getY())
             return true;
         return false;
     }
@@ -43,4 +44,23 @@ public class PathfinderUtils {
             }
         }
     }
+
+    static void drawPath() {
+        if (path.size() == 0) {
+            Node node = endNode.getParentNode();
+
+            while (!isSameNode(node, startNode)) {
+                path.add(node);
+
+                for (int i = 0; i < closedNodes.size(); i++) {
+                    Node current = closedNodes.get(i);
+
+                    if (isSameNode(current, node)) {
+                        node = current.getParentNode();
+                        break;
+                    }
+                }
+            }
+        }
+    }
 }
index 674d0e13e92634711300b474eeb3d686799d6789..c080b58abe2c60b61b835439ba8217d266f24b4a 100644 (file)
Binary files a/target/PathVisualizer-1.0-SNAPSHOT.jar and b/target/PathVisualizer-1.0-SNAPSHOT.jar differ
index 8676543f083443aff7a11be4c46d6808aca453a1..01b3d6cc68535b2c47ca86c5e0694b0a40414633 100644 (file)
Binary files a/target/classes/Main/AStar.class and b/target/classes/Main/AStar.class differ
index fc2bb22d2a4d39132b909bf67233f496852a688a..d729a4180b1e23700c5a13756a20d73e8e822497 100644 (file)
Binary files a/target/classes/Main/Map.class and b/target/classes/Main/Map.class differ
index ccf6ade5e1be0f3d697031f89fd493bf70913077..a6ec0004bc6c2aa82a17eeb2102208067abaf160 100644 (file)
Binary files a/target/classes/Main/PathfinderUtils.class and b/target/classes/Main/PathfinderUtils.class differ
index 1952558253f3d459ea67f584c7a832ef6d9f15fb..c1e1e010e1a605c9dc2dee9fbe4f0ad6a93e2a36 100644 (file)
@@ -1,4 +1,4 @@
 -------------------------------------------------------------------------------
 Test set: Main.AppTest
 -------------------------------------------------------------------------------
-Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.029 s - in Main.AppTest
+Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.019 s - in Main.AppTest
index 518b40fe7b232fe59c0af778c5fff8a5090aa131..7752e56328f2b040bdf0878132bf73794f0ae79f 100644 (file)
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="Main.AppTest" time="0.029" tests="1" errors="0" skipped="0" failures="0">
+<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="Main.AppTest" time="0.019" tests="1" errors="0" skipped="0" failures="0">
   <properties>
     <property name="awt.toolkit" value="sun.awt.X11.XToolkit"/>
     <property name="java.specification.version" value="11"/>
@@ -16,7 +16,7 @@
     <property name="sun.java.launcher" value="SUN_STANDARD"/>
     <property name="user.country" value="GB"/>
     <property name="sun.boot.library.path" value="/opt/openjdk-bin-11.0.11_p9/lib"/>
-    <property name="sun.java.command" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter8510599142121748610.jar /home/leo/Docs/Proj/PathVisualizer/target/surefire 2021-06-27T09-11-09_330-jvmRun1 surefire6120348368433176072tmp surefire_01333341478733871355tmp"/>
+    <property name="sun.java.command" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter3942171315997095630.jar /home/leo/Docs/Proj/PathVisualizer/target/surefire 2021-06-30T11-56-47_958-jvmRun1 surefire2991361844254018063tmp surefire_012996660621204474861tmp"/>
     <property name="jdk.debug" value="release"/>
     <property name="surefire.test.class.path" value="/home/leo/Docs/Proj/PathVisualizer/target/test-classes:/home/leo/Docs/Proj/PathVisualizer/target/classes:/home/leo/.m2/repository/junit/junit/4.11/junit-4.11.jar:/home/leo/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:"/>
     <property name="sun.cpu.endian" value="little"/>
@@ -32,7 +32,7 @@
     <property name="java.specification.name" value="Java Platform API Specification"/>
     <property name="java.vm.specification.vendor" value="Oracle Corporation"/>
     <property name="java.awt.graphicsenv" value="sun.awt.X11GraphicsEnvironment"/>
-    <property name="surefire.real.class.path" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter8510599142121748610.jar"/>
+    <property name="surefire.real.class.path" value="/home/leo/Docs/Proj/PathVisualizer/target/surefire/surefirebooter3942171315997095630.jar"/>
     <property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
     <property name="java.runtime.version" value="11.0.11+9"/>
     <property name="user.name" value="leo"/>