Fix N-gon board shape for all N using cube-lerp edge drawing

Replace the direction-snapping approach (which broke N>6 and N=5) with:

1. Place N corners as a true regular N-gon in Cartesian space (radius =
   sideLength), then snap each to the nearest hex via cube-coordinate
   rounding (CartToHex / CubeRound).
2. Draw each edge with cube-space lerp (HexLine): interpolate in cube
   coords and round at each step, producing the canonical hex-lattice
   line between any two tiles regardless of direction.
3. Flood-fill from (0,0) with the edge tiles as walls.
4. Mark absent all in-bounds tiles outside the filled region.

This correctly handles all N in [3,10] without direction quantisation
or bridging hacks.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-04-06 17:08:04 -04:00
co-authored by Claude Sonnet 4.6
parent d00d7150bb
commit a581b69c2a
@@ -540,10 +540,13 @@ else
}
/// <summary>
/// Generate a regular N-gon board of hex tiles.
/// Each of the N sides is a straight run of sideLength tiles along the nearest hex-grid
/// direction to 360°/N·k. Corners are connected with bridging tiles when the walk doesn't
/// land exactly on the next corner. The interior is flood-filled from the centre.
/// Generate a regular N-gon board of hex tiles for any N in [3,10].
/// Algorithm:
/// 1. Place N corners as a regular N-gon in Cartesian space (scaled by sideLength),
/// then convert each to the nearest hex using cube-coordinate rounding.
/// 2. Draw each edge via cube-space lerp so the line follows the hex lattice naturally.
/// 3. Flood-fill the interior from (0,0), using the border as walls.
/// 4. Mark absent all in-bounds hex cells that are outside the filled region.
/// HexSides = bounding radius + 1; HexPolygonSides = N.
/// </summary>
private void ApplyHexConfig()
@@ -551,87 +554,87 @@ else
int N = Math.Clamp(_pendingHexPolySides, 3, 10);
int sideLength = Math.Clamp(_pendingHexRadius, 2, 15);
// Six axial directions for a pointy-top hex grid, with their angles (degrees).
// Angle convention: x_cart = q + r·½, y_cart = −r·√3/2 (y-up / screen-inverted).
(int dq, int dr)[] hexDirs = { (1,0), (1,-1), (0,-1), (-1,0), (-1,1), (0,1) };
double[] hexAngles = { 0, 60, 120, 180, 240, 300 };
// ── Helpers: cube ↔ axial, rounding, hex-line ────────────────────────────
// ── 1. Choose one hex direction per side ─────────────────────────────────
var sideDirs = new (int dq, int dr)[N];
for (int k = 0; k < N; k++)
// Fractional Cartesian → fractional axial (pointy-top, hexSize = 1)
static (double fq, double fr) CartToAxial(double x, double y)
=> (x * 2.0 / 3.0, -x / 3.0 + y * Math.Sqrt(3.0) / 3.0);
// Cube-coordinate rounding
static (int cx, int cy, int cz) CubeRound(double fx, double fy, double fz)
{
double target = 360.0 * k / N;
int best = 0;
double bestDiff = double.MaxValue;
for (int i = 0; i < 6; i++)
int rx = (int)Math.Round(fx), ry = (int)Math.Round(fy), rz = (int)Math.Round(fz);
double dx = Math.Abs(rx - fx), dy = Math.Abs(ry - fy), dz = Math.Abs(rz - fz);
if (dx > dy && dx > dz) rx = -ry - rz;
else if (dy > dz) ry = -rx - rz;
else rz = -rx - ry;
return (rx, ry, rz);
}
// Axial → cube
static (double cx, double cy, double cz) AxialToCube(double q, double r)
=> (q, -q - r, r);
// Snap a Cartesian point to the nearest hex tile (axial)
static (int q, int r) CartToHex(double x, double y)
{
var (fq, fr) = CartToAxial(x, y);
var (cx, cy, cz) = AxialToCube(fq, fr);
var (rx, _, rz) = CubeRound(cx, cy, cz);
return (rx, rz); // axial q = cube x, axial r = cube z
}
// Hex distance (axial)
static int HexDist(int aq, int ar, int bq, int br)
=> (Math.Abs(aq - bq) + Math.Abs(aq + ar - bq - br) + Math.Abs(ar - br)) / 2;
// All tiles on the hex line from (aq,ar) to (bq,br) inclusive
static IEnumerable<(int q, int r)> HexLine(int aq, int ar, int bq, int br)
{
int steps = HexDist(aq, ar, bq, br);
if (steps == 0) { yield return (aq, ar); yield break; }
// Work in cube space
double ax = aq, ay = (double)(-aq - ar), az = ar;
double bx = bq, by = (double)(-bq - br), bz = br;
for (int i = 0; i <= steps; i++)
{
double diff = (hexAngles[i] - target + 360) % 360;
if (diff > 180) diff = 360 - diff;
if (diff < bestDiff) { bestDiff = diff; best = i; }
double t = (double)i / steps;
var (rx, _, rz) = CubeRound(ax + (bx - ax) * t,
ay + (by - ay) * t,
az + (bz - az) * t);
yield return (rx, rz);
}
sideDirs[k] = hexDirs[best];
}
// ── 2. Compute corner positions in axial coords, centred at origin ───────
var rawCorners = new (int q, int r)[N];
int cqAcc = 0, crAcc = 0;
for (int k = 0; k < N; k++)
{
rawCorners[k] = (cqAcc, crAcc);
cqAcc += sideLength * sideDirs[k].dq;
crAcc += sideLength * sideDirs[k].dr;
}
double avgQ = rawCorners.Average(c => (double)c.q);
double avgR = rawCorners.Average(c => (double)c.r);
int shiftQ = -(int)Math.Round(avgQ);
int shiftR = -(int)Math.Round(avgR);
// ── 1. Compute N corners in axial hex coords ──────────────────────────────
// Regular N-gon in Cartesian, radius = sideLength, top-pointing (−π/2 offset).
var corners = new (int q, int r)[N];
for (int k = 0; k < N; k++)
corners[k] = (rawCorners[k].q + shiftQ, rawCorners[k].r + shiftR);
{
double angle = -Math.PI / 2.0 + 2.0 * Math.PI * k / N;
corners[k] = CartToHex(sideLength * Math.Cos(angle),
sideLength * Math.Sin(angle));
}
// ── 3. Bounding hex radius ────────────────────────────────────────────────
// ── 2. Bounding hex radius ────────────────────────────────────────────────
int R = sideLength;
foreach (var (q, r) in corners)
R = Math.Max(R, Math.Max(Math.Abs(q), Math.Max(Math.Abs(r), Math.Abs(q + r))));
R += 1; // one-cell padding so no board tile touches the canvas edge
R += 1;
// ── 4. Walk perimeter, bridging any gap at each corner ───────────────────
// ── 3. Draw edges via cube lerp → border set ──────────────────────────────
var borderSet = new HashSet<(int q, int r)>();
for (int k = 0; k < N; k++)
{
var (sq, sr) = corners[k];
var (eq, er) = corners[(k + 1) % N];
var (dq, dr) = sideDirs[k];
int pq = sq, pr = sr;
for (int step = 0; step < sideLength; step++)
{
borderSet.Add((pq, pr));
pq += dq;
pr += dr;
}
// Bridge from (pq,pr) → (eq,er) if the walk didn't land exactly there.
int safety = 0;
while ((pq != eq || pr != er) && safety++ < sideLength * 3)
{
borderSet.Add((pq, pr));
int bestBdq = 0, bestBdr = 0;
double bestDist = double.MaxValue;
foreach (var (hdq, hdr) in hexDirs)
{
int nq = pq + hdq, nr = pr + hdr;
double d = Math.Max(Math.Abs(eq - nq),
Math.Max(Math.Abs(er - nr),
Math.Abs((eq - nq) + (er - nr))));
if (d < bestDist) { bestDist = d; bestBdq = hdq; bestBdr = hdr; }
}
pq += bestBdq;
pr += bestBdr;
}
var (aq, ar) = corners[k];
var (bq, br) = corners[(k + 1) % N];
foreach (var tile in HexLine(aq, ar, bq, br))
borderSet.Add(tile);
}
// ── 5. Flood-fill the interior from the centre tile (0,0) ────────────────
// ── 4. Flood-fill interior from (0,0) ────────────────────────────────────
(int dq, int dr)[] hexDirs = { (1,0), (1,-1), (0,-1), (-1,0), (-1,1), (0,1) };
var present = new HashSet<(int q, int r)>(borderSet);
var queue = new Queue<(int q, int r)>();
if (!borderSet.Contains((0, 0)))
@@ -642,9 +645,9 @@ else
while (queue.Count > 0)
{
var (q, r) = queue.Dequeue();
foreach (var (hdq, hdr) in hexDirs)
foreach (var (dq, dr) in hexDirs)
{
int nq = q + hdq, nr = r + hdr;
int nq = q + dq, nr = r + dr;
if (present.Contains((nq, nr))) continue;
if (Math.Max(Math.Abs(nq), Math.Max(Math.Abs(nr), Math.Abs(nq + nr))) > R) continue;
present.Add((nq, nr));
@@ -653,7 +656,7 @@ else
}
}
// ── 6. Commit to template ─────────────────────────────────────────────────
// ── 5. Commit to template ─────────────────────────────────────────────────
int dim = 2 * R + 1;
int hexSides = R + 1;
@@ -677,7 +680,7 @@ else
GetOrCreateTile(x, y).IsAbsent = true;
}
// ── 7. Generate starting zones at the N polygon corners ──────────────────
// ── 6. Generate starting zones at the N polygon corners ──────────────────
int half = Math.Max(1, sideLength / 4);
for (int playerCount = 2; playerCount <= N; playerCount++)
{