Web Dev Solutions

Catalin Mititiuc

From 12890ea00934c6a2df63fe777799332818e4e5ce Mon Sep 17 00:00:00 2001 From: Catalin Mititiuc Date: Tue, 18 Jun 2024 20:24:54 -0700 Subject: Clean up a bit --- public/radial.html | 2 +- src/radial.js | 38 ++++++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/public/radial.html b/public/radial.html index 21ccc36..9cebada 100644 --- a/public/radial.html +++ b/public/radial.html @@ -134,7 +134,7 @@ - + diff --git a/src/radial.js b/src/radial.js index 0a71f03..22c8904 100644 --- a/src/radial.js +++ b/src/radial.js @@ -62,7 +62,7 @@ function generateRadialCoords(l, { q, r, s }, radius) { } function generateRadialCoordsRect({ rows, columns, odd = false, equal = true } = {}, { q = 0, r = 0, s = 0 } = {}) { - const list = new Set(); + const list = new Map(); for (let row = 0; row < rows; row++) { const alternating = row % 2; @@ -77,7 +77,7 @@ function generateRadialCoordsRect({ rows, columns, odd = false, equal = true } = ds = ds + alternating; } - list.add(toKey(q + dq, r + dr, s + ds)); + list.set(toKey(q + dq, r + dr, s + ds), null); } if (!equal) { @@ -90,7 +90,7 @@ function generateRadialCoordsRect({ rows, columns, odd = false, equal = true } = ds = ds + alternating; } - list.add(toKey(q + dq, r + dr, s + ds)); + list.set(toKey(q + dq, r + dr, s + ds), null); } } } @@ -275,35 +275,40 @@ bld1.appendChild(bld1elv1); bld1hexes.forEach(coords => grid.delete(coords)) bld2hexes.forEach(list => list.forEach(coords => grid.delete(coords))); -// drawHexes(elevation1, grid); - function generateRadialCoords2(l, { q, r, s }) { const origin = toKey(q, r, s); const list = new Map(l); - let level = 0; - list.set(origin, level); + list.set(origin, 0); let queue = [origin]; - const top = 5; - const bottom = 5; - - const left = 1; - const right = 1; + const top = 26; + const bottom = 27; + const left = 17; + const right = 17; + const offset = 'right'; while (queue.length > 0) { - level++; const v = queue.shift(); + getNeighbors(v).forEach(w => { const [wq, wr, ws] = w.split(',').map(n => +n); const rDist = Math.abs(wr - r); const dr = (rDist + rDist % 2) / 2; + let dLeft = left; + let dRight = right; + + if (offset === 'left') + dRight -= rDist % 2; + else if (offset === 'right') + dLeft -= rDist % 2; + if ([ !list.has(w), wr < bottom + r && wr > -top + r, - wq > -right + q - dr && wq < left + q + dr, - ws > -left + s - dr && ws < right + s + dr, + wq > -dRight + q - dr && wq < dLeft + q + dr, + ws > -dLeft + s - dr && ws < dRight + s + dr, ].every(v => v)) { list.set(w, dr); queue.push(w); @@ -335,7 +340,8 @@ const list = generateRadialCoords2(new Map(), { q: 0, r: 0, s: 0 }); // q [-2, -8] // s [-2, 4] -drawHexes(elevation2, generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, 10)); +// drawHexes(elevation2, generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, 10)); +drawHexes(elevation2, grid); drawHexes(elevation1, list); const defs = svg.querySelector('defs'); -- cgit v1.2.3