Grouping Hexagonal Coordinates into Larger Hexagons

591 Views Asked by At

When using a data structure to partition space, a convenient approach is to form a sort of self similar hierarchical grid (I'm not sure of the proper terminology here). For example, a quadtree recursively subdivides each node into four children of equal size.

While trying to accomplish something similar with hexagons, I've hit a bit of a dead end. I happen to be using an axial coordinate system, with the positive portion of the y-axis arranged to be symmetric about the x-axis.

Hexagon Coordinates

I'm using an analogous coordinate system to label the groups, but rotated slightly to match their layout.

Group Coordinates

Everything works as expected with the coordinate system itself; the issue arises when I try to translate between child (hexagon) and parent (group) coordinates. Specifically, I'm having trouble with the following points.

  • Given the coordinate for any arbitrary hexagon, I'd like to determine the coordinate of its group.
  • Given the coordinate for any arbitrary group, I'd like to determine the coordinate of the hexagon at its origin (ie its equivalent of the (0, 0) position in the (0, 0) group).

Some examples to hopefully clarify things (same axes and grouping as images):

  • Hex(0, 2) -> Group(0, 0)
  • Hex(0, 3) -> Group(0, 1)
  • Hex(5, 2) -> Group(1, 0)
  • Hex(4, 6) -> Group(1, 1)
  • Group(1, 0) -> Hex(3, 2)
  • Group(0, -1) -> Hex(2, -3)

So far the math doesn't seem like it will be very simple, but I'm hoping I've missed something.

1

There are 1 best solutions below

6
On BEST ANSWER

As pointed out in the comments, it looks like your coordinate system is a bit wonky (in an axial system, the cells you labelled in the bottom row of the first picture should be (2, -2) and (4, -2) instead of what you have here.

Once you have the right coordinate system, you can do exactly as you want, although it is a bit tricky.

The basic idea is the same as for squares though: you perform some type of division, and throw away the remainder, and get new coordinates. The complication comes in because your shapes are hexagons, and not parallelograms. I cannot remember the details exactly, but you have to calulate something that is similar to a "remainder", and subtract it from the point before doing a division (which is a matrix division). I made a PDF document that explains this (and a lot more, including the proper coordinate system) some time ago:

http://www.gamelogic.co.za/downloads/HexMath2.pdf

(In the comments a few errors are pointed out; I have corrected those now.)

enter image description here