deltaX = 2 – 1 = 1

deltaY = 2 – 1 = 1

deltaXY = 1 – 1 = 0

Max of the set is 1. What am I doing wrong?

Thanks for taking the time to clear to write a nice explanation, this is certainly helpful!

]]>I finally found the bug in my code :) ]]>

The distance is three, I think there’s a mistake in how you laid out your hex coordinates.

Looking at the first image above, if you imagine the hex shown as 0,0 to instead be 3,2… the hex shown as 1,1 would be 4,3. Then 2,2 is 5,4, and 6,5 is one hex further in that same direction for a total distance of three. Hope that helps.

]]>a.x = 3

a.y = 2

b.x = 6

b.y = 5

The calculated distance between a and b is 3, but if I draw the 2 points on a hex map, the distance is 5. I don’t understand….

]]>:html => { :novalidate => ‘novalidate’ }

]]>```
```String.prototype.chomp = function () {

return this.replace(/(\n|\r)+$/, '');

}

Your post prompted me to include that in my standard library. Thanks.

]]>So the same exact type of hex-grid you’re using, but rotated 90 degrees. I’m trying to figure out if & how that would necessitate altering the algorithm you’ve explained here? Thanks!

]]>The point of this coordinate system is to allow the x and y axes to align with the hexes so they’re each hitting a hex face “flat side on”. You’ll want to make sure the way you’re drawing your axes they don’t hit hexes “point on”.

If the axes-to-hexes alignment is right then the math shouldn’t change and 2,2 to 1,4 should indeed remain a distance of 3.

]]>My code looks like this:

int deltaX = x1 – x2;

int deltaY = y1 – y2;

int deltaDelta = deltaX – deltaY;

int distance = abs(deltaDelta) > abs(deltaX) && abs(deltaDelta) > abs(deltaY) ?

abs(deltaDelta) : abs(deltaX) > abs(deltaY) ? abs(deltaX) : abs(deltaY);

I think that matches your explanation, though I’m not sure if my use of abs() is 100% correct. It almost works correctly, except it appears to be treating the x and y axes identically, when it should not.

For example, when I calculate the distance from 2,2 to 4,1, I get:

deltaX=-2, deltaY=1, deltaDelta=-3, distance=3

Which is correct! However, when I calculate the distance form 2,2 to 1,4, I get:

deltaX=1, deltaY=-2, deltaDelta=3, distance=3

Which is incorrect- In a point-top hex board, that distance should actually be 2. Any idea what I’m doing wrong?

]]>thanks ]]>