1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
if (window.IS_DEV) {
const source = new EventSource('/esbuild');
source.addEventListener('change', () => location.reload());
}
function toKey(q, r, s) {
return `${[q, r, s]}`;
}
function getNeighbors(coords) {
const [q, r, s] = coords.split(',').map(n => +n);
return [
toKey(q + 1, r, s - 1),
toKey(q - 1, r, s + 1),
toKey(q + 1, r - 1, s),
toKey(q - 1, r + 1, s),
toKey(q, r + 1, s - 1),
toKey(q, r - 1, s + 1),
]
}
function generateRadialCoords(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 => {
list.add(n);
neighbors.add(n);
});
});
next = new Set(neighbors);
neighbors.clear();
}
return list;
}
function sameSigns(a, b) {
return a > -1 && b > -1 || a < 0 && b < 0;
}
function generateRadialCoordsRect({ rows, columns }, { q = 0, r = 0, s = 0 } = {}) {
const list = new Set();
for (let col = 0; col < columns; col++) {
for (let rw = 0; rw < rows; rw++) {
const alternate = rw % 2;
const dq = -(alternate + rw) / 2 + alternate - col;
const dr = rw;
const ds = -(rw + alternate) / 2 + col;
list.add(toKey(q + dq, r + dr, s + ds));
}
}
return list;
}
const hex = {
inradius: 8.66,
circumradius: 10,
}
const horzSpacing = hex.inradius;
const vertSpacing = hex.circumradius * 3 / 2;
// let list = generateRadialCoords(new Map(), { q: 2, r: 0, s: -2 }, 1);
// list = generateRadialCoords(list, { q: -2, r: 0, s: 2 }, 1);
// list = generateRadialCoords(list, { q: 0, r: 0, s: 0 }, 0);
const list = generateRadialCoordsRect({ rows: 4, columns: 5 }, { q: 7, r: -4, s: -3 });
const svg = document.querySelector('svg');
const xmlns = 'http://www.w3.org/2000/svg';
// const map = new Map();
list.forEach(key => {
const [q, r, s] = key.split(',').map(n => +n);
let x;
if (q === s)
x = 0;
else if (sameSigns(q, s))
x = Math.abs(q - s);
else
x = Math.abs(q) + Math.abs(s);
x = (q > s ? -1 : 1) * x * horzSpacing;
y = r * vertSpacing;
const g = document.createElementNS(xmlns, 'g');
g.setAttributeNS(null, 'transform', `translate(${x}, ${y})`);
const use = document.createElementNS(xmlns, 'use');
use.setAttributeNS(null, 'href', '#hex');
const qText = document.createElementNS(xmlns, 'text');
qText.textContent = q;
qText.setAttributeNS(null, 'x', -3);
qText.setAttributeNS(null, 'y', -3);
const rText = document.createElementNS(xmlns, 'text');
rText.textContent = r;
rText.setAttributeNS(null, 'x', 5);
rText.setAttributeNS(null, 'y', 1.5);
const sText = document.createElementNS(xmlns, 'text');
sText.textContent = s;
sText.setAttributeNS(null, 'x', -3);
sText.setAttributeNS(null, 'y', + 5);
[use, qText, rText, sText].forEach(el => g.appendChild(el));
svg.appendChild(g);
// map.set(key, g);
});
|