Search code examples
hexagonal-tiles

Generate HoneyComb hexagonal patern in Axial coordinates


I was searching for am algorithm to generate a HoneyComb of hexagons like this one using axial coordinates :

this has a radius of 4 But I only managed to find generators in Cube coordinates.

I made this question only to share my solution .


Solution

  • This generates all r*(r-1)*3+1 tiles of a hexagonal spiraling pattern . A drawback of the method is that it assumes you are placing the shape at (0,0) .

       public void makeHoneyComb(int radius){
    
        makeCell(ta, 0, 0);
        for (int r = 0; r > -radius; r--)
            for (int q = -r - 1; q > -radius - r; q--)
                makeCell( q, r);
    
        for (int r = 1; r < radius; r++)
            for (int q = 0; q > -radius; q--)
                makeCell( q, r);
    
        for (int q = 1; q < radius; q++)
            for (int r = -q; r < radius - q; r++)
                makeCell( q, r);
        }
    

    This is based on the fact that a shape like that cam be split in 3 similar Rectagles and the center piece .