网格:
+---------------+---------------+---------------+---------------+---------------+
| 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;
}
测试
IDFromPos(-2, -2) = 20
IDFromPos(-2, -1) = 21
IDFromPos(-2, 0) = 22
IDFromPos(-2, 1) = 23
IDFromPos(-2, 2) = 24
IDFromPos(-1, -2) = 27 (should be 19)
IDFromPos(-1, -1) = 6
IDFromPos(-1, 0) = 7
IDFromPos(0, 0) = 0
IDFromPos(0, 1) = 1
IDFromPos(0, 2) = 10
IDFromPos(1, 0) = 5 (should be 3)
IDFromPos(1, 1) = 6 (should be 2)
IDFromPos(1, 2) = 11
IDFromPos(2, 0) = 18 (should be 14)
IDFromPos(2, 1) = 19 (should be 13)
IDFromPos(2, 2) = 20 (should be 12)
我一直盯着这个很久。我看不到我的错误。鉴于(x,y)
位置id
是什么?这个功能出了什么问题?
网格位置不正常所以请仔细看看。 - , - 是左上角,+,+是右下角。
答案 0 :(得分:2)
第二个和第三个else if
内的计算是错误的。在第二个中,最后一个减法实际上应该是一个加法。在第三个中,中间减法应该是一个补充。
// ...
} 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);
}
// ...
这会修复所有测试。
答案 1 :(得分:0)
通过对角线将整个平面划分为四个区段,并形成每个区段的结果:
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