-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhexlib.cs
556 lines (444 loc) · 15 KB
/
hexlib.cs
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// Generated code -- CC0 -- No Rights Reserved -- http://www.redblobgames.com/grids/hexagons/
using System;
using System.Linq;
using System.Collections.Generic;
// TODO: should probably use Godot Vector2 instead of this Point model
public struct Point
{
public Point(double x, double y)
{
this.x = x;
this.y = y;
}
public readonly double x;
public readonly double y;
}
public struct Hex
{
public Hex(int q, int r, int s)
{
this.q = q;
this.r = r;
this.s = s;
if (q + r + s != 0) throw new ArgumentException("q + r + s must be 0");
}
public readonly int q;
public readonly int r;
public readonly int s;
public Hex Add(Hex b)
{
return new Hex(q + b.q, r + b.r, s + b.s);
}
public Hex Subtract(Hex b)
{
return new Hex(q - b.q, r - b.r, s - b.s);
}
public Hex Scale(int k)
{
return new Hex(q * k, r * k, s * k);
}
public Hex RotateLeft()
{
return new Hex(-s, -q, -r);
}
public Hex RotateRight()
{
return new Hex(-r, -s, -q);
}
static public List<Hex> directions = new List<Hex> { new Hex(1, 0, -1), new Hex(1, -1, 0), new Hex(0, -1, 1), new Hex(-1, 0, 1), new Hex(-1, 1, 0), new Hex(0, 1, -1) };
static public Hex Direction(int direction)
{
return Hex.directions[direction];
}
public Hex Neighbor(int direction)
{
return Add(Hex.Direction(direction));
}
static public List<Hex> diagonals = new List<Hex> { new Hex(2, -1, -1), new Hex(1, -2, 1), new Hex(-1, -1, 2), new Hex(-2, 1, 1), new Hex(-1, 2, -1), new Hex(1, 1, -2) };
public Hex DiagonalNeighbor(int direction)
{
return Add(Hex.diagonals[direction]);
}
public int Length()
{
return (int)((Math.Abs(q) + Math.Abs(r) + Math.Abs(s)) / 2);
}
public int Distance(Hex b)
{
return Subtract(b).Length();
}
}
public struct FractionalHex
{
public FractionalHex(double q, double r, double s)
{
this.q = q;
this.r = r;
this.s = s;
if (Math.Round(q + r + s) != 0) throw new ArgumentException("q + r + s must be 0");
}
public readonly double q;
public readonly double r;
public readonly double s;
public Hex HexRound()
{
int qi = (int)(Math.Round(q));
int ri = (int)(Math.Round(r));
int si = (int)(Math.Round(s));
double q_diff = Math.Abs(qi - q);
double r_diff = Math.Abs(ri - r);
double s_diff = Math.Abs(si - s);
if (q_diff > r_diff && q_diff > s_diff)
{
qi = -ri - si;
}
else
if (r_diff > s_diff)
{
ri = -qi - si;
}
else
{
si = -qi - ri;
}
return new Hex(qi, ri, si);
}
public FractionalHex HexLerp(FractionalHex b, double t)
{
return new FractionalHex(q * (1.0 - t) + b.q * t, r * (1.0 - t) + b.r * t, s * (1.0 - t) + b.s * t);
}
static public List<Hex> HexLinedraw(Hex a, Hex b)
{
int N = a.Distance(b);
FractionalHex a_nudge = new FractionalHex(a.q + 1e-06, a.r + 1e-06, a.s - 2e-06);
FractionalHex b_nudge = new FractionalHex(b.q + 1e-06, b.r + 1e-06, b.s - 2e-06);
List<Hex> results = new List<Hex> { };
double step = 1.0 / Math.Max(N, 1);
for (int i = 0; i <= N; i++)
{
results.Add(a_nudge.HexLerp(b_nudge, step * i).HexRound());
}
return results;
}
}
public struct OffsetCoord
{
public OffsetCoord(int col, int row)
{
this.col = col;
this.row = row;
}
public readonly int col;
public readonly int row;
static public int EVEN = 1;
static public int ODD = -1;
static public OffsetCoord QoffsetFromCube(int offset, Hex h)
{
int col = h.q;
int row = h.r + (int)((h.q + offset * (h.q & 1)) / 2);
if (offset != OffsetCoord.EVEN && offset != OffsetCoord.ODD)
{
throw new ArgumentException("offset must be EVEN (+1) or ODD (-1)");
}
return new OffsetCoord(col, row);
}
static public Hex QoffsetToCube(int offset, OffsetCoord h)
{
int q = h.col;
int r = h.row - (int)((h.col + offset * (h.col & 1)) / 2);
int s = -q - r;
if (offset != OffsetCoord.EVEN && offset != OffsetCoord.ODD)
{
throw new ArgumentException("offset must be EVEN (+1) or ODD (-1)");
}
return new Hex(q, r, s);
}
static public OffsetCoord RoffsetFromCube(int offset, Hex h)
{
int col = h.q + (int)((h.r + offset * (h.r & 1)) / 2);
int row = h.r;
if (offset != OffsetCoord.EVEN && offset != OffsetCoord.ODD)
{
throw new ArgumentException("offset must be EVEN (+1) or ODD (-1)");
}
return new OffsetCoord(col, row);
}
static public Hex RoffsetToCube(int offset, OffsetCoord h)
{
int q = h.col - (int)((h.row + offset * (h.row & 1)) / 2);
int r = h.row;
int s = -q - r;
if (offset != OffsetCoord.EVEN && offset != OffsetCoord.ODD)
{
throw new ArgumentException("offset must be EVEN (+1) or ODD (-1)");
}
return new Hex(q, r, s);
}
}
public struct DoubledCoord
{
public DoubledCoord(int col, int row)
{
this.col = col;
this.row = row;
}
public readonly int col;
public readonly int row;
static public DoubledCoord QdoubledFromCube(Hex h)
{
int col = h.q;
int row = 2 * h.r + h.q;
return new DoubledCoord(col, row);
}
public Hex QdoubledToCube()
{
int q = col;
int r = (int)((row - col) / 2);
int s = -q - r;
return new Hex(q, r, s);
}
static public DoubledCoord RdoubledFromCube(Hex h)
{
int col = 2 * h.q + h.r;
int row = h.r;
return new DoubledCoord(col, row);
}
public Hex RdoubledToCube()
{
int q = (int)((col - row) / 2);
int r = row;
int s = -q - r;
return new Hex(q, r, s);
}
}
public struct Orientation
{
public Orientation(double f0, double f1, double f2, double f3, double b0, double b1, double b2, double b3, double start_angle)
{
this.f0 = f0;
this.f1 = f1;
this.f2 = f2;
this.f3 = f3;
this.b0 = b0;
this.b1 = b1;
this.b2 = b2;
this.b3 = b3;
this.start_angle = start_angle;
}
public readonly double f0;
public readonly double f1;
public readonly double f2;
public readonly double f3;
public readonly double b0;
public readonly double b1;
public readonly double b2;
public readonly double b3;
public readonly double start_angle;
}
public struct Layout
{
public Layout(Orientation orientation, Point size, Point origin)
{
this.orientation = orientation;
this.size = size;
this.origin = origin;
}
public readonly Orientation orientation;
public readonly Point size;
public readonly Point origin;
static public Orientation pointy = new Orientation(Math.Sqrt(3.0), Math.Sqrt(3.0) / 2.0, 0.0, 3.0 / 2.0, Math.Sqrt(3.0) / 3.0, -1.0 / 3.0, 0.0, 2.0 / 3.0, 0.5);
static public Orientation flat = new Orientation(3.0 / 2.0, 0.0, Math.Sqrt(3.0) / 2.0, Math.Sqrt(3.0), 2.0 / 3.0, 0.0, -1.0 / 3.0, Math.Sqrt(3.0) / 3.0, 0.0);
public Point HexToPixel(Hex h)
{
Orientation M = orientation;
double x = (M.f0 * h.q + M.f1 * h.r) * size.x;
double y = (M.f2 * h.q + M.f3 * h.r) * size.y;
return new Point(x + origin.x, y + origin.y);
}
public FractionalHex PixelToHex(Point p)
{
Orientation M = orientation;
Point pt = new Point((p.x - origin.x) / size.x, (p.y - origin.y) / size.y);
double q = M.b0 * pt.x + M.b1 * pt.y;
double r = M.b2 * pt.x + M.b3 * pt.y;
return new FractionalHex(q, r, -q - r);
}
public Point HexCornerOffset(int corner)
{
Orientation M = orientation;
double angle = 2.0 * Math.PI * (M.start_angle - corner) / 6.0;
return new Point(size.x * Math.Cos(angle), size.y * Math.Sin(angle));
}
public List<Point> PolygonCorners(Hex h)
{
List<Point> corners = new List<Point> { };
Point center = HexToPixel(h);
for (int i = 0; i < 6; i++)
{
Point offset = HexCornerOffset(i);
corners.Add(new Point(center.x + offset.x, center.y + offset.y));
}
return corners;
}
}
// Tests
struct Tests
{
static public void EqualHex(String name, Hex a, Hex b)
{
if (!(a.q == b.q && a.s == b.s && a.r == b.r))
{
Tests.Complain(name);
}
}
static public void EqualOffsetcoord(String name, OffsetCoord a, OffsetCoord b)
{
if (!(a.col == b.col && a.row == b.row))
{
Tests.Complain(name);
}
}
static public void EqualDoubledcoord(String name, DoubledCoord a, DoubledCoord b)
{
if (!(a.col == b.col && a.row == b.row))
{
Tests.Complain(name);
}
}
static public void EqualInt(String name, int a, int b)
{
if (!(a == b))
{
Tests.Complain(name);
}
}
static public void EqualHexArray(String name, List<Hex> a, List<Hex> b)
{
Tests.EqualInt(name, a.Count, b.Count);
for (int i = 0; i < a.Count; i++)
{
Tests.EqualHex(name, a[i], b[i]);
}
}
static public void TestHexArithmetic()
{
Tests.EqualHex("hex_add", new Hex(4, -10, 6), new Hex(1, -3, 2).Add(new Hex(3, -7, 4)));
Tests.EqualHex("hex_subtract", new Hex(-2, 4, -2), new Hex(1, -3, 2).Subtract(new Hex(3, -7, 4)));
}
static public void TestHexDirection()
{
Tests.EqualHex("hex_direction", new Hex(0, -1, 1), Hex.Direction(2));
}
static public void TestHexNeighbor()
{
Tests.EqualHex("hex_neighbor", new Hex(1, -3, 2), new Hex(1, -2, 1).Neighbor(2));
}
static public void TestHexDiagonal()
{
Tests.EqualHex("hex_diagonal", new Hex(-1, -1, 2), new Hex(1, -2, 1).DiagonalNeighbor(3));
}
static public void TestHexDistance()
{
Tests.EqualInt("hex_distance", 7, new Hex(3, -7, 4).Distance(new Hex(0, 0, 0)));
}
static public void TestHexRotateRight()
{
Tests.EqualHex("hex_rotate_right", new Hex(1, -3, 2).RotateRight(), new Hex(3, -2, -1));
}
static public void TestHexRotateLeft()
{
Tests.EqualHex("hex_rotate_left", new Hex(1, -3, 2).RotateLeft(), new Hex(-2, -1, 3));
}
static public void TestHexRound()
{
FractionalHex a = new FractionalHex(0.0, 0.0, 0.0);
FractionalHex b = new FractionalHex(1.0, -1.0, 0.0);
FractionalHex c = new FractionalHex(0.0, -1.0, 1.0);
Tests.EqualHex("hex_round 1", new Hex(5, -10, 5), new FractionalHex(0.0, 0.0, 0.0).HexLerp(new FractionalHex(10.0, -20.0, 10.0), 0.5).HexRound());
Tests.EqualHex("hex_round 2", a.HexRound(), a.HexLerp(b, 0.499).HexRound());
Tests.EqualHex("hex_round 3", b.HexRound(), a.HexLerp(b, 0.501).HexRound());
Tests.EqualHex("hex_round 4", a.HexRound(), new FractionalHex(a.q * 0.4 + b.q * 0.3 + c.q * 0.3, a.r * 0.4 + b.r * 0.3 + c.r * 0.3, a.s * 0.4 + b.s * 0.3 + c.s * 0.3).HexRound());
Tests.EqualHex("hex_round 5", c.HexRound(), new FractionalHex(a.q * 0.3 + b.q * 0.3 + c.q * 0.4, a.r * 0.3 + b.r * 0.3 + c.r * 0.4, a.s * 0.3 + b.s * 0.3 + c.s * 0.4).HexRound());
}
static public void TestHexLinedraw()
{
Tests.EqualHexArray("hex_linedraw", new List<Hex> { new Hex(0, 0, 0), new Hex(0, -1, 1), new Hex(0, -2, 2), new Hex(1, -3, 2), new Hex(1, -4, 3), new Hex(1, -5, 4) }, FractionalHex.HexLinedraw(new Hex(0, 0, 0), new Hex(1, -5, 4)));
}
static public void TestLayout()
{
Hex h = new Hex(3, 4, -7);
Layout flat = new Layout(Layout.flat, new Point(10.0, 15.0), new Point(35.0, 71.0));
Tests.EqualHex("layout", h, flat.PixelToHex(flat.HexToPixel(h)).HexRound());
Layout pointy = new Layout(Layout.pointy, new Point(10.0, 15.0), new Point(35.0, 71.0));
Tests.EqualHex("layout", h, pointy.PixelToHex(pointy.HexToPixel(h)).HexRound());
}
static public void TestOffsetRoundtrip()
{
Hex a = new Hex(3, 4, -7);
OffsetCoord b = new OffsetCoord(1, -3);
Tests.EqualHex("conversion_roundtrip even-q", a, OffsetCoord.QoffsetToCube(OffsetCoord.EVEN, OffsetCoord.QoffsetFromCube(OffsetCoord.EVEN, a)));
Tests.EqualOffsetcoord("conversion_roundtrip even-q", b, OffsetCoord.QoffsetFromCube(OffsetCoord.EVEN, OffsetCoord.QoffsetToCube(OffsetCoord.EVEN, b)));
Tests.EqualHex("conversion_roundtrip odd-q", a, OffsetCoord.QoffsetToCube(OffsetCoord.ODD, OffsetCoord.QoffsetFromCube(OffsetCoord.ODD, a)));
Tests.EqualOffsetcoord("conversion_roundtrip odd-q", b, OffsetCoord.QoffsetFromCube(OffsetCoord.ODD, OffsetCoord.QoffsetToCube(OffsetCoord.ODD, b)));
Tests.EqualHex("conversion_roundtrip even-r", a, OffsetCoord.RoffsetToCube(OffsetCoord.EVEN, OffsetCoord.RoffsetFromCube(OffsetCoord.EVEN, a)));
Tests.EqualOffsetcoord("conversion_roundtrip even-r", b, OffsetCoord.RoffsetFromCube(OffsetCoord.EVEN, OffsetCoord.RoffsetToCube(OffsetCoord.EVEN, b)));
Tests.EqualHex("conversion_roundtrip odd-r", a, OffsetCoord.RoffsetToCube(OffsetCoord.ODD, OffsetCoord.RoffsetFromCube(OffsetCoord.ODD, a)));
Tests.EqualOffsetcoord("conversion_roundtrip odd-r", b, OffsetCoord.RoffsetFromCube(OffsetCoord.ODD, OffsetCoord.RoffsetToCube(OffsetCoord.ODD, b)));
}
static public void TestOffsetFromCube()
{
Tests.EqualOffsetcoord("offset_from_cube even-q", new OffsetCoord(1, 3), OffsetCoord.QoffsetFromCube(OffsetCoord.EVEN, new Hex(1, 2, -3)));
Tests.EqualOffsetcoord("offset_from_cube odd-q", new OffsetCoord(1, 2), OffsetCoord.QoffsetFromCube(OffsetCoord.ODD, new Hex(1, 2, -3)));
}
static public void TestOffsetToCube()
{
Tests.EqualHex("offset_to_cube even-", new Hex(1, 2, -3), OffsetCoord.QoffsetToCube(OffsetCoord.EVEN, new OffsetCoord(1, 3)));
Tests.EqualHex("offset_to_cube odd-q", new Hex(1, 2, -3), OffsetCoord.QoffsetToCube(OffsetCoord.ODD, new OffsetCoord(1, 2)));
}
static public void TestDoubledRoundtrip()
{
Hex a = new Hex(3, 4, -7);
DoubledCoord b = new DoubledCoord(1, -3);
Tests.EqualHex("conversion_roundtrip doubled-q", a, DoubledCoord.QdoubledFromCube(a).QdoubledToCube());
Tests.EqualDoubledcoord("conversion_roundtrip doubled-q", b, DoubledCoord.QdoubledFromCube(b.QdoubledToCube()));
Tests.EqualHex("conversion_roundtrip doubled-r", a, DoubledCoord.RdoubledFromCube(a).RdoubledToCube());
Tests.EqualDoubledcoord("conversion_roundtrip doubled-r", b, DoubledCoord.RdoubledFromCube(b.RdoubledToCube()));
}
static public void TestDoubledFromCube()
{
Tests.EqualDoubledcoord("doubled_from_cube doubled-q", new DoubledCoord(1, 5), DoubledCoord.QdoubledFromCube(new Hex(1, 2, -3)));
Tests.EqualDoubledcoord("doubled_from_cube doubled-r", new DoubledCoord(4, 2), DoubledCoord.RdoubledFromCube(new Hex(1, 2, -3)));
}
static public void TestDoubledToCube()
{
Tests.EqualHex("doubled_to_cube doubled-q", new Hex(1, 2, -3), new DoubledCoord(1, 5).QdoubledToCube());
Tests.EqualHex("doubled_to_cube doubled-r", new Hex(1, 2, -3), new DoubledCoord(4, 2).RdoubledToCube());
}
static public void TestAll()
{
Tests.TestHexArithmetic();
Tests.TestHexDirection();
Tests.TestHexNeighbor();
Tests.TestHexDiagonal();
Tests.TestHexDistance();
Tests.TestHexRotateRight();
Tests.TestHexRotateLeft();
Tests.TestHexRound();
Tests.TestHexLinedraw();
Tests.TestLayout();
Tests.TestOffsetRoundtrip();
Tests.TestOffsetFromCube();
Tests.TestOffsetToCube();
Tests.TestDoubledRoundtrip();
Tests.TestDoubledFromCube();
Tests.TestDoubledToCube();
}
static public void Main()
{
Tests.TestAll();
}
static public void Complain(String name)
{
Console.WriteLine("FAIL " + name);
}
}