Is @src/routenodes.cpp , change Dijkstra algorithm to A star algorithm --- Content from…
- User
Is @src/routenodes.cpp , change Dijkstra algorithm to A star algorithm --- Content from referenced files --- Content from @C:\routenodes\src\routenodes.cpp: // 计算所求路径的核心代码
#include "routenodes.hpp"
#include #include #include #include
namespace routenodes {
// 拐点惩罚系数 const float BendPenalty = 100.0f; // 路径重叠惩罚系数 const float PathOverlapPenalty = 2.0f; // 路径居中奖励系数 const float PathMiddleReward = 0.9f;
// 生成已有路径周围扩大一圈余量的矩形 void initPathRects(const CompAndCanvasInfo& wsg, std::vector& pathRects) { // 预留的数组大小 size_t cap = 0; for (const auto& path : wsg.paths) { if (path.size() > 1) { cap += path.size() - 1; } }
codepathRects.reserve(cap); for (const auto& path : wsg.paths) { for (size_t i = 0; i < path.size() - 1; ++i) { const auto& point = path[i]; const auto& next = path[i + 1]; pathRects.push_back({ std::min(point.x, next.x) - DefaultPathMargin, std::max(point.x, next.x) + DefaultPathMargin, std::min(point.y, next.y) - DefaultPathMargin, std::max(point.y, next.y) + DefaultPathMargin, }); } }}
using MidLines = std::vector<std::pair<CustomPoint, CustomPoint>>;
void findMidLines(const CompAndCanvasInfo& wsg, MidLines& midLines) { // 求所有极大空矩形 std::vector inRects; inRects.reserve(wsg.compPartInfos.size()); for (const auto& comp : wsg.compPartInfos) { inRects.push_back(Rect(comp)); }
codeconst Rect canvas = {0.0f, wsg.graphicSceneWidth, 0.0f, wsg.graphicSceneHeight}; std::vector<Rect> maxEmptyRects; findMaxEmptyRects(inRects, canvas, maxEmptyRects); midLines.reserve(maxEmptyRects.size() * 2); for (const auto& rect : maxEmptyRects) { // 如果空矩形的左右不与画布边界重合,且左右距离大于两倍余量,则考虑竖直中线 if (!isClose(rect.left, canvas.left) && !isClose(rect.right, canvas.right) && rect.right - rect.left > 2.0f * DefaultMargin) { const float x = (rect.left + rect.right) / 2; midLines.push_back({{x, rect.top}, {x, rect.bottom}}); } // 同理考虑水平中线 if (!isClose(rect.top, canvas.top) && !isClose(rect.bottom, canvas.bottom) && rect.bottom - rect.top > 2.0f * DefaultMargin) { const float y = (rect.top + rect.bottom) / 2; midLines.push_back({{rect.left, y}, {rect.right, y}}); } } Assistant
To switch from Dijkstra's algorithm to the A* algorithm, I will implement a heuristic function that estimates the distance to the destination. This will guide the search, making it more efficient. The chosen heuristic will be the Manhattan distance, which is appropriate for a grid-based map where movement is restricted to horizontal and vertical directions.
First, I'll update the
findShortestPathfunction to implement the A* algorithm. This involves changing the priority queue to order nodes based on the total estimated cost (g+h) and incorporating the heuristic calculation. I will also update the function signature to accept the coordinate vectors required by the heuristic.Assistant
Next, I'll modify the call to
findShortestPathwithin therouteNodesfunction, passing the additional coordinate vectors needed by the new A* implementation.