【发布时间】:2013-10-23 19:29:34
【问题描述】:
这个问题与这些问题相反:
- Find the position nth element of a rectangular tiled spiral?
- Coordinate Algorithm - Rotate around the center
- Looping in a spiral
目前我有这段代码,它可以在正方形螺旋中获取第 n 个元素的坐标:
private int[] getPos(int n) {
int x = 0, z = 0;
if (--n >= 0) {
int v = (int) Math.floor(Math.sqrt(n + .25) - 0.5);
int spiralBaseIndex = v * (v + 1);
int flipFlop = ((v & 1) << 1) - 1;
int offset = flipFlop * ((v + 1) >> 1);
x += offset; z += offset;
int cornerIndex = spiralBaseIndex + (v + 1);
if (n < cornerIndex) {
x -= flipFlop * (n - spiralBaseIndex + 1);
} else {
x -= flipFlop * (v + 1);
z -= flipFlop * (n - cornerIndex + 1);
}
}
return new int[]{x,z};
}
现在我需要一个映射另一种方式的函数,有什么想法吗?
【问题讨论】:
-
从技术上讲,您可以调用 getPos(0)、getPos(1)、...,直到找到您的坐标。我有一种感觉不够好。