寻找最接近的坐标

问题描述 投票:0回答:1

我有这样的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似乎提供了我的问题的答案,但我无法使用我的输入数组。

javascript algorithm collision nearest-neighbor
1个回答
0
投票

我使用pathFinding.js库并且发现非常简单:

currentPath = finder.findPath(heroCoords.x, 
                              heroCoords.y, 
                              monstersCoords[i].x, 
                              monstersCoords[i].y,
                              currentGrid);
© www.soinside.com 2019 - 2024. All rights reserved.