It uses an iterative approach that breaks the spiral into 7 sublevels per spiral loop, one sub-layer to exit the previous spiral level and 6 sub-levels to track the hexagonal path around the boundary of the previous level:
static void spiralLoop(int startx, int starty, int levels)
{
int level = 1, sublevel = 0, sublevelstep = 0;
int x = startx, y = starty;
while(level <= levels)
{
System.out.println("["+x+","+y+"]");
switch(sublevel)
{
case 0:
x++;
break;
case 1:
x+=(y&1);y++;
break;
case 2:
x-=(~y&1);y++;
break;
case 3:
x--;
break;
case 4:
x-=(~y&1);y--;
break;
case 5:
x+=(y&1);y--;
break;
case 6:
x++;
break;
default:
break;
}
if(sublevel == 0)
sublevel = 1;
if(++sublevelstep >= level)
{
sublevelstep = 0;
if(++sublevel > 6)
{
level++;
sublevel = 0;
}
}
}
}
(1) ( ) ( ). , , reset 0.
(2) (- > 6), , - reset 0.
(3) ( ) .
, , .
x, y x y. , , , .
, x , y . , x , y . . :
row 0: 0 1 2 3 4 5 6 7 8 9
row 1: 0 1 2 3 4 5 6 7 8
row 2: 0 1 2 3 4 5 6 7 8 9
row 3: 0 1 2 3 4 5 6 7 8
row 4: 0 1 2 3 4 5 6 7 8 9
row 5: 0 1 2 3 4 5 6 7 8