We have a hexagonal latice:
_ _ _ / \_/ \_/ \_ \_/ \_/ \_/ \ / \_/ \_/ \_/ \_/ \_/ \_/
What is the best way to represent it with 2-dimensional array or whatever
Simply counting the hexes from the grid next to your starting point to the grid where you want to stop is how you measure the distance. If a hex is 1 mile, then 5 hexes would be 5 miles. But simply moving from one hex to another would trigger the 1 mile distance.
The simplest way to represent a hex grid with a 2D array is to skew your axes: each row of hexes is offset by half a step more than the previous one. It doesn't matter whether each row is offset forward or backward, as long as you're consistent about it; below, each successive row is offset half a hex forward:
(0,0) (0,1) (0,2) (0,3) (0,4) (1,0) (1,1) (1,2) (1,3) (1,4) (2,0) (2,1) (2,2) (2,3) (2,4) (3,0) (3,1) (3,2) (3,3) (3,4)
It is easy to determine the nearest neighbors of any given hex: in the case above, for a given array address (r,s)
, you have:
(r-1, s) (r-1, s+1) (r, s-1) (r, s+1) (r+1, s-1) (r+1, s)
Also, note that drawing location is simple: the center of hex (r,s)
above is at screen location:
x= dx * (s + 0.5*r) y= dy * r
As an alternative, you could offset alternate rows by half a hex absolute. This will give you a more rectangular shape for a given array, but determining drawing location and nearest neighbors would then require two cases, for even and odd rows respectively.
There are other coordinate systems available, but they are less convenient and more obscure...
Since the OP wants more, I'll add a link to my favorite obscure hex indexing system: a "spiral honeycomb mosaic". This uses a base-seven system to index successively larger "super-hexagon" groups of hex locations, as follows (note that it is labeled in base-7, not base-ten):
7 elements: 49 elements: 2 3 22 23 1 0 4 --> 12 13 21 20 24 6 5 11 10 14 26 25 32 33 16 15 02 03 31 30 34 --> [3 base-7 digits -> 343 elements...] 62 63 01 00 04 36 35 61 60 64 06 05 42 43 66 65 52 53 41 40 44 51 50 54 46 45 56 55
The link has some code for dealing with this coordinate system, but I haven't really tried evaluating it....
A 2d array is fine using 2 rows = 1 hex height, and 1 column = 1 hex width.
4,1,5,2,6,3 4,7,5,8,6,9 A,7,B,8,C,9 A,D,B,E,C,F
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With