在一个六角形格子砖之间的距离曼哈顿曼哈顿、格子、距离

2023-09-11 00:30:44 作者:非主流四字设计:你很欠吻

有关方格砖A和B之间的欧氏距离为:

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?

推荐答案

我曾经成立了一个六边形坐标系中的一场比赛,这样的的是的轴是在一个60度角 X 轴。这避免了奇数偶数行区别。

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.

这个坐标系中的距离是:

The distance in this coordinate system is:

dx = x1 - x0
dy = y1 - y0

if sign(dx) == sign(dy)
    abs(dx + dy)
else
    max(abs(dx), abs(dy))

您可以转换( X 的',的是的),从你的坐标系( X 的是的)这一次使用:

You can convert (x', y) from your coordinate system to (x, y) in this one using:

x = x' - floor(y/2)

所以 DX 变为:

dx = x1' - x0' - floor(y1/2) + floor(y0/2)

仔细实现此使用整数除法时,四舍五入。在C语言中的 INT是 楼(Y / 2)(Y%2?ÿ -1:Y)/ 2