Search code examples
mathlogichexagonal-tiles

Calculate the coordinate of the small center hexagon of a group of hexagon in a grid


I am stuck on a problem that seems easy to solve but I can't seem to pinpoint the right formula.

I have a list of hexagon groups in a cube coordinate system. I know the cube coordinates of the groups but I need to calculate the "global" coordinate of a small hexagon in a given group.

For example, in the image below, I know the coordinates for GroupA (x=0, y=0, z=0) and GroupB (x=-1, y=1, z=0). How can I calculate the coordinates of the center tile of GroupB given that each group has the same radius (in this case the radius is 1) and they don't overlap each other (let's see it as a tiling of groups starting from 0,0,0 that creates a hex grid)?

In this simple example, I know as a human being that the center tile of GroupB is (x=-1, y=3, z=-2) but I need to code that logic in a way that a computer can calculate it for any given group on the map. I don't particularly need help on the code itself but the overall logic.

In this article, the author does the opposite (going from small hexagon and trying to find its group): https://observablehq.com/@sanderevers/hexagon-tiling-of-an-hexagonal-grid

Hexagon groups

Any help would be greatly appreciated! Thanks!


Solution

  • It looks like I have found something that seems to work.

    Please feel free to correct me if I'm mistaken.

    Based on the article I linked in my original question, I came up with an algorithm that calculates the small hexagon central coordinates based on its higher group coordinates (in this case, I've used a group with a radius of 10). I took the original algorithm and removed the area division the author did. The code is in javascript. The i, j and k variables are the cube coordinates of the group. The function returns the cube coordinates of the central small hex :

    getGroupCentralTileCoordinates(i, j, k)
    {
        let r = 10;
        let shift = 3 * r + 2;
    
        let xh = shift * i + j;
        let yh = shift * j + k;
        let zh = shift * k + i;
    
        return {
            'x': (1 + xh - yh) / 3, 
            'y': (1 + yh - zh) / 3, 
            'z': (1 + zh - xh) / 3
        };
    }