【发布时间】:2016-10-27 04:56:37
【问题描述】:
谁能给我一个简单的例子,它在 hexagonal 网格(在 JS 中)上实现 A* path-finding algorithm。我已经让它在方形网格上工作,但是我所有让它在六边形网格上工作的尝试都失败了。
这是我的网格的样子:
我使用相同的技术来绘制网格并生成坐标,如topic 所示。
这是网格坐标数据以及开始、结束坐标:
[0, 0] , [0, 1], [0, 2],
[1, 0], [1, 1], [1, 2], [1, 3],
[2, 0], [2, 1], [2, 2], [2, 3], [2, 4],
[3, 0], [3, 1], [3, 2], [3, 3],
[4, 0], [4, 1], [4, 2]
start_point: [0,2]
end_point: [4.0]
将曼哈顿距离计算更新为:
var dx = pos1[0] - pos0[0];
var dy = pos1[1] - pos0[1];
var dist;
if ( Math.sign(dx) == Math.sign(dy) ){
dist = Math.abs (dx + dy);
}else{
dist = Math.max(Math.abs(dx), Math.abs(dy))
}
return dist;
我得到这个结果:
还有我计算最短路径的方式:
if (!Array.prototype.remove) {
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
}
var astar = {
init: function(grid) {
for(var x = 0; x < grid.length; x++) {
for(var y = 0; y < grid[x].length; y++) {
grid[x][y].f = 0;
grid[x][y].g = 0;
grid[x][y].h = 0;
//grid[x][y].content = false;
grid[x][y].visited = false;
grid[x][y].closed = false;
grid[x][y].debug = "";
grid[x][y].parent = null;
console.log([grid[x][y].coords[0],grid[x][y].coords[1]])
}
}
},
search: function(grid, start, end, heuristic) {
this.init(grid);
heuristic = heuristic || this.manhattan;
var openList = [];
//// find the start and end points in the grid ////
start = grid[start.pos[0]][start.pos[1]];
end = grid[end.pos[0]][end.pos[1]];
console.log( start, end )
openList.push(start);
while(openList.length > 0) {
// Grab the lowest f(x) to process next
var lowInd = 0;
for(var i=0; i<openList.length; i++) {
if(openList[i].f < openList[lowInd].f) { lowInd = i; }
}
var currentNode = openList[lowInd];
// End case -- result has been found, return the traced path
if( currentNode == end ) {
var curr = currentNode;
var ret = [];
while(curr.parent) {
ret.push(curr);
curr = curr.parent;
}
return ret.reverse();
}
// Normal case -- move currentNode from open to closed, process each of its neighbors
openList.remove( lowInd );
currentNode.closed = true;
var neighbors = this.neighbors(grid, currentNode);
for(var i=0; i<neighbors.length;i++) {
var neighbor = neighbors[i];
if( neighbor.closed || neighbor.content == 2 ) { // not a valid node to process, skip to next neighbor
continue;
}
// g score is the shortest distance from start to current node, we need to check if
// the path we have arrived at this neighbor is the shortest one we have seen yet
var gScore = currentNode.g + 1; // 1 is the distance from a node to it's neighbor
var gScoreIsBest = false;
if(!neighbor.visited) {
// This the the first time we have arrived at this node, it must be the best
// Also, we need to take the h (heuristic) score since we haven't done so yet
gScoreIsBest = true;
neighbor.h = heuristic(neighbor.coords, end.coords);
neighbor.visited = true;
openList.push(neighbor);
}
else if(gScore < neighbor.g) {
// We have already seen the node, but last time it had a worse g (distance from start)
gScoreIsBest = true;
}
if(gScoreIsBest) {
// Found an optimal (so far) path to this node. Store info on how we got here and just how good it really is. ////
neighbor.parent = currentNode;
neighbor.g = gScore;
neighbor.f = neighbor.g + neighbor.h;
neighbor.debug = "F: " + neighbor.f + "<br />G: " + neighbor.g + "<br />H: " + neighbor.h;
}
}
}
// No result was found -- empty array signifies failure to find path
return [];
},
manhattan: function(pos0, pos1) { //// heuristics : use manhattan distances ////
var dx = pos1[0] - pos0[0];
var dy = pos1[1] - pos0[1];
return Math.abs (dx + dy);
},
neighbors: function(grid, node) {
var ret = [];
var x = node.coords[0];
var y = node.coords[1];
if(grid[x-1] && grid[x-1][y] ) {
ret.push(grid[x-1][y]);
}
if( grid[x+1] && grid[x+1][y] ) {
ret.push(grid[x+1][y]);
}
if( grid[x][y-1] && grid[x][y-1] ) {
ret.push(grid[x][y-1]);
}
if( grid[x][y+1] && grid[x][y+1] ) {
ret.push(grid[x][y+1]);
}
return ret;
}
};
尝试在互联网上四处寻找一些好的示例或文档,但实际上找不到任何有用的东西。
【问题讨论】:
-
需要澄清一下。你有一个你正在工作的启发式方法吗?你有现有的六边形网格吗?
-
stackoverflow.com/questions/20734438/… 这就是我的网格的样子,只是我把整个 Java 代码变成了 JS。如您所见,网格使用的是轴坐标系,这是我用来计算距离的启发式
var dx = Math.abs (pos1.x - pos0.x); var dy = Math.abs (pos1.y - pos0.y); return dx + dy; -
这个问题中描述的备用坐标系可能会有所帮助,问题本身也会有所帮助:stackoverflow.com/questions/5084801/… 因为曼哈顿距离将是一个很好的第一个启发式方法。 *编辑:它是同一个坐标系,只是旋转了。
-
询问示例(以及一般链接)的问题在 SO 上是题外话。赏金不会改变这一点。请发布您的代码,尤其是尝试失败的代码,告诉我们什么不起作用以及您期望什么。对于这样一个具体的问题,我们可以为您提供帮助。
-
我已经更新了我的问题并提供了更多细节,如果还不够清楚,请告诉我。
标签: javascript html canvas path-finding a-star