From hex-grid
This skill should be used when the user needs to implement hex-based game boards, convert between hex coordinate systems, calculate hex distances, find hex neighbors, implement hex pathfinding, or draw hex maps. Trigger phrases include "hexagonal grids", "hex grid", "hex map", "honeycomb layout", "hex-based game", "hex pathfinding", "hex distance", "hex neighbors", "cube coordinates", "axial coordinates", "offset coordinates", "hex rotation", "hex ring", "hex spiral", "六边形网格", "六边形地图", "蜂窝布局", "蜂窝网格", "六边形坐标", "六边形寻路", "六边形距离", "六边形邻居", "立方坐标", "轴向坐标", "hex战棋", "蜂窝地图算法".
npx claudepluginhub 15195999826/lomomarketplace --plugin hex-gridThis skill uses the workspace's default tool permissions.
This guide covers various ways to make hexagonal grids, the relationships between different approaches, and common formulas and algorithms. Based on concepts from the authoritative [Red Blob Games Hexagonal Grids Guide](https://www.redblobgames.com/grids/hexagons/).
Generates Dungeondraft battle maps with procedural terrain, rectangular rooms, corridors, and polygon layouts from scene descriptions via YAML configs.
Creates pixel art sprites from scratch with Aseprite tools for canvas setup, layer management, and drawing primitives including pixels, lines, rectangles, circles, fills, and palettes.
Creates pixel art sprites, animations, tilesets, and limited palettes emphasizing deliberate pixel placement and game context. Useful for retro, 8-bit/16-bit, and indie 2D game art.
Share bugs, ideas, or general feedback.
This guide covers various ways to make hexagonal grids, the relationships between different approaches, and common formulas and algorithms. Based on concepts from the authoritative Red Blob Games Hexagonal Grids Guide.
Hexagons offer advantages over squares:
| System | Components | Storage | Algorithms | Best For |
|---|---|---|---|---|
| Offset | (col, row) | Easy | Hard | Rectangular storage |
| Cube | (q, r, s) | Redundant | Easy | All algorithms |
| Axial | (q, r) | Easy | Easy | General purpose |
| Doubled | (col, row) | Easy | Medium | Rectangular + algorithms |
Recommendation: Use axial as primary. Convert to cube for algorithms, offset for rectangular storage.
Three axes (q, r, s) with constraint: q + r + s = 0
Key properties:
max(|dq|, |dr|, |ds|)Two axes (q, r), deriving s as -q - r when needed.
Advantages:
Standard (col, row) with alternating rows/columns shifted:
Characteristics:
Alternative to offset that avoids parity issues:
col + row always evencol + row always evenAdvantage: Neighbors are consistent regardless of position.
Flat-top: Pointy-top:
___ /\
/ \ | |
\___/ \/
Choose based on:
Cube directions (6 neighbors):
[(+1,-1,0), (+1,0,-1), (0,+1,-1),
(-1,+1,0), (-1,0,+1), (0,-1,+1)]
To get neighbor: neighbor = hex + direction[i]
Diagonal neighbors (6 diagonals, distance 2):
[(+2,-1,-1), (+1,+1,-2), (-1,+2,-1),
(-2,+1,+1), (-1,-1,+2), (+1,-2,+1)]
Cube: max(|dq|, |dr|, |ds|)
Or equivalently: (|dq| + |dr| + |ds|) / 2
Axial: (|dq| + |dq + dr| + |dr|) / 2
for q in -N to +N:
for r in max(-N, -q-N) to min(+N, -q+N):
s = -q - r
yield center + (q, r, s)
Total hexes in range: 3*N*(N+1) + 1
Ring at distance N: Start at one corner, walk around (6*N hexes for N>0)
Spiral: Concatenate rings from 0 to N
60° clockwise (cube): (q,r,s) → (-r,-s,-q)
60° counter-clockwise (cube): (q,r,s) → (-s,-q,-r)
To rotate around arbitrary center: translate to origin, rotate, translate back.
Swap two coordinates:
(q,r,s) → (q,s,r)(q,r,s) → (s,r,q)(q,r,s) → (r,q,s)Flat-top:
x = size * (3/2 * q)
y = size * (sqrt(3)/2 * q + sqrt(3) * r)
Pointy-top:
x = size * (sqrt(3) * q + sqrt(3)/2 * r)
y = size * (3/2 * r)
Flat-top:
q = (2/3 * x) / size
r = (-1/3 * x + sqrt(3)/3 * y) / size
Pointy-top:
q = (sqrt(3)/3 * x - 1/3 * y) / size
r = (2/3 * y) / size
Then apply cube rounding (see Common Pitfalls).
Use A* with cube/axial distance as heuristic. The heuristic is admissible (never overestimates).
Cast rays to each potential target. Target is visible if no blocking hex lies on the line.
For toroidal (wrapping) maps, use modular arithmetic. Rectangular wraparound with offset coordinates is simpler than hexagonal wraparound.
When converting pixel → hex, simple rounding breaks q+r+s=0. Use this algorithm:
round all three, then reset the one with largest error:
if |q_diff| > |r_diff| and |q_diff| > |s_diff|:
q = -r - s
elif |r_diff| > |s_diff|:
r = -q - s
else:
s = -q - r
Offset neighbors depend on odd/even column/row. Use lookup tables, not formulas.
Lines through hex edges create ambiguity. Add epsilon to one endpoint before interpolation.
Establish conventions early. Convert explicitly at boundaries.
| Task | Approach |
|---|---|
| Store rectangular map | Offset in 2D array |
| Store sparse map | Axial in hash map |
| Calculate distance | Cube formula |
| Find path | A* with cube heuristic |
| Draw line | Cube interpolation + rounding |
| Get neighbors | Cube addition |
| Rotate/reflect | Cube coordinates |
| Pixel ↔ hex | Axial formulas + cube rounding |
For detailed formulas, algorithms, and pseudocode:
references/formulas.md - Complete formula reference for all conversionsreferences/algorithms.md - Detailed pseudocode for pathfinding, FOV, storageFor interactive diagrams and comprehensive explanations, consult: Red Blob Games: Hexagonal Grids
This is the definitive resource for hexagonal grid development, featuring interactive visualizations and code samples in multiple languages.