Skip to content

Instantly share code, notes, and snippets.

@dbrockman
Created September 12, 2015 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbrockman/03bdb8980a41e7f0da86 to your computer and use it in GitHub Desktop.
Save dbrockman/03bdb8980a41e7f0da86 to your computer and use it in GitHub Desktop.
/**
Axial coordinates
# convert cube to axial
q = x
r = z
# convert axial to cube
x = q
z = r
y = -x-z
# convert cube to even-q offset
q = x
r = z + (x + x&1) / 2
# convert even-q offset to cube
x = q
z = r - (q + q&1) / 2
y = -x-z
# convert cube to odd-q offset
q = x
r = z + (x - x&1) / 2
# convert odd-q offset to cube
x = q
z = r - (q - q&1) / 2
y = -x-z
# convert cube to even-r offset
q = x + (z + z&1) / 2
r = z
# convert even-r offset to cube
x = q - (r + r&1) / 2
z = r
y = -x-z
# convert cube to odd-r offset
q = x + (z - z&1) / 2
r = z
# convert odd-r offset to cube
x = q - (r - r&1) / 2
z = r
y = -x-z
**/
export function cubeToAxial(x, z) {
return { q: x, r: z };
}
export function axialToCube(q, r) {
return { x: q z: r y: -q - r };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment