Drill
Drill

Reputation: 169

Calculate grid distance between hexagons

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?

Upvotes: 5

Views: 5039

Answers (1)

Peter de Rivaz
Peter de Rivaz

Reputation: 33509

Euclidean distance

You can compute the euclidean distance by using the normal formula applied to the computed locations.

Suppose we start with locations a0,b0 and a1,b1.

The x position is given by b*w where w is a constant that depends on the size of the hexagons.

The y position is given by (a+b/2)*h. So the complete formula is:

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 is the height of a hexagon

w is the horizontal distance between columns of hexagons

w can also be computed as a function of h as:

w=sqrt(3)*h/2

Hexagon distance

Suppose you can move from a hexagon to an adjoining hexagon.

You can compute a count of the number of moves to go from one hexagon to another by:

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))

Upvotes: 5

Related Questions