Web Dev Solutions

Catalin Mititiuc

aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--public/radial.html71
-rw-r--r--src/radial.js183
2 files changed, 186 insertions, 68 deletions
diff --git a/public/radial.html b/public/radial.html
index 784040e..5398186 100644
--- a/public/radial.html
+++ b/public/radial.html
@@ -5,7 +5,7 @@
<style>
polygon {
fill: inherit;
- stroke: #999;
+ stroke: inherit;
stroke-width: 0.25px;
}
@@ -20,7 +20,7 @@
user-select: none;
font-family: sans-serif;
fill: black;
- display: none;
+ /* display: none; */
}
.elevation-basement {
@@ -41,7 +41,8 @@
.elevation-2 {
fill: rgb(240, 216, 172);
- display: none;
+ opacity: 0.3;
+ /* display: none; */
}
.elevation-roof {
@@ -113,24 +114,66 @@
.building.view-elevation-basement .doors {
display: none;
}
+
+ use[href="#hex"] {
+ stroke: #666;
+ /* fill: wheat; */
+ fill: url(#asterisk);
+ }
+
+ .building [class*="elevation"] use {
+ fill-opacity: 0.33;
+ stroke: black;
+ }
+
+ .building {
+ display: none;
+ }
+
</style>
</head>
<body>
- <svg viewBox="-300 -200 600 400" xmlns="http://www.w3.org/2000/svg">
+ <svg viewBox="-200 -150 400 300" xmlns="http://www.w3.org/2000/svg">
+ <!-- <svg viewBox="-30 -20 60 40" xmlns="http://www.w3.org/2000/svg"> -->
<defs>
<polygon id="hex" points="0,10 8.66,5 8.66,-5 0,-10 -8.66,-5 -8.66,5" />
+ <line id="ast-line" stroke="#000000" stroke-width="0.3" x1="-1.2" y1="0" x2="1.2" y2="0" />
+ <pattern id="asterisk" viewBox="-15 -15 30 30" width="30" height="30" patternUnits="userSpaceOnUse">
+ <rect x="-15" y="-15" width="30" height="30" fill="wheat" />
+ <use y="-15" x="-15" href="#ast-line" />
+ <use y="-15" x="15" href="#ast-line" />
+ <use y="15" x="-15" href="#ast-line" />
+ <use y="15" x="15" href="#ast-line" />
+
+ <use y="-15" x="-15" transform="rotate(60, -15, -15)" href="#ast-line" />
+ <use y="15" x="15" transform="rotate(60, 15, 15)" href="#ast-line" />
+ <use y="-15" x="15" transform="rotate(-60, 15, -15)" href="#ast-line" />
+ <use y="15" x="-15" transform="rotate(-60, -15, 15)" href="#ast-line" />
+ </pattern>
</defs>
- <g id="building2" class="building view-elevation-1" transform="translate(-181.86, 105)">
- <!-- <g id="building2" class=".building"> -->
- <path class="floor"
- d="m 38.786985,67.5 17.32,-30 v -45 l -17.32,-30 v -30 h -77.94 v 30 l -17.32,30 v 45 l 17.32,30 z" />
- <path class="outer-wall"
- d="m -21.833015,67.5 v -60 m 17.3200004,-75 v 30 M 17.136985,15 4.146985,7.5 m 51.96,30 -12.99,-7.5 m -38.97,-67.5 v 45 h -60.62 m 17.32,-45 h 77.94 m -69.28,105 h 69.28 l 17.32,-30 v -45 l -17.32,-30 v -30 h -77.94 v 30 l -17.32,30 v 45 l 17.32,30 z" />
- <path class="inner-wall"
- d="m -21.833015,67.5 v -60 m 17.3200004,-75 v 30 M 17.136985,15 4.146985,7.5 m 51.96,30 -12.99,-7.5 m -38.97,-67.5 v 45 h -60.62 m 17.32,-45 h 77.94 m -69.28,105 h 69.28 l 17.32,-30 v -45 l -17.32,-30 v -30 h -77.94 v 30 l -17.32,30 v 45 l 17.32,30 z" />
- <path class="doors"
- d="m 3.698745,-67.5 h 9.55648 m 7.76351,30 h 9.55648 M -4.9612546,67.5 H 4.595225 m -18.71725,-60 h 9.5564804 m -35.0357204,0 h 9.55648 m 33.74353,-75 h 9.55648 m -35.53648,30 h 9.55648 m 7.7635204,105 H 4.595225 m -18.71725,-60 h 9.5564804 m -35.0357204,0 h 9.55648" />
+
+ <g id="building1" class="building view-elevation-1">
+ <g transform="translate(-60.62, 105)">
+ <path class="floor" d="M -38.969999,-67.499999 H 38.969999 V 67.499999 h -77.939998 z" />
+ <path class="outer-wall"
+ d="m 38.969998,-7.500001 h -12.99 v 12.5 m -21.6499995,32.5 H 38.969998 m -34.6399995,30 v -60 h -8.66 v -45 m -8.6600005,105 v -45 h -25.979997 m 0,-89.999998 H 38.969999 V 67.499999 h -77.939998 z" />
+ <path class="inner-wall"
+ d="m 38.969998,-7.500001 h -12.99 v 12.5 m -21.6499995,32.5 H 38.969998 m -34.6399995,30 v -60 h -8.66 v -45 m -8.6600005,105 v -45 h -25.979997 m 0,-89.999998 H 38.969999 V 67.499999 h -77.939998 z" />
+ </g>
+ </g>
+
+ <g id="building2" class="building view-elevation-1">
+ <g transform="translate(-181.86, 105)">
+ <path class="floor"
+ d="m 38.786985,67.5 17.32,-30 v -45 l -17.32,-30 v -30 h -77.94 v 30 l -17.32,30 v 45 l 17.32,30 z" />
+ <path class="outer-wall"
+ d="m -21.833015,67.5 v -60 m 17.3200004,-75 v 30 M 17.136985,15 4.146985,7.5 m 51.96,30 -12.99,-7.5 m -38.97,-67.5 v 45 h -60.62 m 17.32,-45 h 77.94 m -69.28,105 h 69.28 l 17.32,-30 v -45 l -17.32,-30 v -30 h -77.94 v 30 l -17.32,30 v 45 l 17.32,30 z" />
+ <path class="inner-wall"
+ d="m -21.833015,67.5 v -60 m 17.3200004,-75 v 30 M 17.136985,15 4.146985,7.5 m 51.96,30 -12.99,-7.5 m -38.97,-67.5 v 45 h -60.62 m 17.32,-45 h 77.94 m -69.28,105 h 69.28 l 17.32,-30 v -45 l -17.32,-30 v -30 h -77.94 v 30 l -17.32,30 v 45 l 17.32,30 z" />
+ <path class="doors"
+ d="m 3.698745,-67.5 h 9.55648 m 7.76351,30 h 9.55648 M -4.9612546,67.5 H 4.595225 m -18.71725,-60 h 9.5564804 m -35.0357204,0 h 9.55648 m 33.74353,-75 h 9.55648 m -35.53648,30 h 9.55648 m 7.7635204,105 H 4.595225 m -18.71725,-60 h 9.5564804 m -35.0357204,0 h 9.55648" />
+ </g>
</g>
</svg>
<script src="radial.js"></script>
diff --git a/src/radial.js b/src/radial.js
index 5a9f875..dd00232 100644
--- a/src/radial.js
+++ b/src/radial.js
@@ -140,40 +140,51 @@ function drawHexes(el, list) {
});
}
-const grid = generateRadialCoordsRect({ rows: 25, columns: 33 }, { q: 22, r: -12, s: -10 });
+function translateRadialCoords({ q, r, s }, direction, distance) {
+ return {
+ q: q + direction.q * distance,
+ r: r + direction.r * distance,
+ s: s + direction.s * distance
+ };
+}
+
+// const grid = generateRadialCoordsRect({ rows: 26, columns: 33 }, { q: 22, r: -12, s: -10 });
+
+const grid = generateRadialCoordsRect({ rows: 52, columns: 33 }, translateRadialCoords({ q: 22, r: -12, s: -10 }, { q: 1, r: -2, s: 1 }, 7));
+
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);
-// generateRadialCoordsRect({ rows: 4, columns: 8, equal: false }, { q: 18, r: -8, s: -10 }).forEach(coords => grid.delete(coords));
-// generateRadialCoordsRect({ rows: 8, columns: 2, equal: false }, { q: 9, r: -10, s: 1 }).forEach(coords => grid.delete(coords));
-// generateRadialCoordsRect({ rows: 6, columns: 13, odd: true }, { q: 2, r: -8, s: 6 }).forEach(coords => grid.delete(coords));
-// generateRadialCoordsRect({ rows: 1, columns: 10 }, { q: -1, r: -9, s: 10 }).forEach(coords => grid.delete(coords));
-// generateRadialCoordsRect({ rows: 1, columns: 2 }, { q: -1, r: -2, s: 3 }).forEach(coords => grid.delete(coords));
-// generateRadialCoordsRect({ rows: 1, columns: 6 }, { q: -8, r: -2, s: 10 }).forEach(coords => grid.delete(coords));
-// generateRadialCoordsRect({ rows: 8, columns: 4, odd: true }, { q: 4, r: 3, s: -7 }).forEach(coords => grid.delete(coords));
-// generateRadialCoordsRect({ rows: 8, columns: 4, odd: true }, { q: -5, r: 3, s: 2 }).forEach(coords => grid.delete(coords));
-// generateRadialCoordsRect({ rows: 6, columns: 3, equal: false }, { q: -12, r: 3, s: 9 }).forEach(coords => grid.delete(coords));
-// generateRadialCoordsRect({ rows: 8, columns: 4 }, { q: 10, r: 3, s: -13 }).forEach(coords => grid.delete(coords));
-// generateRadialCoordsRect({ rows: 3, columns: 1 }, { q: 9, r: 7, s: -16 }).forEach(coords => grid.delete(coords));
-// generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }).forEach(coords => grid.delete(coords));
-
-const buildingCoords = [
- generateRadialCoordsRect({ rows: 4, columns: 8, equal: false }, { q: 18, r: -8, s: -10 }),
- generateRadialCoordsRect({ rows: 8, columns: 2, equal: false }, { q: 9, r: -10, s: 1 }),
- generateRadialCoordsRect({ rows: 6, columns: 13, odd: true }, { q: 2, r: -8, s: 6 }),
- generateRadialCoordsRect({ rows: 1, columns: 10 }, { q: -1, r: -9, s: 10 }),
- generateRadialCoordsRect({ rows: 1, columns: 2 }, { q: -1, r: -2, s: 3 }),
- generateRadialCoordsRect({ rows: 1, columns: 6 }, { q: -8, r: -2, s: 10 }),
- generateRadialCoordsRect({ rows: 8, columns: 4, odd: true }, { q: 4, r: 3, s: -7 }),
- generateRadialCoordsRect({ rows: 8, columns: 4, odd: true }, { q: -5, r: 3, s: 2 }),
- generateRadialCoordsRect({ rows: 6, columns: 3, equal: false }, { q: -12, r: 3, s: 9 }),
-
- // hex building
- // generateRadialCoordsRect({ rows: 8, columns: 4 }, { q: 10, r: 3, s: -13 }),
- // generateRadialCoordsRect({ rows: 3, columns: 1 }, { q: 9, r: 7, s: -16 }),
- // generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }),
-]
+const buildingData = [
+ [{ rows: 4, columns: 8, equal: false }, { q: 18, r: -8, s: -10 }],
+ [{ rows: 8, columns: 2, equal: false }, { q: 9, r: -10, s: 1 }],
+ [{ rows: 6, columns: 13, odd: true }, { q: 2, r: -8, s: 6 }],
+ [{ rows: 1, columns: 10 }, { q: -1, r: -9, s: 10 }],
+ [{ rows: 1, columns: 2 }, { q: -1, r: -2, s: 3 }],
+ [{ rows: 1, columns: 6 }, { q: -8, r: -2, s: 10 }],
+ [{ rows: 8, columns: 4, odd: true }, { q: -5, r: 3, s: 2 }],
+ [{ rows: 6, columns: 3, equal: false }, { q: -12, r: 3, s: 9 }],
+].map(([size, start]) => [size, translateRadialCoords(start, { q: 1, r: -2, s: 1 }, 7)]);
+
+const buildingCoords = buildingData.map(([size, start]) => generateRadialCoordsRect(size, start));
+
+// const buildingCoords = [
+// generateRadialCoordsRect({ rows: 4, columns: 8, equal: false }, { q: 18, r: -8, s: -10 }),
+// generateRadialCoordsRect({ rows: 8, columns: 2, equal: false }, { q: 9, r: -10, s: 1 }),
+// generateRadialCoordsRect({ rows: 6, columns: 13, odd: true }, { q: 2, r: -8, s: 6 }),
+// generateRadialCoordsRect({ rows: 1, columns: 10 }, { q: -1, r: -9, s: 10 }),
+// generateRadialCoordsRect({ rows: 1, columns: 2 }, { q: -1, r: -2, s: 3 }),
+// generateRadialCoordsRect({ rows: 1, columns: 6 }, { q: -8, r: -2, s: 10 }),
+// // generateRadialCoordsRect({ rows: 8, columns: 4, odd: true }, { q: 4, r: 3, s: -7 }),
+// generateRadialCoordsRect({ rows: 8, columns: 4, odd: true }, { q: -5, r: 3, s: 2 }),
+// generateRadialCoordsRect({ rows: 6, columns: 3, equal: false }, { q: -12, r: 3, s: 9 }),
+
+// // hex building
+// // generateRadialCoordsRect({ rows: 8, columns: 4 }, { q: 10, r: 3, s: -13 }),
+// // generateRadialCoordsRect({ rows: 3, columns: 1 }, { q: 9, r: 7, s: -16 }),
+// // generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }),
+// ]
buildingCoords.forEach(building => building.forEach(coords => grid.delete(coords)));
@@ -194,51 +205,115 @@ buildingCoords.forEach(building => building.forEach(coords => grid.delete(coords
// drawHexes(svg, generateRadialCoordsRect({ rows: 7, columns: 6, odd: true, equal: false }, { q: 5, r: -5, s: 0 }));
-const buildings = document.createElementNS(xmlns, 'g');
-buildings.classList.add('buildings');
+// const buildings = document.createElementNS(xmlns, 'g');
+// buildings.classList.add('buildings');
-const bldElevationBasement = document.createElementNS(xmlns, 'g');
-bldElevationBasement.classList.add('elevation-basement');
-buildingCoords.forEach(building => drawHexes(bldElevationBasement, building));
+// const bldElevationBasement = document.createElementNS(xmlns, 'g');
+// bldElevationBasement.classList.add('elevation-basement');
+// buildingCoords.forEach(building => drawHexes(bldElevationBasement, building));
-const bldElevation1 = document.createElementNS(xmlns, 'g');
-bldElevation1.classList.add('elevation-1');
-buildingCoords.forEach(building => drawHexes(bldElevation1, building));
+// const bldElevation1 = document.createElementNS(xmlns, 'g');
+// bldElevation1.classList.add('elevation-1');
+// buildingCoords.forEach(building => drawHexes(bldElevation1, building));
-const bldElevation2 = document.createElementNS(xmlns, 'g');
-bldElevation2.classList.add('elevation-2');
-buildingCoords.forEach(building => drawHexes(bldElevation2, building));
+// const bldElevation2 = document.createElementNS(xmlns, 'g');
+// bldElevation2.classList.add('elevation-2');
+// buildingCoords.forEach(building => drawHexes(bldElevation2, building));
const elevation1 = document.createElementNS(xmlns, 'g');
elevation1.classList.add('elevation-1');
-drawHexes(elevation1, grid);
-buildings.appendChild(bldElevationBasement);
-buildings.appendChild(bldElevation1);
-buildings.appendChild(bldElevation2);
+// buildings.appendChild(bldElevationBasement);
+// buildings.appendChild(bldElevation1);
+// buildings.appendChild(bldElevation2);
const buildingBlah = svg.querySelector('#building2');
const bld2grid = document.createElementNS(xmlns, 'g');
-drawHexes(bld2grid, generateRadialCoordsRect({ rows: 9, columns: 4, odd: true, equal: false }, { q: 4, r: -4, s: 0 }));
-drawHexes(bld2grid, generateRadialCoordsRect({ rows: 5, columns: 1 }, { q: 3, r: -1, s: -2 }));
-drawHexes(bld2grid, generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: -2, r: -1, s: 3 }));
+
+const bld2hexes = [
+ generateRadialCoordsRect({ rows: 9, columns: 4, odd: true, equal: false }, { q: 11, r: 3, s: -14 }),
+ generateRadialCoordsRect({ rows: 5, columns: 1 }, { q: 10, r: 6, s: -16 }),
+ generateRadialCoordsRect({ rows: 5, columns: 1, odd: true }, { q: 5, r: 6, s: -11 }),
+]
+
+// bld2hexes.forEach(list => drawHexes(bld2grid, list));
const bld2elvBasement = bld2grid.cloneNode(true);
bld2elvBasement.classList.add('elevation-basement');
-buildingBlah.querySelector('.floor').after(bld2elvBasement);
+buildingBlah.appendChild(bld2elvBasement);
const bld2elv1 = bld2grid.cloneNode(true);
bld2elv1.classList.add('elevation-1');
-bld2elvBasement.after(bld2elv1);
+buildingBlah.appendChild(bld2elv1);
const bld2elv2 = bld2grid.cloneNode(true);
bld2elv2.classList.add('elevation-2');
-bld2elv1.after(bld2elv2);
+buildingBlah.appendChild(bld2elv2);
const bld2elvRoof = bld2grid.cloneNode(true);
bld2elvRoof.classList.add('elevation-roof');
-bld2elv2.after(bld2elvRoof);
+buildingBlah.appendChild(bld2elvRoof);
+
+
+const bld1 = svg.querySelector('#building1');
+
+const bld1grid = document.createElementNS(xmlns, 'g');
+const bld1hexes = generateRadialCoordsRect({ rows: 9, columns: 4, odd: true, equal: false }, { q: 4, r: 3, s: -7 });
+// drawHexes(bld1grid, bld1hexes);
+
+const bld1elv1 = bld1grid.cloneNode(true);
+bld1elv1.classList.add('elevation-1');
+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 }, radius) {
+ const origin = toKey(q, r, s);
+ const list = new Set(l);
+ const neighbors = new Set();
+
+ let next = new Set();
+
+ list.add(origin);
+ next.add(origin);
+
+ for (let i = 0; i < radius; i++) {
+ next.forEach(coords => {
+ getNeighbors(coords).forEach(n => {
+ const [q, r, s] = n.split(',').map(n => +n);
+
+ const height = 7;
+ const width = 3;
+ const mod = (Math.abs(r) - height % 2) / 2;
+
+ if (Math.abs(r) < height && Math.abs(q) - mod < width && Math.abs(s) - mod < width) {
+ list.add(n);
+ neighbors.add(n);
+ }
+ });
+ });
+
+ next = new Set(neighbors);
+ neighbors.clear();
+ }
+
+ return list;
+}
+
+const elevation2 = document.createElementNS(xmlns, 'g');
+elevation2.classList.add('elevation-2');
+
+
+drawHexes(elevation2, generateRadialCoords(new Map(), { q: 0, r: 0, s: 0 }, 10));
+drawHexes(elevation1, generateRadialCoords2(new Map(), { q: 0, r: 0, s: 0 }, 10));
+
-buildingBlah.before(elevation1);
-buildingBlah.before(buildings);
+const defs = svg.querySelector('defs');
+defs.after(elevation2);
+elevation2.after(elevation1);