On the main hex page I have a wraparound system that uses hex grids made of hexes. Someone asked me in email about wraparound for rhombus/rectangular maps. Here are all hexes within a certain distance from a given hex in this wraparound world:
Try this:
To calculate the wrapped coordinate range[1] you don't need to calculate distances. Instead, you loop over all nearby hexes, and then wrap them back onto the grid. The wrapping function first looks at how much r changed and then uses that to adjust q. In this particular grid, every time floor(r/8) changes by 1 the q value has to "snap" back by 4.
I also wanted to calculate distances. The simplest thing is to calculate the distance from the original point and each of 8 “mirror” points. Pick the shortest distance to those 9 points. I colored the hexes with the color of the mirror point that was used for the distance. (Note that only 6 of the mirrors, plus the non-mirrored original point, actually get used!)
Although the mirrors are pretty simple to calculate, I wanted to find a direct approach. I tried using interlaced[2] coordinates (also called double width[3]). The conversion is: x = 2*q + r, y = r. Here are the interlaced coordinates:
The wrapped distance can be calculated like this:
int dx = abs(a.x - b.x) % (2*width);
int dy = abs(a.y - b.y) % height;
if (dx > width) { dx = 2*width - dx; }
if (dy > height/2) { dy = height-dy; }
return dy + max(0, (dx-dy)/2);