# Hex Coordinate Math - Deriving the center of a "Superhex"

This topic is 1643 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Alright, so I'm building a game that uses hexagons for its maps. For my dark and mysterious reasons the world also needs to be split up into "Supertiles" (or Superhexes as I call them in the thread title) composed of nineteen hexes as shown in the picture below.

Assuming the origin tile of (0,0) is the center of a supertile, I'm trying to figure out a formula to calculate whether or not any other particular tile is the center of a supertile. (If no such exists, I can fall back on recursive algorithms to determine it after awhile using "jump towards origin by supertile-centers and see if you can hit 0,0", but I'd love to be able to calculate it directly).

I have the following tables for determining the centers of any NEIGHBORING supertile based on the coordinates for the center of the CURRENT supertile:

(N1 labeled in picture, go around clockwise for N2-6)

If the X-Coordinate is even:

• N1: (+3, +3)
• N2: (-2, +4)
• N3: (-5, 0)
• N4: (-3, -4)
• N5: (+2, -4)
• N6: (+5, -1)

If The X-Coordinate is odd:

• N1: (+3,+4)
• N2: (-2, +4)
• N3: (-5, +1)
• N4: (-3, -3)
• N5: (+2, -4)
• N6: (+5, 0)

[attachment=16836:hex_arrangement.png]

I have not yet found a solution to the problem by internet searches or using my own meager skills with math. If anyone can point me to one or figure it out, I will be eternally thankful. Furthermore if any part of the info above is incorrect or unclear please let me know - my brain hurts a bit, so it wouldn't surprise me.

I'm going to go sit in a corner and look at some pictures of kittens to soothe my brain.

##### Share on other sites

Your best bet for hex grids is to represent them as a rectangular grid but displace every other row by half a tile.

Like so:

That will probably make things easier for you to think about...

(You can still draw the hexes how you like, just represent the map like that, it will make the maths easier).

##### Share on other sites

Yes! Actually, that is in fact how they are arranged - as a simple 2D array. The graphic and all coordinates are calculated using that. Apologies for not mentioning that in the original post.

##### Share on other sites

I find the arithmetic much easier to handle if you use coordinates where moving down is always adding (0,1) and moving down and to the right is always adding (1,0) (or any other such convention). Those are still fairly standard coordinates in an affine plane, which means a lot of familiar things still work.

So start by transforming from your coordinates to the ones I describe (it should be as easy as taking (x,y) and mapping it to (x, y-floor(x/2)), or something like that).

Now the list of neighboring offsets is (3,2), (-2,5), (-5,3), (-3,-2), (2,-5), (5,-3)

The grid is now much more understandable, and the centers of the hexagons are simply of the form A*(3,2) + B*(-2,5), for some integers A and B.

From here there are several ways to finish the job. For instance, taking the matrix with columns (3,2) and (-2,5) and inverting it, I get this rule: A point (x,y) is the center of a hexagon if and only if 5x+2y is a multiple of 19 and 2x-3y is a multiple of 19.

##### Share on other sites

My approach was to look at the 6 directions in which the neighboring hexagons can be reached as 3 axes of a space. Hence one gets co-ordinates to the 6 surrounding super-hexagons as (0,3,2), (3,2,0), (2,0,-3) and their negatives, of course.

However, this leads to a similar system as in Avaro's solution above but with 3 equations, and hence seems not be worth to be investigated further. Probably this is because Alvaro's co-ordinates and mine can be transformed into each other.

##### Share on other sites

I find the arithmetic much easier to handle if you use coordinates where moving down is always adding (0,1) and moving down and to the right is always adding (1,0) (or any other such convention). Those are still fairly standard coordinates in an affine plane, which means a lot of familiar things still work.

I'm not certain I understand this. Can this be done while still using a 2D array of values to store the hexes (as a heightmap) in memory?

##### Share on other sites

I'm not certain I understand this. Can this be done while still using a 2D array of values to store the hexes (as a heightmap) in memory?

Yes, you only needed to read one more line in my post:

So start by transforming from your coordinates to the ones I describe (it should be as easy as taking (x,y) and mapping it to (x, y-floor(x/2)), or something like that).

##### Share on other sites

Try this:

bool is_center(int x, int y) {
y -= (x - (x&1)) / 2;
return (5*x + 2*y) % 19 == 0 && (2*x - 3*y) % 19 == 0;
}