Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the best way to represent hexagonal latice

Tags:

We have a hexagonal latice:

 _   _   _ / \_/ \_/ \_  \_/ \_/ \_/ \  / \_/ \_/ \_/ \_/ \_/ \_/ 

What is the best way to represent it with 2-dimensional array or whatever

like image 497
Eugeny89 Avatar asked Sep 12 '11 19:09

Eugeny89


People also ask

How do you measure hex grid?

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.


2 Answers

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

like image 181
comingstorm Avatar answered Oct 01 '22 17:10

comingstorm


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 
  • Every pair of numbers is 1 hex tile.
  • Finding 12 and 6 o'clock is just a +-Y until your on a different tile
  • Finding 2 and 4 o'clock
    • Check if Y+1 is the same tile
      • If it is, 2 o'clock is X+1 & 4 o'clock is X+1,Y+1
      • If it is not, 2 o'clock is X+1,Y-1 & 4 o'clock is X+1
like image 35
Louis Ricci Avatar answered Oct 01 '22 18:10

Louis Ricci