寻找最接近的可能坐标

时间:2019-03-22 17:40:30

标签: javascript algorithm collision nearest-neighbor

我有这样的x,y坐标板:

enter image description here

木板最大为100x100。

myPosition 是金色,目的地是绿色,碰撞是红色。 myPosition是对象destinationscollisions是对象数组:

let myPosition={x:0,y:0};
let destinations = [{x: 0, y: 5}, {x: 2, y: 0}, {x: 2, y: 2}];
let collisions = [{x: 1, y: 0},{x: 1, y: 1},{x: 1, y: 2},{x: 1, y: 3},{x: 1, y: 4},{x: 2, y: 1},{x: 2, y: 0},{x: 2, y: 1}]

使用this code (live demo),我可以找到最近的目的地,但它根本不了解碰撞。我无法弄清楚如何编写算法来另外检查冲突并在上述场景中给出输出0,5

还有一个假设,就是我们不能对角移动。

我发现this SO answer似乎为我的问题提供了答案,但我无法使其与我的输入数组配合使用。

1 个答案:

答案 0 :(得分:0)

我使用了pathFinding.js库,发现非常简单:

currentPath = finder.findPath(heroCoords.x, 
                              heroCoords.y, 
                              monstersCoords[i].x, 
                              monstersCoords[i].y,
                              currentGrid);
相关问题