1
\$\begingroup\$

I'm creating a wrap around hexagonal map that will potentially render infinatly.

Tiling Hexagonal map

With the method I'm using, I have an x-y coordinate I use to find it's equivalent axial coordinate with the equations: Q = (sqrt(3)/3 * x - 1./3 * y) / hexSize; R = (2./3 * y) / hexSize; the problem is, my map is of radius n and it's possible for the Q R coordinate to be far outside that radius. How can I find the equivalent Q-R coordinate within the radius n? I know methods for a single radius outside, but I'm talking the potential of having a radius of 15 but a Q-R something like (9999999,99999999). What would that coordinate be in relation to the center of the 15 radius hex region it'd be in?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Great question. I believe you can use two posts from Sander Evers to solve this, but I've never tried it myself. Large hex grid shows how to figure out which large repeating hexagon you're in. Hexmod coordinates shows how to figure out where you are inside the large hexagon. \$\endgroup\$
    – amitp
    Jun 3, 2020 at 19:50

1 Answer 1

2
\$\begingroup\$

I figured it out. For anyone else in the future I followed the posts by Sander Evers that amitp suggested (thanks both of you), just the small_to_big and center_of functions in combination will give you the relative coordinate. One thing to note is the functions will tile the map with the southern mirror shifted right (and other mirrors following suit). Previously I had been assuming it'd be shifted left so It meant I had to tweak a couple of things in the rest of my code. Here's my implementation in c++:

//returns center coordinate of a large hex region with given radius r at cube coordinate (i,j,k)
        int_triple center_of(int i, int j, int k, int r)
        {
            return int_triple{ 
                                (r + 1) * i - r * k, 
                                (r + 1)* j - r * i,
                                (r + 1)* k - r * j
                            };
        }
    //returns the coordinate of the large hex region with given radius r containing cube coordinate (x,y,z)
        int_triple small_to_big(int x, int y, int z, int r)
        {
            int area = (3 * r * r) + (3 * r) + 1;
            int shift = (3 * r) + 2;

            int xh = divide_floor(y + shift * x, area);
            int yh = divide_floor(z + shift * y, area);
            int zh = divide_floor(x + shift * z, area);

            return int_triple{
                                divide_floor(1 + xh - yh,3),
                                divide_floor(1 + yh - zh,3),
                                divide_floor(1 + zh - xh,3)
                            };

        }
    //returns the realative coordinate to the center of the hex region of size r containint the coordinate
        int_triple rel_coords(int x, int y, int z, int r)
        {
            int_triple smallToBig = small_to_big(x,y,z,r);
            int_triple centerOf = center_of(smallToBig.x,smallToBig.y,smallToBig.z,r);
            return int_triple{
                                x - centerOf.x,
                                y - centerOf.y,
                                z - centerOf.z
                            };
        }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .