export interface AxialCoordinate { q: number; r: number; } export interface PixelCoordinate { x: number; y: number; } export type AxialCoord = AxialCoordinate; export type PixelCoord = PixelCoordinate; export interface HexCell extends AxialCoordinate { id: string; center: PixelCoordinate; path: string; edgeFactor?: number; } const SQRT_3 = Math.sqrt(3); export function axialRound(coord: AxialCoord): AxialCoord { const cube = axialToCube(coord); let rx = Math.round(cube.x); let ry = Math.round(cube.y); let rz = Math.round(cube.z); const xDiff = Math.abs(rx - cube.x); const yDiff = Math.abs(ry - cube.y); const zDiff = Math.abs(rz - cube.z); if (xDiff > yDiff && xDiff > zDiff) { rx = -ry - rz; } else if (yDiff > zDiff) { ry = -rx - rz; } else { rz = -rx - ry; } return cubeToAxial({ x: rx, y: ry, z: rz }); } export function axialToPixel( coord: AxialCoord, radius: number, offset: PixelCoord = { x: 0, y: 0 } ): PixelCoord { return { x: offset.x + radius * SQRT_3 * (coord.q + coord.r / 2), y: offset.y + radius * 1.5 * coord.r, }; } export function hexCorners(center: PixelCoord, radius: number): PixelCoord[] { return Array.from({ length: 6 }, (_, i) => { const angleDeg = 60 * i - 90; const angleRad = (Math.PI / 180) * angleDeg; return { x: center.x + radius * Math.cos(angleRad), y: center.y + radius * Math.sin(angleRad), }; }); } export function hexPath(center: PixelCoord, radius: number): string { const points = hexCorners(center, radius); return points .map((point, index) => { const command = index === 0 ? 'M' : 'L'; return `${command} ${point.x.toFixed(3)} ${point.y.toFixed(3)}`; }) .join(' ') + ' Z'; } export function createHexGrid(options: { radius: number; columns: number; rows: number; originQ?: number; originR?: number; }): HexCell[] { const { radius, columns, rows, originQ = 0, originR = 0, } = options; const cells: HexCell[] = []; for (let r = originR; r < originR + rows; r++) { for (let q = originQ; q < originQ + columns; q++) { const center = axialToPixel({ q, r }, radius); cells.push({ id: `hex-${q}-${r}`, q, r, center, path: hexPath(center, radius), }); } } return cells; } export function pixelToAxial(point: PixelCoord, radius: number): AxialCoord { const r = (2 * point.y) / (3 * radius); const q = point.x / (SQRT_3 * radius) - r / 2; return axialRound({ q, r }); } export function createViewportHexGrid(options: { radius: number; width: number; height: number; paddingX?: number; paddingY?: number; centerX?: number; centerY?: number; }): HexCell[] { const { radius, width, height, paddingX = width * 0.25, paddingY = height * 0.25, centerX = width / 2, centerY = height / 2, } = options; const bounds = { minX: -paddingX, maxX: width + paddingX, minY: -paddingY, maxY: height + paddingY, }; const corners = [ { x: bounds.minX - centerX, y: bounds.minY - centerY }, { x: bounds.maxX - centerX, y: bounds.minY - centerY }, { x: bounds.minX - centerX, y: bounds.maxY - centerY }, { x: bounds.maxX - centerX, y: bounds.maxY - centerY }, ]; const axialCorners = corners.map((corner) => pixelToAxial(corner, radius)); const minQ = Math.floor(Math.min(...axialCorners.map((coord) => coord.q)) - 1); const maxQ = Math.ceil(Math.max(...axialCorners.map((coord) => coord.q)) + 1); const minR = Math.floor(Math.min(...axialCorners.map((coord) => coord.r)) - 1); const maxR = Math.ceil(Math.max(...axialCorners.map((coord) => coord.r)) + 1); const cells: HexCell[] = []; for (let r = minR; r <= maxR; r += 1) { for (let q = minQ; q <= maxQ; q += 1) { const center = axialToPixel({ q, r }, radius, { x: centerX, y: centerY }); if ( center.x < bounds.minX || center.x > bounds.maxX || center.y < bounds.minY || center.y > bounds.maxY ) { continue; } cells.push({ id: `hex-${q}-${r}`, q, r, center, path: hexPath(center, radius), }); } } return cells; } export function axialToCube(coord: AxialCoord) { const x = coord.q; const z = coord.r; const y = -x - z; return { x, y, z }; } export function cubeToAxial(cube: { x: number; y: number; z: number }): AxialCoord { return { q: cube.x, r: cube.z }; } function cubeRound(cube: { x: number; y: number; z: number }) { let rx = Math.round(cube.x); let ry = Math.round(cube.y); let rz = Math.round(cube.z); const xDiff = Math.abs(rx - cube.x); const yDiff = Math.abs(ry - cube.y); const zDiff = Math.abs(rz - cube.z); if (xDiff > yDiff && xDiff > zDiff) { rx = -ry - rz; } else if (yDiff > zDiff) { ry = -rx - rz; } else { rz = -rx - ry; } return { x: rx, y: ry, z: rz }; } function lerp(a: number, b: number, t: number) { return a + (b - a) * t; } export function hexDistance(a: AxialCoord, b: AxialCoord): number { const ac = axialToCube(a); const bc = axialToCube(b); return Math.max( Math.abs(ac.x - bc.x), Math.abs(ac.y - bc.y), Math.abs(ac.z - bc.z) ); } export function hexLine(a: AxialCoord, b: AxialCoord): AxialCoord[] { const ac = axialToCube(a); const bc = axialToCube(b); const distance = hexDistance(a, b); if (distance === 0) return [a]; const results: AxialCoord[] = []; for (let i = 0; i <= distance; i++) { const t = i / distance; const rounded = cubeRound({ x: lerp(ac.x, bc.x, t), y: lerp(ac.y, bc.y, t), z: lerp(ac.z, bc.z, t), }); results.push(cubeToAxial(rounded)); } return results; } export function coordsToSvgPath(coords: AxialCoord[], radius: number): string { return coords .map((coord, index) => { const point = axialToPixel(coord, radius); return `${index === 0 ? 'M' : 'L'} ${point.x.toFixed(3)} ${point.y.toFixed(3)}`; }) .join(' '); } export function getGridBounds(cells: HexCell[], radius: number) { const xs = cells.map((cell) => cell.center.x); const ys = cells.map((cell) => cell.center.y); const minX = Math.min(...xs) - radius; const maxX = Math.max(...xs) + radius; const minY = Math.min(...ys) - radius; const maxY = Math.max(...ys) + radius; return { minX, maxX, minY, maxY, width: maxX - minX, height: maxY - minY, }; }