Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

edge-math.ts

138 lines3.4 KB

1import {
2 GRID_HEIGHT,
3 GRID_WIDTH,
4 HORIZ_EDGE_COUNT,
5 TOTAL_BOX_COUNT,
6 TOTAL_EDGE_COUNT,
7 VERT_EDGE_COUNT,
8} from "./game-config.js"
9​
10// Re-export grid dimensions with short aliases for use in this module
11export const W = GRID_WIDTH
12export const H = GRID_HEIGHT
13​
14// Re-export edge/box counts
15export const HORIZ_COUNT = HORIZ_EDGE_COUNT
16export const VERT_COUNT = VERT_EDGE_COUNT
17export const EDGE_COUNT = TOTAL_EDGE_COUNT
18export const BOX_COUNT = TOTAL_BOX_COUNT
19​
20// Types
21export interface EdgeCoords {
22 x: number
23 y: number
24 horizontal: boolean
26​
27export interface BoxCoords {
28 x: number
29 y: number
31​
32/**
33 * Convert an edge ID to its coordinates and orientation.
34 *
35 * Horizontal edges: edgeId = y*W + x (y from 0 to H, x from 0 to W-1)
36 * Vertical edges: edgeId = HORIZ_COUNT + y*(W+1) + x (y from 0 to H-1, x from 0 to W)
37 */
38export function edgeIdToCoords(edgeId: number): EdgeCoords {
39 if (edgeId < HORIZ_COUNT) {
40 // Horizontal edge
41 const y = Math.floor(edgeId / W)
42 const x = edgeId % W
43 return { x, y, horizontal: true }
44 } else {
45 // Vertical edge
46 const idx = edgeId - HORIZ_COUNT
47 const y = Math.floor(idx / (W + 1))
48 const x = idx % (W + 1)
49 return { x, y, horizontal: false }
50 }
52​
53/**
54 * Convert coordinates and orientation to an edge ID.
55 */
56export function coordsToEdgeId(x: number, y: number, horizontal: boolean): number {
57 if (horizontal) {
58 return y * W + x
59 } else {
60 return HORIZ_COUNT + y * (W + 1) + x
61 }
63​
64/**
65 * Check if an edge ID represents a horizontal edge.
66 */
67export function isHorizontal(edgeId: number): boolean {
68 return edgeId < HORIZ_COUNT
70​
71/**
72 * Get the boxes adjacent to an edge.
73 *
74 * - Horizontal edge at (x, y) borders boxes at (x, y-1) and (x, y)
75 * - Vertical edge at (x, y) borders boxes at (x-1, y) and (x, y)
76 *
77 * Returns 0-2 boxes depending on whether the edge is on a boundary.
78 */
79export function getAdjacentBoxes(edgeId: number): Array<BoxCoords> {
80 const boxes: Array<BoxCoords> = []
81 const coords = edgeIdToCoords(edgeId)
82​
83 if (coords.horizontal) {
84 // Horizontal edge at (x, y) borders boxes at (x, y-1) and (x, y)
85 if (coords.y > 0) {
86 boxes.push({ x: coords.x, y: coords.y - 1 })
87 }
88 if (coords.y < H) {
89 boxes.push({ x: coords.x, y: coords.y })
90 }
91 } else {
92 // Vertical edge at (x, y) borders boxes at (x-1, y) and (x, y)
93 if (coords.x > 0) {
94 boxes.push({ x: coords.x - 1, y: coords.y })
95 }
96 if (coords.x < W) {
97 boxes.push({ x: coords.x, y: coords.y })
98 }
99 }
100​
101 return boxes
103​
104/**
105 * Get the four edge IDs surrounding a box at (x, y).
106 * Returns [top, bottom, left, right] edge IDs.
107 */
108export function getBoxEdges(x: number, y: number): [number, number, number, number] {
109 const top = y * W + x // h(x, y)
110 const bottom = (y + 1) * W + x // h(x, y+1)
111 const left = HORIZ_COUNT + y * (W + 1) + x // v(x, y)
112 const right = HORIZ_COUNT + y * (W + 1) + (x + 1) // v(x+1, y)
113 return [top, bottom, left, right]
115​
116/**
117 * Convert box coordinates to a box ID.
118 */
119export function boxCoordsToId(x: number, y: number): number {
120 return y * W + x
122​
123/**
124 * Convert a box ID to box coordinates.
125 */
126export function boxIdToCoords(boxId: number): BoxCoords {
127 return {
128 x: boxId % W,
129 y: Math.floor(boxId / W),
130 }
132​
133/**
134 * Check if an edge ID is valid.
135 */
136export function isValidEdgeId(edgeId: number): boolean {
137 return Number.isInteger(edgeId) && edgeId >= 0 && edgeId < EDGE_COUNT