/
d3-hexjson.js
359 lines (304 loc) · 11.3 KB
/
d3-hexjson.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-array')) :
typeof define === 'function' && define.amd ? define(['exports', 'd3-array'], factory) :
(factory((global.d3 = global.d3 || {}),global.d3));
}(this, function (exports,d3Array) { 'use strict';
// Main render method
function renderHexJSON (hexjson, width, height) {
// Get the layout
var layout = hexjson.layout;
// Get the hex objects as an array
var hexes = [];
var hexRadius = 0;
Object.keys(hexjson.hexes).forEach(function (key) {
hexjson.hexes[key].key = key;
hexes.push(hexjson.hexes[key]);
});
// Calculate the number of rows and columns
var qmax = d3Array.max(hexes, function (d) { return +d.q }),
qmin = d3Array.min(hexes, function (d) { return +d.q }),
rmax = d3Array.max(hexes, function (d) { return +d.r }),
rmin = d3Array.min(hexes, function (d) { return +d.r });
var qnum = qmax - qmin + 1,
rnum = rmax - rmin + 1;
// Calculate maximum radius the hexagons can have to fit the svg
if (layout === "odd-r" || layout === "even-r") {
hexRadius = d3Array.min([(width) / ((qnum + 0.5) * Math.sqrt(3)),
height / ((rnum + 1 / 3) * 1.5)]);
} else {
hexRadius = d3Array.min([(height) / ((rnum + 0.5) * Math.sqrt(3)),
width / ((qnum + 1 / 3) * 1.5)]);
}
// Calculate the hexagon width
var hexWidth = hexRadius * Math.sqrt(3);
// Get the vertices and points for this layout
var vertices = getVertices(layout, hexWidth, hexRadius);
var points = getPoints(vertices);
// Calculate the values needed to render each hex and add to hexes
hexes.forEach(function (hex) {
// Calculate the absolute co-ordinates of each hex
hex.qc = hex.q - qmin;
hex.rc = rmax - hex.r;
// Calculate the x and y position of each hex for this svg
hex.x = getX(hex, layout, hexWidth, hexRadius);
hex.y = getY(hex, layout, hexWidth, hexRadius);
// Add the vertex positions and points relative to x and y
hex.vertices = vertices;
hex.points = points;
});
return hexes;
}
// Get the x position for a hex
function getX (hex, layout, hexWidth, hexRadius) {
var x = 0,
xOffset = 0;
switch (layout) {
case "odd-r":
xOffset = (hex.rc % 2 === 1) ? hexWidth : (hexWidth / 2);
x = (hex.qc * hexWidth) + xOffset;
break;
case "even-r":
xOffset = (hex.rc % 2 === 0) ? hexWidth : (hexWidth / 2);
x = (hex.qc * hexWidth) + xOffset;
break;
case "odd-q":
case "even-q":
x = (hex.qc * hexRadius * 1.5) + hexRadius;
break;
}
return x;
}
// Get the y position for a hex
function getY (hex, layout, hexWidth, hexRadius) {
var y = 0,
yOffset = 0;
switch (layout) {
case "odd-r":
case "even-r":
y = (hex.rc * hexRadius * 1.5) + hexRadius;
break;
case "odd-q":
yOffset = (hex.qc % 2 === 1) ? hexWidth : (hexWidth / 2);
y = (hex.rc * hexWidth) + yOffset;
break;
case "even-q":
yOffset = (hex.qc % 2 === 0) ? hexWidth : (hexWidth / 2);
y = (hex.rc * hexWidth) + yOffset;
break;
}
return y;
}
// Get the positions of the vertices for the hex:
// - Row layouts are ordered from the topmost vertex clockwise
// - Column layouts are ordered from the leftmost vertex clockwise
function getVertices (layout, hexWidth, hexRadius) {
var vertices = [];
switch (layout) {
case "odd-r":
case "even-r":
vertices.push({x: 0, y: (0 - hexRadius)});
vertices.push({x: (0 + hexWidth * 0.5), y: (0 - 0.5 * hexRadius)});
vertices.push({x: (0 + hexWidth * 0.5), y: (0 + 0.5 * hexRadius)});
vertices.push({x: 0, y: (0 + hexRadius)});
vertices.push({x: (0 - hexWidth * 0.5), y: (0 + 0.5 * hexRadius)});
vertices.push({x: (0 - hexWidth * 0.5), y: (0 - 0.5 * hexRadius)});
break;
case "odd-q":
case "even-q":
vertices.push({x: (0 - hexRadius), y: 0});
vertices.push({x: (0 - 0.5 * hexRadius), y: (0 - hexWidth * 0.5)});
vertices.push({x: (0 + 0.5 * hexRadius), y: (0 - hexWidth * 0.5)});
vertices.push({x: (0 + hexRadius), y: 0});
vertices.push({x: (0 + 0.5 * hexRadius), y: (0 + hexWidth * 0.5)});
vertices.push({x: (0 - 0.5 * hexRadius), y: (0 + hexWidth * 0.5)});
break;
}
return vertices;
}
// Get the points attribute for a polygon with these vertices
function getPoints (vertices) {
var points = "";
vertices.forEach(function (v) { points += v.x + "," + v.y + " " });
return points.substring(0, points.length - 1);
}
// Creates a hexjson grid with the layout and dimensions of the given hexjson
function getGridForHexJSON (hexjson) {
// Create a new HexJSON object for the grid
var grid = {};
grid.layout = hexjson.layout;
grid.hexes = {};
// Get the hex objects from the hexjson as an array
var hexes = [];
Object.keys(hexjson.hexes).forEach(function (key) {
hexes.push(hexjson.hexes[key]);
});
// Calculate the number of rows and columns in the grid
var qmax = d3Array.max(hexes, function (d) { return +d.q }),
qmin = d3Array.min(hexes, function (d) { return +d.q }),
rmax = d3Array.max(hexes, function (d) { return +d.r }),
rmin = d3Array.min(hexes, function (d) { return +d.r });
// Create the hexjson grid
var i, j, fkey;
for (i = qmin; i <= qmax; i++) {
for (j = rmin; j <= rmax; j++) {
fkey = "Q" + i + "R" + j;
grid.hexes[fkey] = {q: i, r: j};
}
}
return grid;
}
// Creates a list of dots along the boundaries between
// hexes which have different values of "field"
function getBoundaryDotsForHexJSON (hexjson, width, height, field) {
// Get the hex objects from the hexjson as an array
var hexes = [];
var layout = hexjson.layout;
Object.keys(hexjson.hexes).forEach(function (key) {
hexes.push(hexjson.hexes[key]);
});
// Calculate the number of rows and columns
var qmax = d3Array.max(hexes, function (d) { return +d.q }),
qmin = d3Array.min(hexes, function (d) { return +d.q }),
rmax = d3Array.max(hexes, function (d) { return +d.r }),
rmin = d3Array.min(hexes, function (d) { return +d.r });
var qnum = qmax - qmin + 1,
rnum = rmax - rmin + 1;
var hexRadius;
// Calculate maximum radius the hexagons can have to fit the svg
if (layout === "odd-r" || layout === "even-r") {
hexRadius = d3Array.min([(width) / ((qnum + 0.5) * Math.sqrt(3)),
height / ((rnum + 1 / 3) * 1.5)]);
} else {
hexRadius = d3Array.min([(height) / ((rnum + 0.5) * Math.sqrt(3)),
width / ((qnum + 1 / 3) * 1.5)]);
}
// Calculate the hexagon width
var hexWidth = hexRadius * Math.sqrt(3);
// Create an array into which we will put points along the
// boundaries between differing hexes.
// Each edge has five points, equally spaced.
var lines = [];
var hexRadiusSquared = hexRadius * hexRadius * 4;
var maxHex = hexes.length;
if (maxHex > 1) {
hexes.forEach(function (hex) {
hex.qc = hex.q - qmin;
hex.rc = rmax - hex.r;
// Calculate the x and y position of each hex for this svg
hex.x = getX(hex, layout, hexWidth, hexRadius);
hex.y = getY(hex, layout, hexWidth, hexRadius);
});
for (var i = 0; i < maxHex - 1; i++) {
for (var j = i + 1; j < maxHex; j++) {
var hex = hexes[i];
var otherHex = hexes[j];
if (hex[field] !== otherHex[field]) {
if (Math.abs(hex.q - otherHex.q) <= 1 &&
Math.abs(hex.r - otherHex.r) <= 1) {
if (((hex.x - otherHex.x) * (hex.x - otherHex.x)) +
((hex.y - otherHex.y) * (hex.y - otherHex.y)) < hexRadiusSquared) {
// They're neighbours
var midpoint = {};
midpoint.x = otherHex.x + (hex.x - otherHex.x) / 2;
midpoint.y = otherHex.y + (hex.y - otherHex.y) / 2;
var perp = {};
var denom = Math.sqrt(3) * 4;
perp.dx = (hex.y - otherHex.y) / denom;
perp.dy = -(hex.x - otherHex.x) / denom;
lines.push({x: midpoint.x - 2 * perp.dx, y: midpoint.y - 2 * perp.dy});
lines.push({x: midpoint.x - perp.dx, y: midpoint.y - perp.dy});
lines.push({x: midpoint.x, y: midpoint.y});
lines.push({x: midpoint.x + perp.dx, y: midpoint.y + perp.dy});
lines.push({x: midpoint.x + 2 * perp.dx, y: midpoint.y + 2 * perp.dy});
}
}
}
}
}
}
return lines;
}
// Creates a list of line segments along the boundaries
// between hexes which have different values of "field"
function getBoundarySegmentsForHexJSON (hexjson, width, height, field) {
// Get the hex objects from the hexjson as an array
var hexes = [];
var layout = hexjson.layout;
Object.keys(hexjson.hexes).forEach(function (key) {
hexes.push(hexjson.hexes[key]);
});
// Calculate the number of rows and columns
var qmax = d3Array.max(hexes, function (d) { return +d.q }),
qmin = d3Array.min(hexes, function (d) { return +d.q }),
rmax = d3Array.max(hexes, function (d) { return +d.r }),
rmin = d3Array.min(hexes, function (d) { return +d.r });
var qnum = qmax - qmin + 1,
rnum = rmax - rmin + 1;
var hexRadius;
// Calculate maximum radius the hexagons can have to fit the svg
if (layout === "odd-r" || layout === "even-r") {
hexRadius = d3Array.min([(width) / ((qnum + 0.5) * Math.sqrt(3)),
height / ((rnum + 1 / 3) * 1.5)]);
} else {
hexRadius = d3Array.min([(height) / ((rnum + 0.5) * Math.sqrt(3)),
width / ((qnum + 1 / 3) * 1.5)]);
}
// Calculate the hexagon width
var hexWidth = hexRadius * Math.sqrt(3);
// Create an array into which we will put points along the
// boundaries between differing hexes.
// Each segment will be of the form
// {x: <start point X>, y: <start point Y>, cx: <difference X>, cy: <difference Y> }
// intended to be used with the simple line drawing functionality of d3
//
var segments = [];
var hexRadiusSquared = hexRadius * hexRadius * 4;
var maxHex = hexes.length;
if (maxHex > 1) {
hexes.forEach(function (hex) {
hex.qc = hex.q - qmin;
hex.rc = rmax - hex.r;
// Calculate the x and y position of each hex for this svg
hex.x = getX(hex, layout, hexWidth, hexRadius);
hex.y = getY(hex, layout, hexWidth, hexRadius);
});
for (var i = 0; i < maxHex - 1; i++) {
for (var j = i + 1; j < maxHex; j++) {
var hex = hexes[i];
var otherHex = hexes[j];
if (hex[field] !== otherHex[field]) {
if (Math.abs(hex.q - otherHex.q) <= 1 &&
Math.abs(hex.r - otherHex.r) <= 1) {
if (((hex.x - otherHex.x) * (hex.x - otherHex.x)) +
((hex.y - otherHex.y) * (hex.y - otherHex.y)) < hexRadiusSquared) {
// They're neighbours
var midpoint = {};
midpoint.x = otherHex.x + (hex.x - otherHex.x) / 2;
midpoint.y = otherHex.y + (hex.y - otherHex.y) / 2;
var perp = {};
var direction = +1;
if (hex[field] < otherHex[field]) {
direction = -1;
} // otherwise, direction will be +1
var denom = Math.sqrt(3) * 2 * direction;
perp.dx = (hex.y - otherHex.y) / denom;
perp.dy = -(hex.x - otherHex.x) / denom;
segments.push({
x1: midpoint.x - perp.dx,
y1: midpoint.y - perp.dy,
x2: midpoint.x + perp.dx,
y2: midpoint.y + perp.dy});
}
}
}
}
}
}
return segments;
}
exports.renderHexJSON = renderHexJSON;
exports.getGridForHexJSON = getGridForHexJSON;
exports.getBoundaryDotsForHexJSON = getBoundaryDotsForHexJSON;
exports.getBoundarySegmentsForHexJSON = getBoundarySegmentsForHexJSON;
Object.defineProperty(exports, '__esModule', { value: true });
}));