为什么我的星号算法在 javascript 中不起作用?
why does my a star algorithm not work in javascript?
function algorithm(){
if(startPoint === true && endPoint === true){
//add the heuristic distance to the start position from the final position
startPosition.h = distance([startPosition.x, startPosition.y]);
let openList = []
openList.push(startPosition)
let closedList = []
while (openList.length > 0){
//print(openList)
lowPos = 0;
for(let i = 0; i < openList.length; i++){
if(openList[i].f < openList[lowPos].f){
lowPos = i;
}
}
let currentPosition = openList[lowPos];
//currentPosition.check()
//if the currentPosition is the endPosition, retrace steps and find the path, then return this path
if(currentPosition === endPosition){
let curr = currentPosition;
let ret = [];
while(curr.parent != null){
curr.path()
ret.push(curr);
curr = curr.parent;
}
endPosition.end()
return ret.reverse();
}
openList.splice(lowPos, 1);
closedList.push(currentPosition);
let neighbours = neighbors(currentPosition);
for(let i = 0; i < neighbours.length; i++){
let neighbour = neighbours[i];
if(closedList.includes(neighbour) || neighbour.colour == "black"){
continue;
}
neighbour.check()
let gScore = currentPosition.g + 1;
let gScoreBest = false;
if(openList.includes(neighbour) == false){
gScoreBest = true;
neighbour.h = distance([neighbour.x, neighbour.y]);
openList.push(neighbour);
}
else if(gScore < neighbour.g){
gScoreBest = true;
}
if(gScoreBest == true){
neighbour.parent = currentPosition;
neighbour.g = gScore;
neighbour.f = neighbour.g + neighbour.h;
}
}
}
}
//meaning that either the path is not possible or the final node/initial node
has not yet been placed.
return [];
}
这是我在 p5 中的明星算法,我正在尝试制作一个明星可视化项目,但由于某种原因,突出显示的块比预期的要多得多。
[: https://i.stack.imgur.com/ILlOr.png
实际上它应该是这样的:: https://i.stack.imgur.com/nsF5r.png
第二张图不是我的,是别人的实现:https://qiao.github.io/PathFinding.js/visual/ = link到第二张图
我认为这与行的顺序有关:neighbour.check() 改变了方块的颜色。
这是一个对角线解决方案,您可以看到由于某种原因左上角有紫色,这是我的问题。不应该搜索左上角,但出于某种原因。
如果您需要我的更多代码,请告诉我。
您似乎没有检查对角线。
这不是错误。你做得很好。
我修复了它,令人惊讶的是我的距离公式出了问题,我调用了错误的变量。
这是现在的样子! :)
function algorithm(){
if(startPoint === true && endPoint === true){
//add the heuristic distance to the start position from the final position
startPosition.h = distance([startPosition.x, startPosition.y]);
let openList = []
openList.push(startPosition)
let closedList = []
while (openList.length > 0){
//print(openList)
lowPos = 0;
for(let i = 0; i < openList.length; i++){
if(openList[i].f < openList[lowPos].f){
lowPos = i;
}
}
let currentPosition = openList[lowPos];
//currentPosition.check()
//if the currentPosition is the endPosition, retrace steps and find the path, then return this path
if(currentPosition === endPosition){
let curr = currentPosition;
let ret = [];
while(curr.parent != null){
curr.path()
ret.push(curr);
curr = curr.parent;
}
endPosition.end()
return ret.reverse();
}
openList.splice(lowPos, 1);
closedList.push(currentPosition);
let neighbours = neighbors(currentPosition);
for(let i = 0; i < neighbours.length; i++){
let neighbour = neighbours[i];
if(closedList.includes(neighbour) || neighbour.colour == "black"){
continue;
}
neighbour.check()
let gScore = currentPosition.g + 1;
let gScoreBest = false;
if(openList.includes(neighbour) == false){
gScoreBest = true;
neighbour.h = distance([neighbour.x, neighbour.y]);
openList.push(neighbour);
}
else if(gScore < neighbour.g){
gScoreBest = true;
}
if(gScoreBest == true){
neighbour.parent = currentPosition;
neighbour.g = gScore;
neighbour.f = neighbour.g + neighbour.h;
}
}
}
}
//meaning that either the path is not possible or the final node/initial node
has not yet been placed.
return [];
}
这是我在 p5 中的明星算法,我正在尝试制作一个明星可视化项目,但由于某种原因,突出显示的块比预期的要多得多。
[
第二张图不是我的,是别人的实现:https://qiao.github.io/PathFinding.js/visual/ = link到第二张图
我认为这与行的顺序有关:neighbour.check() 改变了方块的颜色。
这是一个对角线解决方案,您可以看到由于某种原因左上角有紫色,这是我的问题。不应该搜索左上角,但出于某种原因。
如果您需要我的更多代码,请告诉我。
您似乎没有检查对角线。 这不是错误。你做得很好。
我修复了它,令人惊讶的是我的距离公式出了问题,我调用了错误的变量。
这是现在的样子! :)