LINE1 10 20 30 40 50 60 70 8012345678910export const COLUMNS = 13;11export const ROWS = 10;12export const ROW_SPACING = Math.sqrt(3) / 2;13export const TILE_RADIUS = 0.55;1415export function makeHex(column, row) {16 if (!Number.isInteger(column) || !Number.isInteger(row)) return null;17 if (column < 0 || column >= COLUMNS || row < 0 || row >= ROWS) return null;18 return { column, row };19}2021export function sameHex(a, b) {22 return Boolean(a && b) && a.column === b.column && a.row === b.row;23}2425export function key(hex) {26 return `${hex.column},${hex.row}`;27}2829export function fromKey(value) {30 const [column, row] = value.split(',').map(Number);31 return { column, row };32}3334export function center(hex) {35 return {36 x: hex.column + (hex.row % 2) * 0.5 - ((COLUMNS - 1) * 0.5 + 0.25),37 z: hex.row * ROW_SPACING - ((ROWS - 1) * 0.5) * ROW_SPACING,38 };39}4041export function allHexes() {42 const result = [];43 for (let row = 0; row < ROWS; row += 1) {44 for (let column = 0; column < COLUMNS; column += 1) {45 result.push({ column, row });46 }47 }48 return result;49}5051const DIRECTIONS = [52 [1, -1, 0],53 [1, 0, -1],54 [0, 1, -1],55 [-1, 1, 0],56 [-1, 0, 1],57 [0, -1, 1],58];5960function toCube(hex) {61 const x = hex.column - (hex.row - (hex.row & 1)) / 2;62 const z = hex.row;63 return { x, y: -x - z, z };64}6566function fromCube(x, y, z) {67 if (z < 0 || z >= ROWS) return null;68 const column = x + (z - (z & 1)) / 2;69 return makeHex(column, z);70}7172export function distance(a, b) {73 const left = toCube(a);74 const right = toCube(b);75 return Math.max(76 Math.abs(left.x - right.x),77 Math.abs(left.y - right.y),78 Math.abs(left.z - right.z),79 );80}8182export function neighbors(hex) {83 const cube = toCube(hex);84 const result = [];85 for (const [dx, dy, dz] of DIRECTIONS) {86 const next = fromCube(cube.x + dx, cube.y + dy, cube.z + dz);87 if (next) result.push(next);88 }89 return result;90}9192function cubeRound(x, y, z) {93 let rx = Math.round(x);94 let ry = Math.round(y);95 let rz = Math.round(z);96 const dx = Math.abs(rx - x);97 const dy = Math.abs(ry - y);98 const dz = Math.abs(rz - z);99 if (dx > dy && dx > dz) rx = -ry - rz;100 else if (dy > dz) ry = -rx - rz;101 else rz = -rx - ry;102 return { x: rx, y: ry, z: rz };103}104105106export function line(from, to) {107 const steps = distance(from, to);108 if (steps === 0) return [{ ...from }];109 const a = toCube(from);110 const b = toCube(to);111 const cells = [];112 for (let index = 0; index <= steps; index += 1) {113 const t = index / steps;114 const rounded = cubeRound(115 a.x + (b.x - a.x) * t,116 a.y + (b.y - a.y) * t,117 a.z + (b.z - a.z) * t,118 );119 const cell = fromCube(rounded.x, rounded.y, rounded.z);120 if (cell) cells.push(cell);121 }122 return cells;123}124125126export function hexFromPoint(x, z, tolerance = 0.58) {127 let best = null;128 let bestDistance = Infinity;129 for (const cell of allHexes()) {130 const point = center(cell);131 const dx = x - point.x;132 const dz = z - point.z;133 const squared = dx * dx + dz * dz;134 if (squared < bestDistance) {135 bestDistance = squared;136 best = cell;137 }138 }139 return bestDistance <= tolerance * tolerance ? best : null;140}