螺旋队列算法

观察右上的规律,每一圈的最大值: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);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值