I have been hunting for an answer on how to accurately calculate the distance between 2 hexagons on a hexagon grid as a number of hexs or "steps".
I came across this posting and it seems to be the correct solution as I have my hexagons setup exactly how Torben describes, however I am trying to figure out the algorithm he is proposing. Specifically:
mydistance((x1,y1), (x2,y2))
= if x1>x2 then mydistance((x2,y2), (x1,y1))
else if y2>=y1 then x2-x1 + y2-y1
else max(x2-x1, y1-y2)
I am pretty sure he is describing an algorithm however, I am getting lost on what he means by "mydistance((x2,y2), (x1,y1))" etc. I figure he probably meant the formula for a distance between 2 points? I tried that and its not working out :( Furthermore, he says it on top before the = and that's just throwing me off.
Can anyone make an accurate idea what he means?
Thanks!!!
EDIT (IMPORTANT) ---- Here is an image of my hexagon grid pattern to shed some light: Please visit http://www.bart4president.com/test/hexGrid.jpg
Here is a clipping of the original posting below:
I prefer a numbering where both x and y correspond to straight lines of hexes, i.e., letting x increase (with constant y) by going right and y increase (with constant x) by going 60 degrees down from right (assuming (0,0) is top left corner).
This way, if you move in one of the three "natural" directions, you either have constant x, constant y or constant (x+y).
That makes calculation of distances etc. easier, as you don't have to sepcial-case on odd and even rows.
I assume you know the hex coordinates and want to find the distance in number of hexes while moving across edges.
If you had used the alternative numbering I described above, the distance is calculated as follows:
mydistance((x1,y1), (x2,y2))
= if x1>x2 then mydistance((x2,y2), (x1,y1))
else if y2>=y1 then x2-x1 + y2-y1
else max(x2-x1, y1-y2)
With the numbering shown on the webpage you sited, you can calculate distance as follows:
yourdistance((x1,y1),(x2,y2))
= mydistance((x1 - y1 `div` 2,y1), (x2 - y2 `div` 2,y2))
I.e., convert to the simpler coordinate system and calculate distance in that. You convert by subtracting half the y coordinate (rounded down) from the x coordinate.
Torben