观察右上的规律,每一圈的最大值:max = (2*c+1)^2
其他推算。
代码:
public static Point GetPointSpiral(int num)
{
int c = 0;
int _cc = 0;
int q = 0; //圈 总
int square = 0;
do
{
c++;
_cc = (2 * c - 1) * (2 * c - 1);
} while (_cc < num);
int x = -1;
int y = -1;
int w;
int h;
int offSet = num - (2 * (c - 1) - 1) * (2 * (c - 1) - 1); //- 小圈max
int len = (2 * c - 2);//当前边宽-1
if (len == 0) len = 1;
w = offSet / len; //方向维度
h = offSet % len; //方向上, 偏差维度 (mod)
if (_cc == num)
{
w = 3;
h = 2 * c - 2;
}
switch (w)
{
case 0://右边
x = c - 1;
y = c - 1 - (h);
break;
case 1://下
x = c - 1 - (h);
y = -(c - 1);
break;
case 2://左边
x = -(c - 1);
y = -(c - 1) + (h);
break;
case 3://上边
x = -(c - 1) + (h);
y = (c - 1);
break;
}
return new Point(x, y);
//Debug.Log(c+"-->" + x + "," + y + " : " + w + "," + h);
}