algorithm - 径向网格搜索算法

标签 algorithm search grid radial

我确信有一种干净的方法可以做到这一点,但我可能没有使用正确的关键字来找到它。

假设我有一个网格。从网格上的一个位置开始,返回落在给定距离内的所有网格坐标。所以我称之为:

getCoordinates( currentPosition, distance )

对于每个坐标,从初始位置开始,添加所有基本方向,然后添加周围的空格等等,直到达到距离。我想在网格上这看起来像一颗钻石。该函数将返回该坐标数组。有人能告诉我一个可以有效执行此操作的例程吗(我在 AS3 中工作,这是值得的)?

在期望的输出中,迭代 1 将是:

.x.
xxx
.x.

迭代 2 将是:

..x..
.xxx.
xxxxx
.xxx.
..x..

迭代 3:

...x...
..xxx..
.xxxxx.
xxxxxxx
.xxxxx.
..xxx..
...x...

等等……

最佳答案

编辑:更新算法以反射(reflect) OP 的要求。

迭代 1:

.x.
xxx
.x.

迭代 2:

..x..
.xxx.
xxxxx
.xxx.
..x..

... 迭代 4:

....x....
...xxx...
..xxxxx..
.xxxxxxx.
xxxxxxxxx
.xxxxxxx.
..xxxxx..
...xxx...
....x....

显然,您无需迭代即可确定坐标。

若起点为(X,Y),迭代次数为n

for(int i = x - n; i <= x + n; i++)
{
    for(int j = y - n; j <= y + n; j++)
    {
        int dx = abs(i - x);
        int dy = abs(j - y);
        if(dx + dy <= n) //Produces a diamond, n+1 would produce a diamond with cut corners
        {
            //The point at (i, j) is within the marked area.
        }
    }
}

关于algorithm - 径向网格搜索算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2435963/

相关文章:

algorithm - 在不使用额外数组的情况下实现归并排序?

c# - 在列表中搜索匹配对象并赋值

ios - 将搜索栏添加到 UITableView 的顶部

css - 如何在空 div 上强制设置最小宽度

javascript onclick 函数不起作用

CSS相对大小列不等宽问题

algorithm - 查找数组中最大的整数和但不大于 x

algorithm - 表示位置和基于接近度查询的有效方法?

algorithm - 如何解决给定算法的递归?

javascript - 如何在多个文本节点中搜索一个字符串