gpt4 book ai didi

c# - 给定一个网格,从中心开始向外螺旋,还有一个位置 ID 是什么?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:59:19 27 4
gpt4 key购买 nike

网格:

+---------------+---------------+---------------+---------------+---------------+
| 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 是什么?这个函数有什么问题?

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

最佳答案

第二个和第三个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);
}
// ...

这会修复所有测试。

关于c# - 给定一个网格,从中心开始向外螺旋,还有一个位置 ID 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49526584/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com