Skip to content
This repository was archived by the owner on May 8, 2023. It is now read-only.
16 changes: 11 additions & 5 deletions src/files/Controller.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package files;

import files.controllers.Input;
import files.interfaces.Geometry;
import files.models.Graph;
import files.models.Shape;
import files.models.graph.Vertex;

import java.util.ArrayList;

Expand All @@ -15,9 +16,9 @@ public class Controller {
Controller(Model model, View view){
this.onlyOneInstance();
this.model = model;

this.view = view;
this.input = new Input(model,view);
this.run();
this.updateView();
}

Expand All @@ -35,13 +36,18 @@ private void onlyOneInstance (){
private void run(){
//Graph graph = model.getGraph();
//view.getUI().getGraphic().setGraph(graph);
//view.getUI().getGraphic().draw();
view.getUI().getGraphic().draw();
}

private void updateView(){
// TODO: Insert Environment
ArrayList<Shape> shapes = model.getEnvironment().getShapes();
//view.setEnvironment();
Graph graph = model.getGraph();
ArrayList<Vertex> resultingPath = model.getPathfinding().getResult();
view.getUI().getGraphic().setEnvironment(shapes);
view.getUI().getGraphic().setGraph(graph);
view.getUI().getGraphic().setResultPath(resultingPath);

run();
}

private void updateModel(){
Expand Down
10 changes: 7 additions & 3 deletions src/files/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public class Model {
public Model(){
this.onlyOneInstance(); // Limit to only 1 instance of the Model class
environment = new Environment();
pathfinding = new Pathfinding();
//graph = new Graph(6);
//graph.graphTest(6);
//graph = new Graph("waypoint",environment);
graph = new Graph("navmesh",environment);
pathfinding = new Pathfinding(graph);
}

private void onlyOneInstance (){
Expand All @@ -42,4 +42,8 @@ public void setGraph(Graph graph) {
public Environment getEnvironment() {
return environment;
}

public Pathfinding getPathfinding() {
return pathfinding;
}
}
2 changes: 1 addition & 1 deletion src/files/Starter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public static void main(String[] args) {
public void start(Stage stage) {
stage.setTitle("Pathfinding Project 2019");
new Controller(new Model(),new View(stage)); // Model-View-Controller Architectural Pattern
stage.show(); //
stage.show();
}
}
26 changes: 26 additions & 0 deletions src/files/controllers/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import files.Model;
import files.View;
import files.models.Environment;
import files.models.Shape;
import files.models.graph.Vertex;
import javafx.scene.Node;
import javafx.scene.input.KeyCode;

Expand All @@ -26,6 +29,29 @@ private void addEventListeners(ArrayList<Node> nodes){

switch(nodeName){
case "button":
node.setOnMouseClicked(event -> {
model.getEnvironment().generateEnvironment();
model.getGraph().recalculate(model.getEnvironment());
model.getPathfinding().run(model.getGraph());
ArrayList<Vertex> resultPath = model.getPathfinding().getResult();

ArrayList<Shape> shape = model.getEnvironment().getShapes();
view.getUI().getGraphic().setEnvironment(shape);
view.getUI().getGraphic().setResultPath(resultPath);
view.getUI().getGraphic().draw();

});
/*
node.setOnKeyPressed(event -> {
System.out.println(event.getCode());
if(event.getCode().equals(KeyCode.ENTER)) {
// do something
System.out.println("You Pressed Enter");
}
});
*/
break;
case "fullscreenButton":
node.setOnMouseClicked(event -> {
System.out.println("fullscreen "+fullScreen);
fullScreen = !fullScreen;
Expand Down
2 changes: 2 additions & 0 deletions src/files/interfaces/Environment.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package files.interfaces;

import files.models.geometry.Point;
import javafx.scene.canvas.GraphicsContext;

public interface Environment {
void draw(GraphicsContext graphicsContext);
boolean isColliding(Point point);
}
7 changes: 0 additions & 7 deletions src/files/interfaces/Geometry.java

This file was deleted.

7 changes: 7 additions & 0 deletions src/files/interfaces/Overlay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package files.interfaces;

import javafx.scene.canvas.GraphicsContext;

public interface Overlay {
void draw(GraphicsContext graphicsContext);
}
96 changes: 54 additions & 42 deletions src/files/models/AStar.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@
import java.util.TreeSet;

public class AStar {

private Graph graph;
int infinity = (int) Double.POSITIVE_INFINITY;

public static void main(String[] args) {
AStar aStar = new AStar();
aStar.startAStar();
}
private final int infinity = (int) Double.POSITIVE_INFINITY;
private ArrayList<Vertex> result;

public void startAStar() {
public AStar(Graph graph) {

graph = this.makeSmallGraphA();
//graph = this.makeSmallGraphA();
/*
// Create graph
//graph = this.makeDijkstraGraph();
Expand Down Expand Up @@ -81,25 +76,27 @@ public Graph makeSmallGraphB() {
}
*/
// A
Vertex startNode = graph.getVertex("A");
Vertex endNode = graph.getVertex("E");
//Vertex startNode = graph.getVertex("A");
//Vertex endNode = graph.getVertex("E");

// B
//Vertex startNode = graph.getVertex("J");
//Vertex endNode = graph.getVertex("F");

ArrayList<Vertex> result = aStar(startNode, endNode);
//ArrayList<Vertex> result = start(graph, startNode, endNode);

for(Vertex v : result) {
System.out.print( v.name + " Dist:" + v.distance + " ");
if (v!=endNode)
System.out.print("-> ");
}
//for(Vertex v : result) {
// System.out.print( v.name + " Dist:" + v.distance + " ");
// if (v!=endNode)
// System.out.print("-> ");
//}


}

public Graph makeSmallGraphA() {
// TODO: take this graph
/*
Graph myGraph= new Graph();
final Vertex A= myGraph.addVertex("A");
final Vertex B= myGraph.addVertex("B");
Expand All @@ -118,12 +115,14 @@ public Graph makeSmallGraphA() {
myGraph.newEdge(E,D, 4, 3);

return myGraph;

*/ return null;
}


public ArrayList<Vertex> aStar(Vertex startNode, Vertex endNode) {
public ArrayList<Vertex> start(Graph graph, Vertex startNode, Vertex endNode) {

TreeSet<Vertex> openTreeSet = new TreeSet<>(Comparator.comparingLong(Vertex::getF));
TreeSet<Vertex> openTreeSet = new TreeSet<>(Comparator.comparingDouble(Vertex::getF));
ArrayList<Vertex> closedList = new ArrayList<>();

for (Vertex vertex : graph.getVertices()) {
Expand All @@ -132,59 +131,66 @@ public ArrayList<Vertex> aStar(Vertex startNode, Vertex endNode) {
}

startNode.setDistance(0);
startNode.setF(startNode.distance + heuristic(0, endNode));
startNode.setF(startNode.distance + heuristic(startNode, endNode)); // TODO: temporary...?
openTreeSet.add(startNode);
Vertex current;

int counter = 0;

loop:
while (openTreeSet.size() != 0) {
current = openTreeSet.first();
openTreeSet.remove(current);

/*
/*
if (current == endNode){
System.out.println("solution found " + current.name + current.predecessor.name);
System.out.println("solution found " + current.name + " " + current.predecessor.name);
break;
}
*/
*/

for (Edge edge : current.edges){

Vertex successor = edge.getToVertex();
int successorG = current.distance + edge.distance;
long successorH = heuristic(current.distance,endNode);
long successorF = successorG + successorH;
double successorG = current.distance + edge.distance;
double successorH = heuristic(successor,endNode);
double successorF = successorG + successorH;


if (successor == endNode){
successor.setPredecessor(current);
successor.setDistance(successorG);
successor.setF(successorF);
System.out.println("solution found " + successor.name + successor.predecessor.name);
System.out.println("solution found ");
break loop;
}

if (openTreeSet.contains(successor) && successorF < successor.f){
successor.setPredecessor(current);
successor.setDistance(successorG);
successor.setF(successorF);
openTreeSet.remove(successor);
openTreeSet.add(successor);
} else if (closedList.contains(successor)){

if (openTreeSet.contains(successor)){
if (successorF < successor.f) {
closedList.remove(successor);
openTreeSet.remove(successor);
successor.setDistance(successorG);
successor.setF(successorF);
successor.setPredecessor(current);
openTreeSet.add(successor);
}
} else {
} else if (closedList.contains(successor)){
/*if (successorF < successor.f) {
closedList.remove(successor);
System.out.println("reinserted on open: " + successor.name);
openTreeSet.add(successor);
}*/
} else if (openTreeSet.first() != successor){
successor.setPredecessor(current);
successor.setDistance(successorG);
successor.setF(successorF);
openTreeSet.add(successor);
}
System.out.println("evaluated: " + edge.getFromVertex().name
+ " going to: " + edge.getToVertex().name
+ " fValue: " + successorF
+ " removed: " +current.name);
+ " fValue: " + successorF);
}
openTreeSet.remove(current);
System.out.println("removed: " + current.name);
closedList.add(current);
}

Expand All @@ -195,12 +201,18 @@ public ArrayList<Vertex> aStar(Vertex startNode, Vertex endNode) {
while ((resultCurrent != startNode) && (resultCurrent.predecessor != null)) {
resultCurrent = resultCurrent.predecessor;
Path.add(0,resultCurrent);

}
this.result = Path;
return Path;
}

public long heuristic(int dist, Vertex endNode) {
//return sqrt(pow(endNode.x - current.x, 2) + pow(endNode.y - current.y, 2));
return 9-dist;
private double heuristic(Vertex start, Vertex end) {
return Math.sqrt(Math.pow(end.getX() - start.getX(), 2) + Math.pow(end.getY() - start.getY(), 2)+1);
//return 9-dist;
}

public ArrayList<Vertex> getResult() {
return result;
}
}
Loading