C# 给定一个网格,从中心开始螺旋向外,一个位置,ID是多少?

C# 给定一个网格,从中心开始螺旋向外,一个位置,ID是多少?,c#,algorithm,C#,Algorithm,网格: +---------------+---------------+---------------+---------------+---------------+ | id: 20 | id: 19 | id: 18 | id: 17 | id: 16 | | pos: (-2, -2) | pos: (-1, -2) | pos: (0, -2) | pos: (1, -2) | pos: (2, -2) |

网格:

+---------------+---------------+---------------+---------------+---------------+
| id:  20       | id:  19       | id:  18       | id:  17       | id:  16       |
| pos: (-2, -2) | pos: (-1, -2) | pos: (0, -2)  | pos: (1, -2)  | pos: (2, -2)  |
+---------------+---------------+---------------+---------------+---------------+
| id:  21       | id:  6        | id:  5        | id:  4        | id:  15       |
| pos: (-2, -1) | pos: (-1, -1) | pos: (0, -1)  | pos: (1, -1)  | pos: (2, -1)  |
+---------------+---------------+---------------+---------------+---------------+
| id:  22       | id:  7        | id:  0        | id:  3        | id:  14       |
| pos: (-2, 0)  | pos: (-1, 0)  | pos: (0, 0)   | pos: (1, 0)   | pos: (2, 0)   |
+---------------+---------------+---------------+---------------+---------------+
| id:  23       | id:  8        | id:  1        | id:  2        | id:  13       |
| pos: (-2, 1)  | pos: (-1, 1)  | pos: (0, 1)   | pos: (1, 1)   | pos: (2, 1)   |
+---------------+---------------+---------------+---------------+---------------+
| id:  24       | id:  9        | id:  10       | id:  11       | id:  12       |
| pos: (-2, 2)  | pos: (-1, 2)  | pos: (0, 2)   | pos: (1, 2)   | pos: (2, 2)   |
+---------------+---------------+---------------+---------------+---------------+
public static int IDFromPos(int sectionX, int sectionY) {
    int sectionId = 0;
    if (sectionX < 0 && Mathf.Abs (sectionX) >= Mathf.Abs (sectionY)) {
        sectionId = (int)Mathf.Pow (((-2 * sectionX) + 1), 2) - 1 - (-sectionX - sectionY);
    } else if (sectionX > 0 && Mathf.Abs (sectionX) >= Mathf.Abs (sectionY)) {
        sectionId = (int)Mathf.Pow (((2 * sectionX) + 1), 2) - 1 - (4 * sectionX) - (-sectionX - sectionY);
    } else if (sectionY < 0) {
        sectionId = (int)Mathf.Pow (((-2 * sectionY) + 1), 2) - 1 - (2 * sectionY) - (-sectionY + sectionX);
    } else {
        sectionId = (int)Mathf.Pow ((2 * (sectionY - 1) + 1), 2) + (sectionY - 1 + sectionX);
    }

    return sectionId;
}
代码:

