爪哇语:Vector2D spiral(int i) {
int index = i + 1;
int s = ceil(sqrt(index)) + ((ceil(sqrt(index)) % 2 + 1) % 2);
int ringIndex = 0;
int p = 1;
if (s > 1) {
ringIndex = i - (s - 2) * (s - 2);
p = s * s - (s - 2) * (s - 2);
}
int ri = (ringIndex + (int) (s / 2)) % p;
int x = 0;
if (s > 1)
x = ri < (p / 4) ? ri :
(ri <= (p / 4 * 2 - 1) ? (p / 4) :
(ri <= (p / 4 * 3) ? ((p / 4 * 3) - ri) :
0));
int y = 0;
if (s > 1)
y = ri < (p / 4) ? 0 :
(ri <= (p / 4 * 2 - 1) ? (ri - (p / 4)) :
(ri <= (p / 4 * 3) ? (p / 4) :
(p - ri)));
x -= (int) (s / 2);
y -= (int) (s / 2);
return new Vector2D(x, y);
}
Python:
^{pr2}$