I made it for learning how the astar algorithm works, try to make the fastest, tested and configurable as possible for my needs (future games and works).
# How does it work
It is an [astar algorithm](https://en.wikipedia.org/wiki/A*_search_algorithm), the main idea is to find the shortest path between two points in a grid/map.
This project is a header-only library and easy to use, just copy the `include/astar` folder in your project and include the `astar/astar.hpp` header or via CMake FetchContent_Declare.
Now you can use the `Astar::Astar` class to find the shortest path between two points in a grid.
```cpp
#include <astar/astar.hpp>
#include <iostream>
auto main() -> int {
// Create the template class with optional a type (e.g. uint32_t) and a boolean
// if you want enable debug mode (AStar::AStar<uint32_t,true>)
AStar::AStar pathFinder;
// Define the map size (width, height)
pathFinder.setWorldSize({10, 10});
// Set the heuristic function (manhattan, euclidean, octagonal etc...), it is optional, default is euclidean
// if you want to enable diagonal movement, it is optional, default is false
pathFinder.setDiagonalMovement(true);
// Add a obstacle point (5, 5) and (5, 6)
pathFinder.addObstacle({5, 5});
pathFinder.addObstacle({5, 6});
// Find the path from (0, 0) to (9, 9)
auto path = pathFinder.findPath({0, 0}, {9, 9});
// Print the path
for (auto& p : path) {
std::cout <<p.x<<""<<p.y<<std::endl;
}
return 0;
}
```
### Alternative version (direct access to the map)
You can use the alternative version of the library if you want astar have direct access to the map, this version is faster than the non-direct access version.
```cpp
#include <astar/astar.hpp>
#include <iostream>
auto main() -> int {
// Create the template class with optional a type (e.g. uint32_t) and a boolean
// if you want enable debug mode (AStar::AStar<uint32_t,true>)
AStar::AStarFast pathFinder;
// Set the heuristic function (manhattan, euclidean, octagonal etc...), it is optional, default is euclidean