+---------------+---------------+---------------+---------------+---------------+
| id:  20       | id:  19       | id:  18       | id:  17       | id:  16       |
| pos: (-2, -2) | pos: (-1, -2) | pos: (0, -2)  | pos: (1, -2)  | pos: (2, -2)  |
+---------------+---------------+---------------+---------------+---------------+
| id:  21       | id:  6        | id:  5        | id:  4        | id:  15       |
| pos: (-2, -1) | pos: (-1, -1) | pos: (0, -1)  | pos: (1, -1)  | pos: (2, -1)  |
+---------------+---------------+---------------+---------------+---------------+
| id:  22       | id:  7        | id:  0        | id:  3        | id:  14       |
| pos: (-2, 0)  | pos: (-1, 0)  | pos: (0, 0)   | pos: (1, 0)   | pos: (2, 0)   |
+---------------+---------------+---------------+---------------+---------------+
| id:  23       | id:  8        | id:  1        | id:  2        | id:  13       |
| pos: (-2, 1)  | pos: (-1, 1)  | pos: (0, 1)   | pos: (1, 1)   | pos: (2, 1)   |
+---------------+---------------+---------------+---------------+---------------+
| id:  24       | id:  9        | id:  10       | id:  11       | id:  12       |
| pos: (-2, 2)  | pos: (-1, 2)  | pos: (0, 2)   | pos: (1, 2)   | pos: (2, 2)   |
+---------------+---------------+---------------+---------------+---------------+
public static int IDFromPos(int sectionX, int sectionY) {
    int sectionId = 0;
    if (sectionX < 0 && Mathf.Abs (sectionX) >= Mathf.Abs (sectionY)) {
        sectionId = (int)Mathf.Pow (((-2 * sectionX) + 1), 2) - 1 - (-sectionX - sectionY);
    } else if (sectionX > 0 && Mathf.Abs (sectionX) >= Mathf.Abs (sectionY)) {
        sectionId = (int)Mathf.Pow (((2 * sectionX) + 1), 2) - 1 - (4 * sectionX) - (-sectionX - sectionY);
    } else if (sectionY < 0) {
        sectionId = (int)Mathf.Pow (((-2 * sectionY) + 1), 2) - 1 - (2 * sectionY) - (-sectionY + sectionX);
    } else {
        sectionId = (int)Mathf.Pow ((2 * (sectionY - 1) + 1), 2) + (sectionY - 1 + sectionX);
    }

    return sectionId;
}
我已经盯着这个看了很久了。我看不到我的错误。给定
(x,y)
位置
id
是什么?这个函数有什么问题


网格位置不正常,请仔细观察。--是左上角,+,+是右下角。

第二个和第三个
中的计算,如果
错误,则为其他。在第二种情况下,最后的减法实际上应该是加法。第三,中间的减法应该是加法

// ...
} else if(sectionX > 0 && Mathf.Abs(sectionX) >= Mathf.Abs(sectionY)) {
    sectionId = (int)Mathf.Pow(((2 * sectionX) + 1), 2) - 1 - (4 * sectionX) + (-sectionX - sectionY);
} else if(sectionY < 0) {
    sectionId = (int)Mathf.Pow(((-2 * sectionY) + 1), 2) - 1 + (2 * sectionY) - (-sectionY + sectionX);
}
// ...
/。。。
}如果(截面X>0&&Mathf.Abs(截面X)>=Mathf.Abs(截面Y)){
sectionId=(int)数学功率((2*sectionX)+1),2)-1-(4*sectionX)+(-sectionX-sectionY);
}否则如果(截面Y<0){
sectionId=(int)数学功率((-2*sectionY)+1),2)-1+(2*sectionY)-(sectionY+sectionX);
}
// ...

这将修复您的所有测试。

通过对角线将整个平面划分为四个部分,并形成每个部分的结果:

x * x >= y * y, x >= 0:
   id = 4 * x * x - x - y
x * x >= y * y, x < 0:
   id = 4 * x * x - 3 * x + y
y * y < x * x, y >= 0:
   id = 4 * y * y - 3 * y + x
y * y < x * x, y < 0:
   id = 4 * y * y - y - x

id(2,1) = 16 - 2 - 1 = 12
id(2,-1) = 16 - 2 + 1 = 15
id(-2,1) = 16 + 6 + 1= 23
id(-1, -2) = 16 + 2 + 1= 19
id(-1, 2) = 16  - 6 - 1= 9
x*x>=y*y,x>=0:
id=4*x*x-x-y
x*x>=y*y,x<0:
id=4*x*x-3*x+y
y*y=0:
id=4*y*y-3*y+x
y*y
我不太明白你的问题是什么。@SeM-我又添加了一些测试&它们应该是什么。我只是想为给定的
x,y
位置获取
id
,映射表是一个选项吗?@richej-否。网格只是一个示例。其大小未设置,可以很大。请查看此帖子: