-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hex,grid): add round() and pointToCube() functions and pointToHe…
…x() method to Grid round() converts float axial coordinates to integer cube coordinates. pointToCube() converts a point to cube coordinates. pointToHex() converts a point (e.g. a pixel) to a hex in a grid.
- Loading branch information
1 parent
2c4f3c5
commit b302c08
Showing
9 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { createHexPrototype } from './createHexPrototype' | ||
import { pointToCube } from './pointToCube' | ||
|
||
describe('pointy hex', () => { | ||
test('converts a point to cube coordinates', () => { | ||
const pointyHexPrototype = createHexPrototype({ | ||
orientation: 'pointy', | ||
dimensions: { xRadius: 50, yRadius: 30 }, | ||
origin: { x: -30, y: -30 }, | ||
}) | ||
|
||
// test points close to each side of all edges of hex with coordinates { q: 3, r: 4 } | ||
expect(pointToCube(pointyHexPrototype, { x: 440, y: 185 })).toMatchObject({ q: 3, r: 3 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 440, y: 190 })).toMatchObject({ q: 3, r: 4 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 485, y: 185 })).toMatchObject({ q: 4, r: 3 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 485, y: 190 })).toMatchObject({ q: 3, r: 4 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 505, y: 210 })).toMatchObject({ q: 3, r: 4 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 510, y: 210 })).toMatchObject({ q: 4, r: 4 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 440, y: 230 })).toMatchObject({ q: 3, r: 4 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 440, y: 235 })).toMatchObject({ q: 2, r: 5 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 485, y: 230 })).toMatchObject({ q: 3, r: 4 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 485, y: 235 })).toMatchObject({ q: 3, r: 5 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 415, y: 210 })).toMatchObject({ q: 2, r: 4 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 420, y: 210 })).toMatchObject({ q: 3, r: 4 }) | ||
}) | ||
}) | ||
|
||
describe('flat hex', () => { | ||
test('converts a point to cube coordinates', () => { | ||
const pointyHexPrototype = createHexPrototype({ | ||
orientation: 'flat', | ||
dimensions: { xRadius: 50, yRadius: 30 }, | ||
origin: { x: -30, y: -30 }, | ||
}) | ||
|
||
// test points close to each side of all edges of hex with coordinates { q: 3, r: 4 } | ||
expect(pointToCube(pointyHexPrototype, { x: 255, y: 285 })).toMatchObject({ q: 3, r: 3 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 255, y: 290 })).toMatchObject({ q: 3, r: 4 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 290, y: 300 })).toMatchObject({ q: 4, r: 3 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 290, y: 305 })).toMatchObject({ q: 3, r: 4 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 290, y: 325 })).toMatchObject({ q: 3, r: 4 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 290, y: 335 })).toMatchObject({ q: 4, r: 4 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 255, y: 340 })).toMatchObject({ q: 3, r: 4 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 255, y: 345 })).toMatchObject({ q: 3, r: 5 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 220, y: 325 })).toMatchObject({ q: 3, r: 4 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 220, y: 335 })).toMatchObject({ q: 2, r: 5 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 220, y: 300 })).toMatchObject({ q: 2, r: 4 }) | ||
expect(pointToCube(pointyHexPrototype, { x: 220, y: 305 })).toMatchObject({ q: 3, r: 4 }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { HexPrototype, Point } from '../types' | ||
import { round } from './round' | ||
|
||
// inspired by https://github.com/gojuno/hexgrid-py | ||
// and simplified by https://www.symbolab.com/solver/simplify-calculator/simplify | ||
export const pointToCube = ( | ||
{ dimensions: { xRadius, yRadius }, origin, isPointy }: Pick<HexPrototype, 'dimensions' | 'origin' | 'isPointy'>, | ||
{ x, y }: Point, | ||
) => { | ||
x += origin.x | ||
y += origin.y | ||
|
||
if (isPointy) { | ||
return round({ q: (Math.sqrt(3) * x) / (3 * xRadius) - y / (3 * yRadius), r: (2 / 3) * (y / yRadius) }) | ||
} | ||
|
||
return round({ q: (2 / 3) * (x / xRadius), r: (Math.sqrt(3) * y) / (3 * yRadius) - x / (3 * xRadius) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { round } from './round' | ||
|
||
test('rounds the passed axial or cube coordinates', () => { | ||
expect(round({ q: 1.5, r: 1.1 })).toEqual({ q: 2, r: 1, s: -3 }) | ||
expect(round({ q: 1.1, r: 1.5 })).toEqual({ q: 1, r: 2, s: -3 }) | ||
expect(round({ q: 1.1, r: 1.1 })).toEqual({ q: 1, r: 1, s: -2 }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { CubeCoordinates, PartialCubeCoordinates } from '../types' | ||
|
||
export const round = ({ q, r, s = -q - r }: PartialCubeCoordinates): CubeCoordinates => { | ||
let roundedQ = Math.round(q) | ||
let roundedR = Math.round(r) | ||
let roundedS = Math.round(s) | ||
const diffQ = Math.abs(q - roundedQ) | ||
const diffR = Math.abs(r - roundedR) | ||
const diffS = Math.abs(s - roundedS) | ||
|
||
if (diffQ > diffR && diffQ > diffS) { | ||
roundedQ = -roundedR - roundedS | ||
} else if (diffR > diffS) { | ||
roundedR = -roundedQ - roundedS | ||
} else { | ||
roundedS = -roundedQ - roundedR | ||
} | ||
|
||
return { q: roundedQ, r: roundedR, s: roundedS } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters