diff options
author | ymherklotz <ymherklotz@gmail.com> | 2016-12-11 15:42:21 +0000 |
---|---|---|
committer | ymherklotz <ymherklotz@gmail.com> | 2016-12-11 15:42:21 +0000 |
commit | 45912bc25564150c985de69c8c35c875b3141361 (patch) | |
tree | b124abe3b5c8847463be2db87e4f96203fa582ce | |
parent | 5e28a82b5e8638392d5527dbce8aa44008280b04 (diff) | |
download | A-star-algorithm-45912bc25564150c985de69c8c35c875b3141361.tar.gz A-star-algorithm-45912bc25564150c985de69c8c35c875b3141361.zip |
adding support for 8 corners
-rwxr-xr-x | bin/main | bin | 418104 -> 418304 bytes | |||
-rw-r--r-- | include/astar.hpp | 4 | ||||
-rw-r--r-- | include/node.hpp | 2 | ||||
-rw-r--r-- | src/astar.cpp | 25 |
4 files changed, 27 insertions, 4 deletions
Binary files differ diff --git a/include/astar.hpp b/include/astar.hpp index 30e9d65..321431c 100644 --- a/include/astar.hpp +++ b/include/astar.hpp @@ -9,7 +9,7 @@ // defines the number of nodes one can go to // we don't need this as we will have the queues -#define NEIGHBOUR_NUM 4 +#define NEIGHBOUR_NUM 8 // TODO add constructors and functions to calculate heuristics etc.. class AStar { @@ -29,6 +29,8 @@ private: int graph_height; int path_length; + int count; + Node start_node, end_node; bool start_algorithm(); diff --git a/include/node.hpp b/include/node.hpp index f126fc4..5e29d81 100644 --- a/include/node.hpp +++ b/include/node.hpp @@ -31,7 +31,7 @@ private: // path length to get to that node. int g_score; // heuristic length to destination. - int h_score; + double h_score; // the x and y coordinates of the node in the grid int x, y; diff --git a/src/astar.cpp b/src/astar.cpp index 9739e72..edbfaa9 100644 --- a/src/astar.cpp +++ b/src/astar.cpp @@ -3,7 +3,7 @@ #include <cmath> #include <iostream> -AStar::AStar() : graph(NULL), graph_width(0), graph_height(0), path_length(1) { +AStar::AStar() : graph(NULL), graph_width(0), graph_height(0), path_length(10), count(0) { } AStar::AStar(int *curr_graph, const unsigned int& width, const unsigned int& height) : graph(curr_graph), graph_width(width), graph_height(height), path_length(1) { @@ -92,15 +92,35 @@ Node AStar::get_neighbour(Node& n_in, const int& neighbour_num) { if(neighbour_num == 0 && n_in.y > 0) { n.x = n_in.x; n.y = n_in.y - 1; + path_length = 10; } else if(neighbour_num == 1 && n_in.x < graph_width - 1) { n.x = n_in.x + 1; n.y = n_in.y; + path_length = 10; } else if(neighbour_num == 2 && n_in.y < graph_height - 1) { n.x = n_in.x; n.y = n_in.y + 1; + path_length = 10; } else if(neighbour_num == 3 && n_in.x > 0) { n.x = n_in.x - 1; n.y = n_in.y; + path_length = 10; + } else if(neighbour_num == 4 && n_in.y > 0 && n_in.x < graph_height - 1) { + n.x = n_in.x + 1; + n.y = n_in.y - 1; + path_length = 14; + } else if(neighbour_num == 5 && n_in.y < graph_height - 1 && n_in.x < graph_width - 1) { + n.x = n_in.x + 1; + n.y = n_in.y + 1; + path_length = 14; + } else if(neighbour_num == 6 && n_in.y < graph_height - 1 && n_in.x > 0) { + n.x = n_in.x - 1; + n.y = n_in.y + 1; + path_length = 14; + } else if(neighbour_num == 7 && n_in.y > 0 && n_in.x > 0) { + n.x = n_in.x - 1; + n.y = n_in.y - 1; + path_length = 14; } calc_heuristic(n); @@ -109,7 +129,8 @@ Node AStar::get_neighbour(Node& n_in, const int& neighbour_num) { } void AStar::calc_heuristic(Node& n) { - n.h_score = path_length * (abs(n.x - end_node.x) + abs(n.y - end_node.y)); + n.h_score = sqrt(pow(10 * (end_node.x - n.x), 2) + pow(10 * (end_node.y - n.y), 2)) * (1 + 0.01 * count); + ++count; } void AStar::calc_f(Node& n) { |