4

enter image description here

I have a hexagonal grid like the one in the picture and Im trying to find the easiest way(a formula maybe) to calculate the distance between two hexagons inside this grid. Of course the size of my grid is bigger than this but Im trying to find a formula similar to the Euclidian Distance formula when we calculate the distance between two nodes in a regular grid (with horizontal and vertical axes).

I read for some ways but they all say that Y axis should be 60 degree and then they offer some formulas (Manhattan Distance between tiles in a hexagonal grid). Is there a way to calculate distances using the "Coordinate System" same as in the picture I have uploaded?

4

1 回答 1

5

欧几里得距离

您可以使用应用于计算位置的正常公式来计算欧几里得距离。

假设我们从位置 a0,b0 和 a1,b1 开始。

x 位置由 b*w 给出,其中 w 是一个常数,取决于六边形的大小。

y 位置由 (a+b/2)*h 给出。所以完整的公式是:

x0 = b0*w
x1 = b1*w
y0 = (a0+b0/2)*h
y1 = (a1+b1/2)*h 
dist = sqrt( (x1-x0)^2 + (y1-y0)^2 )

h 是六边形的高度

w 是六边形列之间的水平距离

w 也可以作为 h 的函数计算为:

w=sqrt(3)*h/2

六边形距离

假设您可以从一个六边形移动到一个相邻的六边形。

您可以通过以下方式计算从一个六边形到另一个六边形的移动次数:

x0 = a0-floor(b0/2)
y0 = b0
x1 = a1-floor(b1/2)
y1 = b1
dx = x1 - x0
dy = y1 - y0
dist = max(abs(dx), abs(dy), abs(dx+dy))
于 2013-11-29T16:56:42.057 回答