测试
#include <iostream>
#include "source/AStar.hpp"int main()
{AStar::Generator generator;generator.setWorldSize({25, 25});generator.setHeuristic(AStar::Heuristic::euclidean);generator.setDiagonalMovement(true);generator.addCollision({1, 1}); // 添加障碍物generator.addCollision({1, 2}); // 添加障碍物generator.addCollision({2, 1}); // 添加障碍物generator.addCollision({2, 0}); // 添加障碍物generator.addCollision({0, 2}); // 添加障碍物std::cout << "Generate path ... \n";auto path = generator.findPath({0, 0}, {3, 3});for(auto& coordinate : path) {std::cout << coordinate.x << " " << coordinate.y << "\n";}
}
效果
正常
25 x 25 map, Generate path ...
3 3
2 2
1 1
0 0
{1,1}{1,2}障碍
25 x 25 map, Generate path ...
3 3
2 3
1 2
0 1
0 0
全障碍
25 x 25 map, Generate path ...
0 1
0 0
参考
GitHub - daancode/a-star: A* algorithm C++ implementation.