开发者

what is the best way to represent hexagonal latice

开发者 https://www.devze.com 2023-04-04 03:39 出处:网络
We have a hexagonal latice: ___ / \\_/ \\_/ \\_ 开发者_如何学C \\_/ \\_/ \\_/ \\ / \\_/ \\_/ \\_/

We have a hexagonal latice:

 _   _   _
/ \_/ \_/ \_ 开发者_如何学C
\_/ \_/ \_/ \ 
/ \_/ \_/ \_/
\_/ \_/ \_/

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


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


Please be noted that, there are four different ways to represent the hexagonal lattice. I spend lots of hour searching for a solution and could not able to match one with another. And then I realize, there are 4 different ways to build and represent a lattice. So, when choosing one, make sure what format are you trying to use. The four formats are given below:

what is the best way to represent hexagonal latice

Picture is taken from: https://www.redblobgames.com/grids/hexagons/#conversions

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号