From 2979c6c3fdfe89ea0a15669540a20459377c0a8c Mon Sep 17 00:00:00 2001
From: Catalin Mititiuc
Date: Mon, 17 Jun 2024 14:55:56 -0700
Subject: Rectangle grid can have unequal row lenghts
---
src/radial.js | 39 ++++++++++++++++++++++++++++++++-------
1 file changed, 32 insertions(+), 7 deletions(-)
(limited to 'src/radial.js')
diff --git a/src/radial.js b/src/radial.js
index dd368f7..add5fe3 100644
--- a/src/radial.js
+++ b/src/radial.js
@@ -61,14 +61,14 @@ function generateRadialCoords(l, { q, r, s }, radius) {
return list;
}
-function generateRadialCoordsRect({ rows, columns, odd = false } = {}, { q = 0, r = 0, s = 0 } = {}) {
+function generateRadialCoordsRect({ rows, columns, odd = false, equal = true } = {}, { q = 0, r = 0, s = 0 } = {}) {
const list = new Set();
- for (let col = 0; col < columns; col++) {
- for (let row = 0; row < rows; row++) {
- const alternating = row % 2;
- const dr = row;
+ for (let row = 0; row < rows; row++) {
+ const alternating = row % 2;
+ const dr = row;
+ for (let col = 0; col < columns; col++) {
let dq = -(alternating + row) / 2 + alternating - col;
let ds = -(row + alternating) / 2 + col;
@@ -79,6 +79,20 @@ function generateRadialCoordsRect({ rows, columns, odd = false } = {}, { q = 0,
list.add(toKey(q + dq, r + dr, s + ds));
}
+
+ if (!equal) {
+ if ((odd && !(row % 2)) || (!odd && row % 2)) {
+ let dq = -(alternating + row) / 2 + alternating - columns;
+ let ds = -(row + alternating) / 2 + columns;
+
+ if (odd) {
+ dq = dq - alternating;
+ ds = ds + alternating;
+ }
+
+ list.add(toKey(q + dq, r + dr, s + ds));
+ }
+ }
}
return list;
@@ -123,12 +137,11 @@ function drawHexes(el, list) {
[use, qText, rText, sText].forEach(el => g.appendChild(el));
el.appendChild(g);
- // map.set(key, g);
});
}
const grid = generateRadialCoordsRect({ rows: 10, columns: 14 }, { q: 9, r: -5, s: -4 });
-const building1 = generateRadialCoordsRect({ rows: 4, columns: 3 }, { q: 6, r: -3, s: -3 });
+const building1 = generateRadialCoordsRect({ rows: 5, columns: 3, equal: false }, { q: 7, r: -4, s: -3 });
const building2 = generateRadialCoordsRect({ rows: 4, columns: 3, odd: true }, { q: 0, r: -4, s: 4 });
const building3 = generateRadialCoords(new Map(), { q: -1, r: 2, s: -1 }, 1);
@@ -137,3 +150,15 @@ building2.forEach(coords => grid.delete(coords));
building3.forEach(coords => grid.delete(coords));
drawHexes(svg, grid);
+
+// const rectEven = generateRadialCoordsRect({ rows: 2, columns: 2 }, { q: 9, r: -5, s: -4 });
+// const rectOdd = generateRadialCoordsRect({ rows: 2, columns: 2, odd: true }, { q: 8, r: -2, s: -6 });
+// const rectEvenUnevenCols = generateRadialCoordsRect({ rows: 2, columns: 1, equal: false}, { q: 6, r: 1, s: -7 });
+// const rectOddUnevenCols = generateRadialCoordsRect({ rows: 2, columns: 1, odd: true, equal: false}, { q: 5, r: 4, s: -9 });
+
+// drawHexes(svg, rectEven);
+// drawHexes(svg, rectOdd);
+// drawHexes(svg, rectEvenUnevenCols);
+// drawHexes(svg, rectOddUnevenCols);
+
+// drawHexes(svg, generateRadialCoordsRect({ rows: 7, columns: 6, odd: true, equal: false }, { q: 5, r: -5, s: 0 }));
--
cgit v1.2.3