Skip to content

Instantly share code, notes, and snippets.

@dbrockman
Created September 12, 2015 20:35
Show Gist options
  • Select an option

  • Save dbrockman/03bdb8980a41e7f0da86 to your computer and use it in GitHub Desktop.

Select an option

Save dbrockman/03bdb8980a41e7f0da86 to your computer and use it in GitHub Desktop.

Revisions

  1. David Brockman Smoliansky created this gist Sep 12, 2015.
    57 changes: 57 additions & 0 deletions axial-coordinates.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    /**
    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 };
    }