曼哈顿 六边形网格中瓷砖之间的距离
对于方形网格,图块 A 和 B 之间的欧几里德距离为:
distance = sqrt(sqr(x1-x2)) + sqr(y1-y2))
对于被限制沿方形网格移动的演员,曼哈顿距离是我们必须行进的实际距离的更好衡量标准:
manhattanDistance = abs(x1-x2) + abs(y1-y2))
如何获取两个图块之间的曼哈顿距离如下图红线和蓝线所示的六边形网格?
For a square grid the euclidean distance between tile A and B is:
distance = sqrt(sqr(x1-x2)) + sqr(y1-y2))
For an actor constrained to move along a square grid, the Manhattan Distance is a better measure of actual distance we must travel:
manhattanDistance = abs(x1-x2) + abs(y1-y2))
How do I get the manhattan distance between two tiles in a hexagonal grid as illustrated with the red and blue lines below?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我曾经在游戏中设置了一个六边形坐标系,使 y 轴与 x 轴成 60 度角。这避免了奇偶行的区别。
(来源:althenia.net)< /sub>
此坐标系中的距离为:
您可以将 (x', y) 从您的坐标系转换为 (x, < em>y) 在这一点中使用:
因此 dx 变为:
使用整数除法实现此操作时请小心舍入。在 C 中,
int y
floor(y/2)
为(y%2 ? y-1 : y)/2
。I once set up a hexagonal coordinate system in a game so that the y-axis was at a 60-degree angle to the x-axis. This avoids the odd-even row distinction.
(source: althenia.net)
The distance in this coordinate system is:
You can convert (x', y) from your coordinate system to (x, y) in this one using:
So
dx
becomes:Careful with rounding when implementing this using integer division. In C for
int y
floor(y/2)
is(y%2 ? y-1 : y)/2
.我假设您想要如图所示识别的两个图块中心之间的平面中的欧几里得距离。我想这可以从图中推导出来。对于任何 x 和 y,从图块 (x, y) 的中心到图块 (x + dx, y) 的中心的向量为 (dx, 0)。从图块 (x, y) 和 (x, y + dy) 中心出发的向量为 (-dy / 2, dy*sqrt(3) / 2)。对于任何 x、y、dx,简单的向量加法给出 (x, y) 和 (x + dx, y + dy) 之间的 (dx - (dy / 2), dy * sqrt(3) / 2) 向量,和迪。总距离就是向量的范数: sqrt((dx - (dy / 2)) ^ 2 + 3 * dy * dy / 4)
I assume that you want the Euclidean distance in the plane between the centers of two tiles that are identified as you showed in the figure. I think this can be derived from the figure. For any x and y, the vector from the center of tile (x, y) to the center of tile (x + dx, y) is (dx, 0). The vector from the center of tile (x, y) and (x, y + dy) is (-dy / 2, dy*sqrt(3) / 2). A simple vector addition gives a vector of (dx - (dy / 2), dy * sqrt(3) / 2) between (x, y) and (x + dx, y + dy) for any x, y, dx, and dy. The total distance is then the norm of the vector: sqrt((dx - (dy / 2)) ^ 2 + 3 * dy * dy / 4)
如果你想要直线距离:
我想说的是,如果
dy
是偶数,它只是一个矩形空间。如果dy
为奇数,则右上角的位置向左或向右1/2个单位。If you want the straight-line distance:
What I'm trying to say is, if
dy
is even, it's just a rectangular space. Ifdy
is odd, the position of the upper right corner is 1/2 unit to the left or to the right.对这个问题的直接回答是不可能的。这个问题的答案与你如何在内存中组织图块有很大关系。我使用 odd-q 垂直布局,并使用以下 matlab 代码始终给我正确的答案。
这是一个 matlab 测试代码
有关此解决方案的数学详细信息,请访问:http://www.redblobgames.com /网格/六边形/ 。您可以在以下位置获取完整的 hextile 库: http://www.redblobgames.com/grids /hexagons/implementation.html
A straight forward answer for this question is not possible. The answer of this question is very much related to how you organize your tiles in the memory. I use odd-q vertical layout and with the following matlab code gives me the right answer always.
Here is a matlab testing code
For mathematical details of this solution visit: http://www.redblobgames.com/grids/hexagons/ . You can get a full hextile library at: http://www.redblobgames.com/grids/hexagons/implementation.html
这听起来像是 Bresenham 线算法 的工作。您可以使用它来计算从 A 到 B 的路段数量,这将告诉您路径距离。
This sounds like a job for the Bresenham line algorithm. You can use that to count the number of segments to get from A to B, and that will tell you the path distance.
如果将不同的六边形定义为图,则可以得到从节点 A 到节点 B 的最短路径。由于到六边形中心的距离是恒定的,因此将其设置为边权重。
不过,这对于大油田来说可能效率低下。
If you define the different hexagons as a graph, you can get the shortest path from node A to node B. Since the distance from the hexagon centers is constant, set that as the edge weight.
This will probably be inefficient for large fields though.