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!
Answer
So upon further inspection your problem actually has nothing to do with coordinate system conversions. This could have been made more clear by not naming your axial coordinates X and Y but rather Q and R. The problem you're actually having is bad loop conditions. The original code sample produces delta q's and r's which you try to convert, in your for loops, to absolute coordinates and you made a mistake. The algorithm should look instead as follows:
for (int dx = -distance; dx <= distance; dx++)
{
for (int dy = Math.Max(-distance, -dx - distance); dy <= Math.Min(distance, -dx + distance); dy++)
{
HexPosition rangePosition = new HexPosition(
this.CellPositionX + dx, this.CellPositionY + dy);
range.Add(rangePosition);
}
}
No comments:
Post a Comment