如何在网格中导航寻找最接近的位置?
我有一个网格,其声明如下:
PlayerStatus enum
{
OCCUPIED,
VACANT
}
PlayerStatus[][] grid_ = new PlayerStatus[200][200];
网格设置为等于除玩家所在位置之外的所有空白网格。
我想要一种方法来告诉我一个玩家是否在另一个玩家的某个网格附近,例如:
boolean inRange(int x, int y, int range)
{
//This method finds if a player is close to another one
}
所以如果我传入 inRange(10, 15, 5)
并在 10, 19
有一个玩家我希望该方法返回 true;其中 10, 21
将返回 false。
是否有任何算法可以进行此类搜索,我可以研究一下?或者有人有什么解决办法吗?我觉得计算对角线之类的会比较困难,我该怎么办?任何帮助表示赞赏。
I have a grid that is declared like:
PlayerStatus enum
{
OCCUPIED,
VACANT
}
PlayerStatus[][] grid_ = new PlayerStatus[200][200];
The grid is set equal to all vacant except where players located at.
I would like to have a method that tells me if a player is in a certain grid proximity of another player, something like:
boolean inRange(int x, int y, int range)
{
//This method finds if a player is close to another one
}
So if I pass into inRange(10, 15, 5)
and at 10, 19
there is a player I would like the method to return true; where as 10, 21
would return false.
Are there any algorithms that do this kind of searching that I could look into? Or does anyone have any solutions? I feel that calculating diagonals and such will be rather hard, what should I do for that? Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数学.hypot()
是实现毕达哥拉斯理论的不错选择定理。这里有一个名为norm()
的示例,在此KineticModel
。Math.hypot()
is a good choice for implementing the Pythagorean theorem. There's an example here namednorm()
, which is used in thisKineticModel
.