All files / src/core/grid hex-grid.ts

24.28% Statements 34/140
100% Branches 4/4
18.18% Functions 2/11
24.28% Lines 34/140

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 1641x             1x 1x   1x                 1x               1x                                   1x             1x     4x 4x 4x 4x 3x 3x 3x 3x 3x 3x 4x 1x 1x 1x 1x 1x 1x 1x 4x 4x   1x                         1x               1x                                         1x                                         1x                                             1x  
import { Point, EdgeInfo } from "../../types";
import { pointsClose, removeDuplicateEdges } from "../utils/geometry";
import { Grid } from "./grid";
import { scanCellWindow } from "./grid-scan";
import { findClosestEdgeInWindow, findClosestVertexInWindow } from "./grid-search";
 
// pointy-top, odd-r offset coordinates
export class HexagonGrid implements Grid {
  constructor(private scale: number) {}
 
  private axialToOffset(axial: { q: number; r: number }): {
    col: number;
    row: number;
  } {
    const col = axial.q + (axial.r - (axial.r & 1)) / 2;
    const row = axial.r;
    return { col, row };
  }
 
  pixelToCell(pixel: Point): { col: number; row: number } | null {
    const tempQ =
      ((Math.sqrt(3) / 3) * pixel.x - (1 / 3) * pixel.y) / this.scale;
    const tempR = ((2 / 3) * pixel.y) / this.scale;
    const axial = this.axialRound({ q: tempQ, r: tempR, s: -tempQ - tempR });
    return this.axialToOffset(axial);
  }
 
  private axialRound(frac: { q: number; r: number; s: number }): {
    q: number;
    r: number;
  } {
    let q = Math.round(frac.q);
    let r = Math.round(frac.r);
    const s = Math.round(frac.s);
    const q_diff = Math.abs(q - frac.q);
    const r_diff = Math.abs(r - frac.r);
    const s_diff = Math.abs(s - frac.s);
    if (q_diff > r_diff && q_diff > s_diff) {
      q = -r - s;
    } else if (r_diff > s_diff) {
      r = -q - s;
    }
    return { q, r };
  }
 
  cellToPixel(cell: { col: number; row: number }): Point {
    const hexSpacingX = this.scale * Math.sqrt(3);
    const hexSpacingY = this.scale * 1.5;
    const offsetX = (cell.row % 2) * (hexSpacingX / 2);
    return { x: cell.col * hexSpacingX + offsetX, y: cell.row * hexSpacingY };
  }
 
  getNeighbors(cell: {
    col: number;
    row: number;
  }): { col: number; row: number }[] {
    const isOddRow = cell.row & 1;
    const neighbors: { col: number; row: number }[] = [];
    if (isOddRow) {
      neighbors.push({ col: cell.col + 1, row: cell.row });
      neighbors.push({ col: cell.col - 1, row: cell.row });
      neighbors.push({ col: cell.col, row: cell.row - 1 });
      neighbors.push({ col: cell.col + 1, row: cell.row - 1 });
      neighbors.push({ col: cell.col, row: cell.row + 1 });
      neighbors.push({ col: cell.col + 1, row: cell.row + 1 });
    } else {
      neighbors.push({ col: cell.col + 1, row: cell.row });
      neighbors.push({ col: cell.col - 1, row: cell.row });
      neighbors.push({ col: cell.col - 1, row: cell.row - 1 });
      neighbors.push({ col: cell.col, row: cell.row - 1 });
      neighbors.push({ col: cell.col - 1, row: cell.row + 1 });
      neighbors.push({ col: cell.col, row: cell.row + 1 });
    }
    return neighbors;
  }
 
  getCellPolygon(cell: { col: number; row: number }): Point[] {
    const center = this.cellToPixel(cell);
    const points: Point[] = [];
    for (let i = 0; i < 6; i++) {
      const angle = (Math.PI / 3) * i + Math.PI / 6;
      points.push({
        x: center.x + this.scale * Math.cos(angle),
        y: center.y + this.scale * Math.sin(angle),
      });
    }
    return points;
  }
 
  getCellEdges(cell: { col: number; row: number }): EdgeInfo[] {
    const poly = this.getCellPolygon(cell);
    return poly.map((p, i) => ({
      type: "edge",
      points: [p, poly[(i + 1) % poly.length]],
    }));
  }
 
  getEdgeAt(
    pixel: Point,
    threshold: number,
    gridWidth: number,
    gridHeight: number
  ): EdgeInfo | null {
    const approxRow = Math.floor(pixel.y / (this.scale * 1.5));
    const approxCol = Math.floor(pixel.x / (this.scale * Math.sqrt(3)));
    return findClosestEdgeInWindow(
      pixel,
      threshold,
      gridWidth,
      gridHeight,
      approxCol,
      approxRow,
      2,
      2,
      (cell) => this.getCellEdges(cell)
    );
  }
 
  getVertexAt(
    pixel: Point,
    threshold: number,
    gridWidth: number,
    gridHeight: number
  ): Point | null {
    const approxRow = Math.floor(pixel.y / (this.scale * 1.5));
    const approxCol = Math.floor(pixel.x / (this.scale * Math.sqrt(3)));
    return findClosestVertexInWindow(
      pixel,
      threshold,
      gridWidth,
      gridHeight,
      approxCol,
      approxRow,
      2,
      2,
      (cell) => this.getCellPolygon(cell)
    );
  }
 
  getEdgesAtVertex(
    vertex: Point,
    gridWidth: number,
    gridHeight: number
  ): EdgeInfo[] {
    const edges: EdgeInfo[] = [];
    const epsilon = 0.1;
    // For simplicity, search around the vertex
    const approxRow = Math.floor(vertex.y / (this.scale * 1.5));
    const approxCol = Math.floor(vertex.x / (this.scale * Math.sqrt(3)));
    scanCellWindow(gridWidth, gridHeight, approxCol, approxRow, 2, 2, (c, r) => {
      const cellEdges = this.getCellEdges({ col: c, row: r });
      for (const edge of cellEdges) {
        if (
          pointsClose(vertex, edge.points[0], epsilon) ||
          pointsClose(vertex, edge.points[1], epsilon)
        ) {
          edges.push(edge);
        }
      }
    });
    return removeDuplicateEdges(edges);
  }
}