Javascript 如何使用easystarjs尽可能遵循这一方法

Javascript 如何使用easystarjs尽可能遵循这一方法,javascript,path-finding,Javascript,Path Finding,我使用easystarjs并从页面上的示例开始 var grid = [[0,0,1,0,0], [0,0,1,0,0], [0,0,1,0,0], [0,0,1,0,0], [0,0,1,0,0]]; //startX, startY ,endX, endY, callback easystar.findPath(0, 3, 4, 3, function( path ) {

我使用easystarjs并从页面上的示例开始

var grid = [[0,0,1,0,0],
            [0,0,1,0,0],
            [0,0,1,0,0],
            [0,0,1,0,0],
            [0,0,1,0,0]];

//startX, startY ,endX, endY, callback        
easystar.findPath(0, 3, 4, 3, function( path ) {
            path = path || [];
            for(var i = 0, ilen = path.length; i < ilen; i++) {
                //console.log(path[i].x, path[i].y);
                marker.drawRect(path[i].x*32, path[i].y*32, 32, 32);
        }

    });
如果我运行代码,没有办法画出来,因为它是不完整的,在路上有一堵墙。是否可以修改代码,使其不只是说找不到路径或没有绘图,我希望代码尽可能地绘制到墙的路径


如果我将数字1改为数字0并创建一个段落,代码就会工作。

该函数将返回不同的结果,具体取决于您将哪个1变为0,因此尽可能与该1相关

如果路径为空,则按如下方式填充:

currentX = startX;
currentY = startY;
path = [];

while(grid[currentY][currentX] != 1){
  path.push({x: currentX, y: currentY});
  dX = endX - currentX;
  dY = endY - currentY;
  distanceX = Math.abs(dX);
  distanceY = Math.abs(dY);
  directionX = dX / distanceX;
  directionY = dY / distanceY;
  // Make a step in the direction where the distance is bigger
  if(distanceX > distanceY){
    currentX += directionX;
  }else{
    currentY += directionY;
  }
}
这将是一条通往目的地的直线,被墙壁打断