First up - I know I'm being super-dense here.
With that out of the way, I'm trying to write a C# implementation of this algorithm:
var results = []
for each -N ≤ dx ≤ N:
for each max(-N, -dx-N) ≤ dy ≤ min(N, -dx+N):
var dz = -dx-dy
results.append(cube_add(center, Cube(dx, dy, dz)))
I've taken this from this tremendous resource.
My problem is that every implementation of this I've tried so far has had crazy results. For example, the code shown below currently results in this:
and this:
My code currently sits like this:
for (int x = this.CellPositionX - distance; x <= this.CellPositionX + distance; x++)
{
for (int y = this.CellPositionY - Math.Max(-distance, -x - distance); y <= this.CellPositionY + Math.Min(distance, -x + distance); y++)
{
HexPosition rangePosition = new HexPosition(x, y);
range.Add(rangePosition);
}
}
Can anyone spot something awry here? All suggestions welcome. I've been banging my head on this one a while now.
Thanks!
Updated note: I am using Axial coordinates in the grid. Update #2: as pointed out below, I had my for..each loop wrong and wasn't using deltas for the working out. Thanks for the help!
I currently have an issue as shown below with the implementation from the answers:
I'm going to keep investigating - if I figure it out I'll post the full results back here. Thanks all!