Open enpepolicy opened 11 months ago
Thanks for opening the issue. I'll look into it next week at the earliest.
I don't see how the distance between hexes in a grid is affected by the hex offset setting. And I couldn't find anything related to that on the redblobgames.com website. Do you have a link that explains the expected behavior?
Yes of course, look this link (https://www.redblobgames.com/grids/hexagons/)
Distance are different in the Coordinate Systems -> offset coordinates-> (pointed odd r and pointed even r) have different distances from 3,1 to 4,2.
HI flauwekeul , could you check it?
I will, sorry for the silence. I was able to reproduce the problem. My free time is very limited, but I'll try to look into it this year. Thanks for your patience.
got it working with this: ` it('calculates the correct distance between hex coordinates in an odd-r layout', () => { // Tests for horizontal distances expect(oddRHorizontalLayoutDistance([0, 0], [6, 0])).toBe(6) expect(oddRHorizontalLayoutDistance([1, 1], [5, 1])).toBe(4)
// Tests for vertical distances
expect(oddRHorizontalLayoutDistance([0, 0], [0, 6])).toBe(6)
expect(oddRHorizontalLayoutDistance([3, 3], [3, 6])).toBe(3)
// Tests for diagonal distances
expect(oddRHorizontalLayoutDistance([1, 1], [5, 5])).toBe(6)
expect(oddRHorizontalLayoutDistance([2, 2], [5, 6])).toBe(5)
// Test for the same location (distance should be 0)
expect(oddRHorizontalLayoutDistance([4, 4], [4, 4])).toBe(0)
// Tests for complex distances where rowOffset calculation is necessary
expect(oddRHorizontalLayoutDistance([0, 6], [5, 6])).toBe(5)
expect(oddRHorizontalLayoutDistance([1, 6], [5, 5])).toBe(5)
// Others
expect(oddRHorizontalLayoutDistance([0, 0], [2, 1])).toBe(3)
})
type HexPoint = [number, number]
type CubePoint = [number, number, number]
function oddrToCube(hex: [number, number]): CubePoint {
const x = hex[0] - Math.floor((hex[1] - (hex[1] & 1)) / 2)
const z = hex[1]
const y = -x - z
return [x, y, z]
}
function cubeDistance(a: CubePoint, b: CubePoint): number {
return Math.max(
Math.abs(a[0] - b[0]),
Math.abs(a[1] - b[1]),
Math.abs(a[2] - b[2])
)
}
function oddRHorizontalLayoutDistance(
hex1: HexPoint,
hex2: HexPoint
): number {
const cube1 = oddrToCube(hex1)
const cube2 = oddrToCube(hex2)
return cubeDistance(cube1, cube2)
}`
i dunno if this is useful, but I used offset [1,1] and the distances were calculated correctly.
const size = 40; const gridSize = 11; const center = 5;
const Hex = defineHex({ dimensions: { xRadius: size/2, yRadius: size/2 }, offset:[1,1] }) const hexgrid = new Grid(Hex, rectangle({ width: gridSize, height: gridSize }))
Describe the bug Offset is not applying to pointy hex when using grid distance method
To Reproduce const WideHex = defineHex({ dimensions: { xRadius: 50, yRadius: 30 }, // wide hexes offset: -1, // or 1 }) const distanceCalculator = new Grid(WideHex, rectangle({ width: 10, height: 10 }))
console.log(
Starting coordinates:${boardTile.x},${boardTile.y}
,Destination coordinates:${qBoardCoordinate},${rBoardCoordinate}
,Distance:${distanceCalculator.distance( [boardTile.x, boardTile.y], [qBoardCoordinate, rBoardCoordinate] )}
, distanceCalculator.toJSON(), )// Case 1
// Case 2
Expected behavior When changing offset one of them should calculate 1 and the other 2, as distance as seen in redblob blog.
Environment (please complete the following information):
Screenshots Not apply
Additional context Not for the moment