Switch hex polygon board from radius to side length
- Add HexSideLength property to BoardTemplate (tiles per edge) - Add EF migration 20260406000000_AddBoardTemplateHexSideLength - Replace NPolyInside (Euclidean circle test) with HexLine + flood-fill: * N corners placed at circumradius = (L-1)/(2·sin(π/N)) in hex-Cartesian * Corners snapped to nearest integer axial cell via HexRound * Perimeter built by hex-line lerp between adjacent corners * Interior determined by BFS flood-fill from center - Auto-compute bounding hex radius from side length (max(L*2, ceil(circumR)+2)) - UI: expose Side Length input instead of Radius; canvas header shows L=n tiles/edge - Zone tiles now walk along actual hex-line edges from each corner Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
d28d02428d
commit
1feda0a59c
@@ -108,10 +108,10 @@ else
|
||||
<div class="form-text">Number of corners / player slots. 3 = triangle, 6 = hexagon, 10 = decagon.</div>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label class="form-label fw-semibold">Radius (tiles)</label>
|
||||
<input type="number" class="form-control" min="2" max="15"
|
||||
@bind="_pendingHexRadius" />
|
||||
<div class="form-text">Board radius from center to corner. Diameter = 2R+1.</div>
|
||||
<label class="form-label fw-semibold">Side Length (tiles)</label>
|
||||
<input type="number" class="form-control" min="1" max="12"
|
||||
@bind="_pendingHexSideLength" />
|
||||
<div class="form-text">Number of hex tiles per polygon edge.</div>
|
||||
</div>
|
||||
<button class="btn btn-outline-secondary btn-sm w-100" @onclick="ApplyHexConfig">
|
||||
Apply
|
||||
@@ -266,9 +266,9 @@ else
|
||||
<div class="card-header d-flex justify-content-between align-items-center">
|
||||
@if (_template.GridType == "Hexagon")
|
||||
{
|
||||
int _hpoly = _template.HexPolygonSides > 0 ? _template.HexPolygonSides : _pendingHexPolySides;
|
||||
int _hradius = _template.HexSides > 1 ? _template.HexSides - 1 : _pendingHexRadius;
|
||||
<span>Canvas <span class="text-muted small">@_hpoly-gon, R=@_hradius (@(2*_hradius+1)×@(2*_hradius+1))</span></span>
|
||||
int _hpoly = _template.HexPolygonSides > 0 ? _template.HexPolygonSides : _pendingHexPolySides;
|
||||
int _hside = _template.HexSideLength > 0 ? _template.HexSideLength : _pendingHexSideLength;
|
||||
<span>Canvas <span class="text-muted small">@_hpoly-gon, L=@_hside tiles/edge</span></span>
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -284,7 +284,7 @@ else
|
||||
const double HexR = 16.0;
|
||||
const double HexPad = 12.0;
|
||||
// hexN = radius + 1 (for HexGrid coordinate math)
|
||||
int hexN = _template.HexSides > 0 ? _template.HexSides : (_pendingHexRadius + 1);
|
||||
int hexN = _template.HexSides > 0 ? _template.HexSides : (_pendingHexSideLength * 2 + 1);
|
||||
int hexDim = 2 * hexN - 1;
|
||||
double svgW = HexR * Math.Sqrt(3) * (hexDim - 1) + 2 * HexR + 2 * HexPad;
|
||||
double svgH = HexR * 1.5 * (hexDim - 1) + 2 * HexR + 2 * HexPad;
|
||||
@@ -406,8 +406,8 @@ else
|
||||
private BoardTemplate _template = new();
|
||||
private int _pendingWidth = 8;
|
||||
private int _pendingHeight = 8;
|
||||
private int _pendingHexPolySides = 6; // polygon corner count (3–10)
|
||||
private int _pendingHexRadius = 5; // board radius in tiles (2–15)
|
||||
private int _pendingHexPolySides = 6; // polygon corner count (3–10)
|
||||
private int _pendingHexSideLength = 5; // tiles per polygon edge (1–12)
|
||||
|
||||
// Only non-default tiles stored; key = "x,y"
|
||||
private Dictionary<string, TileDefinition> _tiles = new();
|
||||
@@ -454,8 +454,8 @@ else
|
||||
_template = new BoardTemplate { CreatedAt = DateTimeOffset.UtcNow, UpdatedAt = DateTimeOffset.UtcNow };
|
||||
_pendingWidth = _template.Width;
|
||||
_pendingHeight = _template.Height;
|
||||
_pendingHexPolySides = 6;
|
||||
_pendingHexRadius = 5;
|
||||
_pendingHexPolySides = 6;
|
||||
_pendingHexSideLength = 5;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -469,8 +469,8 @@ else
|
||||
_template = t;
|
||||
_pendingWidth = t.Width;
|
||||
_pendingHeight = t.Height;
|
||||
_pendingHexPolySides = t.HexPolygonSides > 0 ? t.HexPolygonSides : 6;
|
||||
_pendingHexRadius = t.HexSides > 1 ? t.HexSides - 1 : 5;
|
||||
_pendingHexPolySides = t.HexPolygonSides > 0 ? t.HexPolygonSides : 6;
|
||||
_pendingHexSideLength = t.HexSideLength > 0 ? t.HexSideLength : 5;
|
||||
DeserializeTiles(t.TilesJson);
|
||||
DeserializeZones(t.StartingZonesJson);
|
||||
}
|
||||
@@ -540,81 +540,112 @@ else
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate a regular N-gon of hex tiles and N corner starting zones.
|
||||
/// HexSides is stored as radius+1 (for HexGrid coordinate math).
|
||||
/// HexPolygonSides is stored as N (the corner count / polygon shape).
|
||||
/// Generate a regular N-gon of hex tiles using a HexLine perimeter + flood-fill interior.
|
||||
/// HexSideLength = tiles per polygon edge (drives circumradius and bounding pool).
|
||||
/// HexSides = bounding radius + 1 (auto-computed; used for hex coordinate math).
|
||||
/// HexPolygonSides = N (corner count / polygon shape).
|
||||
/// </summary>
|
||||
private void ApplyHexConfig()
|
||||
{
|
||||
int N = Math.Clamp(_pendingHexPolySides, 3, 10);
|
||||
int R = Math.Clamp(_pendingHexRadius, 2, 15);
|
||||
int sides = R + 1; // HexGrid "sides" parameter = radius + 1
|
||||
int dim = 2 * R + 1;
|
||||
int N = Math.Clamp(_pendingHexPolySides, 3, 10);
|
||||
int L = Math.Clamp(_pendingHexSideLength, 1, 12);
|
||||
|
||||
_pendingHexPolySides = N;
|
||||
_pendingHexRadius = R;
|
||||
_template.HexSides = sides;
|
||||
_template.HexPolygonSides = N;
|
||||
_template.Width = dim;
|
||||
_template.Height = dim;
|
||||
// Circumradius (hex-Cartesian units ≡ hex-step units) so each edge ≈ L tiles.
|
||||
// Adjacent corner distance = 2R·sin(π/N) = L−1 hex steps.
|
||||
double circumR = L <= 1 ? 0.0 : (L - 1.0) / (2.0 * Math.Sin(Math.PI / N));
|
||||
|
||||
// Bounding hex-grid radius: must fully contain the polygon with margin.
|
||||
int R = Math.Max(L * 2, (int)Math.Ceiling(circumR) + 2);
|
||||
R = Math.Clamp(R, 2, 30);
|
||||
|
||||
int dim = 2 * R + 1;
|
||||
|
||||
_pendingHexPolySides = N;
|
||||
_pendingHexSideLength = L;
|
||||
_template.HexSides = R + 1;
|
||||
_template.HexPolygonSides = N;
|
||||
_template.HexSideLength = L;
|
||||
_template.Width = dim;
|
||||
_template.Height = dim;
|
||||
|
||||
_tiles.Clear();
|
||||
_zones.Clear();
|
||||
|
||||
// Mark every tile that is inside the hexagonal arrangement but outside the N-gon as absent.
|
||||
// Tiles inside the N-gon are left as default (no entry = void tile = present).
|
||||
// ── Step 1: Compute N corners in axial (q,r) ─────────────────────────
|
||||
var corners = new (int q, int r)[N];
|
||||
for (int k = 0; k < N; k++)
|
||||
{
|
||||
double angle = Math.PI / 2.0 + 2.0 * Math.PI * k / N;
|
||||
// Hex-Cartesian → axial: cy = r·√3/2 → fr = cy·2/√3; cx = q + r·0.5 → fq = cx − fr·0.5
|
||||
double hcx = circumR * Math.Cos(angle);
|
||||
double hcy = circumR * Math.Sin(angle);
|
||||
double fr = hcy * 2.0 / Math.Sqrt(3.0);
|
||||
double fq = hcx - fr * 0.5;
|
||||
corners[k] = HexRound(fq, fr);
|
||||
}
|
||||
|
||||
// ── Step 2: Perimeter via hex-lines between adjacent corners ──────────
|
||||
var perimeter = new HashSet<(int q, int r)>();
|
||||
for (int k = 0; k < N; k++)
|
||||
{
|
||||
var (q0, r0) = corners[k];
|
||||
var (q1, r1) = corners[(k + 1) % N];
|
||||
foreach (var pt in HexLinePath(q0, r0, q1, r1))
|
||||
perimeter.Add(pt);
|
||||
}
|
||||
|
||||
// ── Step 3: Flood-fill interior from center (0,0) ────────────────────
|
||||
var interior = new HashSet<(int q, int r)>(perimeter);
|
||||
if (!interior.Contains((0, 0)))
|
||||
{
|
||||
interior.Add((0, 0));
|
||||
var bfsQueue = new Queue<(int q, int r)>();
|
||||
bfsQueue.Enqueue((0, 0));
|
||||
int[] dq = { 1, 0, -1, -1, 0, 1 };
|
||||
int[] dr = { 0, 1, 1, 0, -1, -1 };
|
||||
while (bfsQueue.Count > 0)
|
||||
{
|
||||
var (q, r) = bfsQueue.Dequeue();
|
||||
for (int d = 0; d < 6; d++)
|
||||
{
|
||||
int nq = q + dq[d], nr = r + dr[d];
|
||||
if (Math.Abs(nq) > R || Math.Abs(nr) > R || Math.Abs(nq + nr) > R) continue;
|
||||
if (!interior.Add((nq, nr))) continue;
|
||||
bfsQueue.Enqueue((nq, nr));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Step 4: Mark tiles outside polygon as absent ──────────────────────
|
||||
for (int x = 0; x < dim; x++)
|
||||
for (int y = 0; y < dim; y++)
|
||||
{
|
||||
int q = x - R, r = y - R;
|
||||
if (Math.Max(Math.Abs(q), Math.Max(Math.Abs(r), Math.Abs(q + r))) > R)
|
||||
continue; // outside hex bounding circle — not rendered
|
||||
|
||||
double cx = q + r * 0.5;
|
||||
double cy = r * Math.Sqrt(3.0) / 2.0;
|
||||
if (!NPolyInside(cx, cy, N, R))
|
||||
{
|
||||
var tile = GetOrCreateTile(x, y);
|
||||
tile.IsAbsent = true;
|
||||
}
|
||||
if (Math.Abs(q) > R || Math.Abs(r) > R || Math.Abs(q + r) > R) continue;
|
||||
if (!interior.Contains((q, r)))
|
||||
GetOrCreateTile(x, y).IsAbsent = true;
|
||||
}
|
||||
|
||||
// N vertex positions (scaled hex Cartesian)
|
||||
var verts = new (double vx, double vy)[N];
|
||||
for (int k = 0; k < N; k++)
|
||||
{
|
||||
double angle = Math.PI / 2.0 + 2.0 * Math.PI * k / N;
|
||||
verts[k] = (R * Math.Cos(angle), R * Math.Sin(angle));
|
||||
}
|
||||
|
||||
// For each player count 2..N, pick evenly-spaced corners.
|
||||
int half = Math.Max(1, R / 4);
|
||||
// ── Step 5: Build starting zones (corner + tiles along adjacent edges) ─
|
||||
int half = Math.Max(1, L / 4);
|
||||
for (int playerCount = 2; playerCount <= N; playerCount++)
|
||||
{
|
||||
var seatMap = new Dictionary<int, HashSet<string>>();
|
||||
for (int seat = 0; seat < playerCount; seat++)
|
||||
{
|
||||
int ci = (int)Math.Round((double)seat * N / playerCount) % N;
|
||||
var (vx, vy) = verts[ci];
|
||||
var zone = new HashSet<string>();
|
||||
int ci = (int)Math.Round((double)seat * N / playerCount) % N;
|
||||
var (cq, cr) = corners[ci];
|
||||
// Snap corner to nearest non-absent tile using hex-Cartesian lookup.
|
||||
var cornerTile = FindNearestNgonTile(cq + cr * 0.5, cr * Math.Sqrt(3.0) / 2.0, R);
|
||||
var zone = new HashSet<string> { TileKey(cornerTile.x, cornerTile.y) };
|
||||
|
||||
// Corner tile
|
||||
var corner = FindNearestNgonTile(vx, vy, R);
|
||||
zone.Add(TileKey(corner.x, corner.y));
|
||||
|
||||
// Edge tiles toward adjacent vertices
|
||||
for (int step = 1; step <= half; step++)
|
||||
{
|
||||
double t = (double)step / (half + 1);
|
||||
int nextCi = (ci + 1) % N;
|
||||
int prevCi = (ci - 1 + N) % N;
|
||||
var n1 = FindNearestNgonTile(vx + (verts[nextCi].vx - vx) * t,
|
||||
vy + (verts[nextCi].vy - vy) * t, R);
|
||||
var n2 = FindNearestNgonTile(vx + (verts[prevCi].vx - vx) * t,
|
||||
vy + (verts[prevCi].vy - vy) * t, R);
|
||||
zone.Add(TileKey(n1.x, n1.y));
|
||||
zone.Add(TileKey(n2.x, n2.y));
|
||||
}
|
||||
// Walk along the hex-line edges toward each adjacent corner.
|
||||
var (nq, nr) = corners[(ci + 1) % N];
|
||||
var (pq, pr) = corners[(ci - 1 + N) % N];
|
||||
foreach (var (lq, lr) in HexLinePath(cq, cr, nq, nr).Skip(1).Take(half))
|
||||
zone.Add(TileKey(lq + R, lr + R));
|
||||
foreach (var (lq, lr) in HexLinePath(cq, cr, pq, pr).Skip(1).Take(half))
|
||||
zone.Add(TileKey(lq + R, lr + R));
|
||||
|
||||
seatMap[seat] = zone;
|
||||
}
|
||||
@@ -628,9 +659,13 @@ else
|
||||
_zones.Clear();
|
||||
if (_template.GridType == "Hexagon")
|
||||
{
|
||||
_template.HexSides = _pendingHexRadius + 1;
|
||||
double circumR = _pendingHexSideLength <= 1 ? 0.0
|
||||
: (_pendingHexSideLength - 1.0) / (2.0 * Math.Sin(Math.PI / _pendingHexPolySides));
|
||||
int R = Math.Max(_pendingHexSideLength * 2, (int)Math.Ceiling(circumR) + 2);
|
||||
R = Math.Clamp(R, 2, 30);
|
||||
_template.HexSides = R + 1;
|
||||
_template.HexPolygonSides = _pendingHexPolySides;
|
||||
int dim = 2 * _pendingHexRadius + 1;
|
||||
int dim = 2 * R + 1;
|
||||
_template.Width = dim;
|
||||
_template.Height = dim;
|
||||
}
|
||||
@@ -656,24 +691,28 @@ else
|
||||
return Math.Max(Math.Abs(q), Math.Max(Math.Abs(r), Math.Abs(q + r))) <= R;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True when scaled hex-Cartesian point (cx,cy) is inside a regular N-gon with
|
||||
/// circumradius R. Uses cross-product test on each CCW edge (slightly expanded to
|
||||
/// include boundary tiles).
|
||||
/// </summary>
|
||||
private static bool NPolyInside(double cx, double cy, int N, double R)
|
||||
/// <summary>Round continuous axial (fq,fr) to the nearest integer hex cell (preserves cube constraint).</summary>
|
||||
private static (int q, int r) HexRound(double fq, double fr)
|
||||
{
|
||||
double Rexp = R * 1.02; // slight expansion to include perimeter tiles
|
||||
for (int k = 0; k < N; k++)
|
||||
double fs = -fq - fr;
|
||||
int rq = (int)Math.Round(fq), rr = (int)Math.Round(fr), rs = (int)Math.Round(fs);
|
||||
double dq = Math.Abs(rq - fq), dr = Math.Abs(rr - fr), ds = Math.Abs(rs - fs);
|
||||
if (dq > dr && dq > ds) rq = -rr - rs;
|
||||
else if (dr > ds) rr = -rq - rs;
|
||||
return (rq, rr);
|
||||
}
|
||||
|
||||
/// <summary>Yield all hex cells on the straight line from (q0,r0) to (q1,r1) via cube lerp.</summary>
|
||||
private static IEnumerable<(int q, int r)> HexLinePath(int q0, int r0, int q1, int r1)
|
||||
{
|
||||
int s0 = -q0 - r0, s1 = -q1 - r1;
|
||||
int steps = Math.Max(Math.Abs(q1 - q0), Math.Max(Math.Abs(r1 - r0), Math.Abs(s1 - s0)));
|
||||
for (int i = 0; i <= steps; i++)
|
||||
{
|
||||
double a1 = Math.PI / 2.0 + 2.0 * Math.PI * k / N;
|
||||
double a2 = Math.PI / 2.0 + 2.0 * Math.PI * (k + 1) / N;
|
||||
double v1x = Rexp * Math.Cos(a1), v1y = Rexp * Math.Sin(a1);
|
||||
double v2x = Rexp * Math.Cos(a2), v2y = Rexp * Math.Sin(a2);
|
||||
double cross = (v2x - v1x) * (cy - v1y) - (v2y - v1y) * (cx - v1x);
|
||||
if (cross < -1e-9) return false;
|
||||
double t = steps == 0 ? 0.0 : (double)i / steps;
|
||||
var pt = HexRound(q0 + (q1 - q0) * t, r0 + (r1 - r0) * t);
|
||||
yield return pt;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Find the nearest non-absent hex tile (in offset coords) to scaled-Cartesian (vx,vy).</summary>
|
||||
|
||||
Reference in New Issue
Block a user