Print 2-D Array in clockwise expanding spiral from center

前端 未结 4 1882
离开以前
离开以前 2020-12-02 00:18

I have an guaranteed to be a perfect square matrix. I want to start at the center of the matrix in this case it would be matrix[2][2], I know h

4条回答
  •  感动是毒
    2020-12-02 01:19

        int radius = 0;
        int i = centerX;
        int j = centerY;
        Debug.Log("i=" + i + "; j=" + j);
        ++i;
        radius += 2;
        while ((i < dimm) && (i >= 0))
        {
            for (int c = j; j < c + radius; j++)
                Debug.Log("i=" + i + "; j=" + j);
            --j;
            --i;
            for (int c = i; i > c - radius + 1; i--)
                Debug.Log("i=" + i + "; j=" + j);
            if (i < 0)
                break;
            else
                Debug.Log("i=" + i + "; j=" + j);
            --j;
            for (int c = j; j > c - radius; j--)
                Debug.Log("i=" + i + "; j=" + j);
            ++i;
            ++j;
            for (int c = i; i < c + radius; i++)
                Debug.Log("i=" + i + "; j=" + j);
            radius += 2;
        }
    

    This Code will output counterclockwise squared matrix (dimm X dimm) indexes from custom center(CenterX, CenterY); and end up, if it came out of matrix size.

提交回复
热议问题