我确信有一种干净的方法可以做到这一点,但我可能没有使用正确的关键字来找到它。
假设我有一个网格。从网格上的一个位置开始,返回落在给定距离内的所有网格坐标。所以我称之为:
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/