Well, I just found out that my hex distance maths don't work
.
CLOSE! but...
There are all sorts of solutions on the internet (which I surveyed when I wrote mine a zillion years ago), but none are drag and drop specifically with the way are hexes are numbered, etc. And I can't explain what mine is doing (right or wrong) anyway.
So, curious what others are using.
Here's what I have.
It fails for 1903 to 1805. I get 2 instead of 3. (Subsector format which is column and row order, so its column 19 and row 3).
Math.signum returns 0 for zeros, -1 for negative numbers and +1 for positive numbers (BASIC SGN function).
So, just curious what others are using for this.

CLOSE! but...
There are all sorts of solutions on the internet (which I surveyed when I wrote mine a zillion years ago), but none are drag and drop specifically with the way are hexes are numbered, etc. And I can't explain what mine is doing (right or wrong) anyway.
So, curious what others are using.
Here's what I have.
Code:
public int hexDistance(int x1, int y1, int x2, int y2) {
int ax = x1 - floor2(y1);
int ay = x1 + ceil2(y1);
int bx = x2 - floor2(y2);
int by = x2 + ceil2(y2);
int dx = bx - ax;
int dy = by - ay;
if (Math.signum(dx) == Math.signum(dy)) {
return Math.max(Math.abs(dx), Math.abs(dy));
}
return Math.abs(dx) + Math.abs(dy);
}
private int floor2(int x) {
return ((x >= 0) ? (x / 2) : (x - 1) / 2);
}
private int ceil2(int x) {
return ((x >= 0) ? ((x + 1) / 2) : x / 2);
}
It fails for 1903 to 1805. I get 2 instead of 3. (Subsector format which is column and row order, so its column 19 and row 3).
Code:
hexDistance(19, 3, 18, 5)
Math.signum returns 0 for zeros, -1 for negative numbers and +1 for positive numbers (BASIC SGN function).
So, just curious what others are using for this.