forked from apaleyes/penrose-tiling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
penrose-bundle.js
871 lines (861 loc) · 28.6 KB
/
penrose-bundle.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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
var penroseFillPolygon = (() => {
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// penrose-fill-polygon.js
var penrose_fill_polygon_exports = {};
__export(penrose_fill_polygon_exports, {
Rhombus: () => Rhombus,
Triangle: () => Triangle,
TriangleC: () => TriangleC,
TriangleD: () => TriangleD,
TriangleX: () => TriangleX,
TriangleY: () => TriangleY,
Vector: () => Vector,
average_vectors: () => average_vectors,
calculateBaseRhombuses: () => calculateBaseRhombuses,
calculatePenroseTiling: () => calculatePenroseTiling,
calculateRhombusesBB: () => calculateRhombusesBB,
calculateTrianglesBB: () => calculateTrianglesBB,
interpolate_vectors: () => interpolate_vectors,
rhomb_key: () => rhomb_key,
scaleVector: () => scaleVector,
tatham_neighbor: () => tatham_neighbor,
tatham_neighbor_or_null: () => tatham_neighbor_or_null,
triangleListsIntersect: () => triangleListsIntersect,
trianglesIntersect: () => trianglesIntersect,
truncate_float: () => truncate_float
});
// node_modules/d3-array/src/cross.js
function length(array) {
return array.length | 0;
}
function empty(length2) {
return !(length2 > 0);
}
function arrayify(values) {
return typeof values !== "object" || "length" in values ? values : Array.from(values);
}
function reducer(reduce) {
return (values) => reduce(...values);
}
function cross(...values) {
const reduce = typeof values[values.length - 1] === "function" && reducer(values.pop());
values = values.map(arrayify);
const lengths = values.map(length);
const j = values.length - 1;
const index = new Array(j + 1).fill(0);
const product = [];
if (j < 0 || lengths.some(empty))
return product;
while (true) {
product.push(index.map((j2, i2) => values[i2][j2]));
let i = j;
while (++index[i] === lengths[i]) {
if (i === 0)
return reduce ? product.map(reduce) : product;
index[i--] = 0;
}
}
}
// node_modules/d3-array/src/extent.js
function extent(values, valueof) {
let min2;
let max2;
if (valueof === void 0) {
for (const value of values) {
if (value != null) {
if (min2 === void 0) {
if (value >= value)
min2 = max2 = value;
} else {
if (min2 > value)
min2 = value;
if (max2 < value)
max2 = value;
}
}
}
} else {
let index = -1;
for (let value of values) {
if ((value = valueof(value, ++index, values)) != null) {
if (min2 === void 0) {
if (value >= value)
min2 = max2 = value;
} else {
if (min2 > value)
min2 = value;
if (max2 < value)
max2 = value;
}
}
}
}
return [min2, max2];
}
// node_modules/d3-array/src/max.js
function max(values, valueof) {
let max2;
if (valueof === void 0) {
for (const value of values) {
if (value != null && (max2 < value || max2 === void 0 && value >= value)) {
max2 = value;
}
}
} else {
let index = -1;
for (let value of values) {
if ((value = valueof(value, ++index, values)) != null && (max2 < value || max2 === void 0 && value >= value)) {
max2 = value;
}
}
}
return max2;
}
// node_modules/d3-array/src/min.js
function min(values, valueof) {
let min2;
if (valueof === void 0) {
for (const value of values) {
if (value != null && (min2 > value || min2 === void 0 && value >= value)) {
min2 = value;
}
}
} else {
let index = -1;
for (let value of values) {
if ((value = valueof(value, ++index, values)) != null && (min2 > value || min2 === void 0 && value >= value)) {
min2 = value;
}
}
}
return min2;
}
// node_modules/d3-array/src/mean.js
function mean(values, valueof) {
let count = 0;
let sum = 0;
if (valueof === void 0) {
for (let value of values) {
if (value != null && (value = +value) >= value) {
++count, sum += value;
}
}
} else {
let index = -1;
for (let value of values) {
if ((value = valueof(value, ++index, values)) != null && (value = +value) >= value) {
++count, sum += value;
}
}
}
if (count)
return sum / count;
}
// node_modules/d3-array/src/range.js
function range(start, stop, step) {
start = +start, stop = +stop, step = (n = arguments.length) < 2 ? (stop = start, start = 0, 1) : n < 3 ? 1 : +step;
var i = -1, n = Math.max(0, Math.ceil((stop - start) / step)) | 0, range2 = new Array(n);
while (++i < n) {
range2[i] = start + i * step;
}
return range2;
}
// node_modules/d3-random/src/defaultSource.js
var defaultSource_default = Math.random;
// node_modules/d3-random/src/uniform.js
var uniform_default = function sourceRandomUniform(source) {
function randomUniform(min2, max2) {
min2 = min2 == null ? 0 : +min2;
max2 = max2 == null ? 1 : +max2;
if (arguments.length === 1)
max2 = min2, min2 = 0;
else
max2 -= min2;
return function() {
return source() * max2 + min2;
};
}
randomUniform.source = sourceRandomUniform;
return randomUniform;
}(defaultSource_default);
// penrose-fill-polygon.js
var GOLDEN_RATIO = 0.6180339887498949;
var cross2 = function(A, B) {
var a1 = A.v1;
var a2 = A.v2;
var a3 = A.v3;
var b1 = B.v1;
var b2 = B.v2;
var b3 = B.v3;
var dXa = a1.x - b3.x;
var dYa = a1.y - b3.y;
var dXb = a2.x - b3.x;
var dYb = a2.y - b3.y;
var dXc = a3.x - b3.x;
var dYc = a3.y - b3.y;
var dX21 = b3.x - b2.x;
var dY12 = b2.y - b3.y;
var D = dY12 * (b1.x - b3.x) + dX21 * (b1.y - b3.y);
var sa = dY12 * dXa + dX21 * dYa;
var sb = dY12 * dXb + dX21 * dYb;
var sc = dY12 * dXc + dX21 * dYc;
var ta = (b3.y - b1.y) * dXa + (b1.x - b3.x) * dYa;
var tb = (b3.y - b1.y) * dXb + (b1.x - b3.x) * dYb;
var tc = (b3.y - b1.y) * dXc + (b1.x - b3.x) * dYc;
if (D < 0)
return sa >= 0 && sb >= 0 && sc >= 0 || ta >= 0 && tb >= 0 && tc >= 0 || sa + ta <= D && sb + tb <= D && sc + tc <= D;
return sa <= 0 && sb <= 0 && sc <= 0 || ta <= 0 && tb <= 0 && tc <= 0 || sa + ta >= D && sb + tb >= D && sc + tc >= D;
};
var trianglesIntersect = function(A, B) {
return !(cross2(A, B) || cross2(B, A));
};
var triangleListsIntersect = function(As, Bs) {
return cross(As, Bs).some(([A, B]) => trianglesIntersect(A, B));
};
var Vector = class _Vector {
constructor(x, y) {
this.x = x;
this.y = y;
}
static fromPoints(start, end) {
return new _Vector(end.x - start.x, end.y - start.y);
}
static fromJson(json) {
return new _Vector(json.x, json.y);
}
print(xform, yform, prec = 4) {
xform = xform || ((x) => x);
yform = yform || ((y) => y);
return `${xform(this.x).toFixed(prec)}, ${yform(this.y).toFixed(prec)}`;
}
multiply(multiplier) {
return new _Vector(this.x * multiplier, this.y * multiplier);
}
add(anotherVector) {
return new _Vector(this.x + anotherVector.x, this.y + anotherVector.y);
}
subtract(anotherVector) {
return new _Vector(this.x - anotherVector.x, this.y - anotherVector.y);
}
};
function average_vectors(...vs) {
return new Vector(
d3.sum(vs, ({ x }) => x) / vs.length,
d3.sum(vs, ({ y }) => y) / vs.length
);
}
function interpolate_vectors(a, b, t) {
return new Vector(
d3.interpolateNumber(a.x, b.x)(t),
d3.interpolateNumber(a.y, b.y)(t)
);
}
function sign(v1, v2, v3) {
return (v1.x - v3.x) * (v2.y - v3.y) - (v2.x - v3.x) * (v1.y - v3.y);
}
var Triangle = class {
constructor(v1, v2, v3, coord, fillColor) {
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
this.fillColor = fillColor;
this.coord = coord;
}
pointInside(pt) {
const d1 = sign(pt, this.v1, this.v2), d2 = sign(pt, this.v2, this.v3), d32 = sign(pt, this.v3, this.v1);
const has_neg = d1 < 0 || d2 < 0 || d32 < 0;
const has_pos = d1 > 0 || d2 > 0 || d32 > 0;
return !(has_neg && has_pos);
}
center() {
return average_vectors(this.v1, this.v2, this.v3);
}
side(i) {
return i === 0 ? [this.v2, this.v3] : i === 1 ? [this.v1, this.v2] : i === 2 ? [this.v3, this.v1] : null;
}
};
var TriangleC = class _TriangleC extends Triangle {
constructor(v1, v2, v3, coord) {
super(v1, v2, v3, coord, "blue");
}
split() {
var vector_12 = Vector.fromPoints(this.v1, this.v2).multiply(GOLDEN_RATIO);
var split_point_12 = this.v1.add(vector_12);
var new_triangles = [];
new_triangles.push(new _TriangleC(this.v3, split_point_12, this.v2, "C" + this.coord));
new_triangles.push(new TriangleY(split_point_12, this.v3, this.v1, "Y" + this.coord));
return new_triangles;
}
};
var TriangleD = class _TriangleD extends Triangle {
constructor(v1, v2, v3, coord) {
super(v1, v2, v3, coord, "blue");
}
split() {
var vector_13 = Vector.fromPoints(this.v1, this.v3).multiply(GOLDEN_RATIO);
var split_point_13 = this.v1.add(vector_13);
var new_triangles = [];
new_triangles.push(new _TriangleD(this.v2, this.v3, split_point_13, "D" + this.coord));
new_triangles.push(new TriangleX(split_point_13, this.v1, this.v2, "X" + this.coord));
return new_triangles;
}
};
var TriangleX = class _TriangleX extends Triangle {
constructor(v1, v2, v3, coord) {
super(v1, v2, v3, coord, "red");
}
split() {
var vector_32 = Vector.fromPoints(this.v3, this.v2).multiply(GOLDEN_RATIO);
var split_point_32 = this.v3.add(vector_32);
var vector_31 = Vector.fromPoints(this.v3, this.v1).multiply(GOLDEN_RATIO);
var split_point_31 = this.v3.add(vector_31);
var new_triangles = [];
new_triangles.push(new TriangleY(split_point_31, split_point_32, this.v3, "Y" + this.coord));
new_triangles.push(new TriangleC(split_point_32, split_point_31, this.v1, "C" + this.coord));
new_triangles.push(new _TriangleX(split_point_32, this.v1, this.v2, "X" + this.coord));
return new_triangles;
}
};
var TriangleY = class _TriangleY extends Triangle {
constructor(v1, v2, v3, coord) {
super(v1, v2, v3, coord, "red");
}
split() {
var vector_21 = Vector.fromPoints(this.v2, this.v1).multiply(GOLDEN_RATIO);
var split_point_21 = this.v2.add(vector_21);
var vector_23 = Vector.fromPoints(this.v2, this.v3).multiply(GOLDEN_RATIO);
var split_point_23 = this.v2.add(vector_23);
var new_triangles = [];
new_triangles.push(new _TriangleY(split_point_23, this.v3, this.v1, "Y" + this.coord));
new_triangles.push(new TriangleD(split_point_23, this.v1, split_point_21, "D" + this.coord));
new_triangles.push(new TriangleX(split_point_21, this.v2, split_point_23, "X" + this.coord));
return new_triangles;
}
};
var Rhombus = class _Rhombus {
constructor(v1, v2, v3, v4, coord, fillColor) {
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
this.v4 = v4;
this.fillColor = fillColor;
this.coord = coord;
}
getTriangles() {
return [
new Triangle(this.v1, this.v2, this.v3),
new Triangle(this.v3, this.v4, this.v1)
];
}
getPoints() {
return [this.v1, this.v2, this.v3, this.v4];
}
side(i) {
return i === 0 ? [this.v1, this.v2] : i === 1 ? [this.v2, this.v3] : i === 2 ? [this.v3, this.v4] : i === 3 ? [this.v4, this.v1] : null;
}
static fromJson(json) {
const { v1, v2, v3, v4, coord, fillColor } = json;
return new _Rhombus(Vector.fromJson(v1), Vector.fromJson(v2), Vector.fromJson(v3), Vector.fromJson(v4), coord, fillColor);
}
};
var rtri_neighbors = {
CC: {
0: { external: true, side: 1, hand: "r" },
1: { prefix: "Y", enter: 1 },
2: { external: true, side: 0 }
},
YC: {
0: { external: true, side: 2 },
1: { prefix: "C", enter: 1 },
2: { external: true, side: 1, hand: "l" }
},
XD: {
0: { external: true, side: 1 },
1: { external: true, side: 2, hand: "r" },
2: { prefix: "D", enter: 2 }
},
DD: {
0: { external: true, side: 2, hand: "l" },
1: { external: true, side: 0 },
2: { prefix: "X", enter: 2 }
},
YX: {
0: { external: true, side: 0, hand: "r" },
1: { prefix: "C", enter: 1 },
2: { external: true, side: 2, hand: "l" }
},
CX: {
0: { external: true, side: 2, hand: "r" },
1: { prefix: "Y", enter: 1 },
2: { prefix: "X", enter: 1 }
},
XX: {
0: { external: true, side: 1 },
1: { prefix: "C", enter: 2 },
2: { external: true, side: 0, hand: "l" }
},
YY: {
0: { external: true, side: 2 },
1: { external: true, side: 0, hand: "r" },
2: { prefix: "D", enter: 1 }
},
DY: {
0: { external: true, side: 1, hand: "l" },
1: { prefix: "Y", enter: 2 },
2: { prefix: "X", enter: 2 }
},
XY: {
0: { external: true, side: 0, hand: "l" },
1: { external: true, side: 1, hand: "r" },
2: { prefix: "D", enter: 2 }
}
};
var other_hand = {
l: "r",
r: "l"
};
var rtri_entries = {
C: {
0: { w: { part: "C", side: 2 } },
1: {
l: { part: "Y", side: 2 },
r: { part: "C", side: 0 }
},
2: { w: { part: "Y", side: 0 } }
},
D: {
0: { w: { part: "D", side: 1 } },
1: { w: { part: "X", side: 0 } },
2: {
l: { part: "D", side: 0 },
r: { part: "X", side: 1 }
}
},
X: {
0: {
l: { part: "X", side: 2 },
r: { part: "Y", side: 0 }
},
1: { w: { part: "X", side: 0 } },
2: {
l: { part: "Y", side: 2 },
r: { part: "C", side: 0 }
}
},
Y: {
0: {
l: { part: "X", side: 0 },
r: { part: "Y", side: 1 }
},
1: {
l: { part: "D", side: 0 },
r: { part: "X", side: 1 }
},
2: { w: { part: "Y", side: 0 } }
}
};
function tatham_neighbor(coord, side) {
if (coord.length < 2)
throw new Error("no neighbor");
const pre2 = coord.slice(0, 2);
const neighbors = rtri_neighbors[pre2];
console.assert(neighbors, "unknown prefix", pre2);
const nei = neighbors[side];
var result;
if (nei.external) {
console.assert(nei.side !== void 0);
const [parent, pside] = tatham_neighbor(coord.slice(1), nei.side);
const enter = rtri_entries[parent[0]][pside];
let part, side2;
if (nei.hand) {
console.assert(enter.l);
({ part, side: side2 } = enter[other_hand[nei.hand]]);
} else {
console.assert(enter.w);
({ part, side: side2 } = enter.w);
}
return [part + parent, side2];
} else {
return [nei.prefix + coord.slice(1), nei.enter];
}
}
function tatham_neighbor_or_null(coord, side) {
try {
return tatham_neighbor(coord, side)[0];
} catch (xep) {
console.warn("no neighbor", side, "for", coord);
return null;
}
}
var shape_spec = {
square: {
sides: 4,
offset: 0.5
},
pentagon: {
sides: 5,
offset: -0.25
},
hexagon: {
sides: 6
}
};
function regularPolygon(center, r, shape) {
const { sides, offset } = shape_spec[shape];
const thetas = range(offset || 0, sides, 1).map((v) => v * 2 * Math.PI / sides);
return thetas.map((theta) => new Vector(Math.cos(theta) * r + center.x, Math.sin(theta) * r + center.y));
}
function triangulate(polygon) {
return range(2, polygon.length).map((i) => new Triangle(polygon[0], polygon[i - 1], polygon[i], "N/A", "green"));
}
function generateTriangles(triangles, filt, enough) {
const discarded = [];
do {
var new_triangles = [];
for (var i = 0; i < triangles.length; i++) {
var trig = triangles[i];
new_triangles = new_triangles.concat(trig.split());
}
triangles = new_triangles.filter((tri) => {
if (filt(tri))
return true;
discarded.push(tri);
return false;
});
} while (triangles.length && !enough(triangles));
return [triangles, discarded];
}
function lighten(color) {
switch (color) {
case "blue":
return "lightblue";
case "red":
return "pink";
}
console.log("unknown color to lighten", color);
return color;
}
function calculateBaseRhombuses() {
const TAU = 2 * Math.PI;
const cos36_2 = Math.cos(TAU / 10) / 2, sin36_2 = Math.sin(TAU / 10) / 2;
const cos72_2 = Math.cos(TAU / 5) / 2, sin72_2 = Math.sin(TAU / 5) / 2;
const rhomb0 = [
new Vector(0.5 - cos72_2, -sin72_2),
new Vector(0.5 + cos72_2, sin72_2),
new Vector(cos72_2 - 0.5, sin72_2),
new Vector(-0.5 - cos72_2, -sin72_2)
];
const rhomb9 = [
new Vector(0.5 + cos36_2, sin36_2),
new Vector(cos36_2 - 0.5, sin36_2),
new Vector(-0.5 - cos36_2, -sin36_2),
new Vector(0.5 - cos36_2, -sin36_2)
];
const rots = [
0,
TAU / 5,
TAU * 2 / 5,
TAU * 3 / 5,
TAU * 4 / 5,
TAU * 2 / 10,
-TAU / 10,
-TAU * 4 / 10,
TAU * 3 / 10,
0
];
return range(20).map((i) => {
const rhomb = i % 10 < 5 ? rhomb0 : rhomb9;
const rot = i < 10 ? rots[i] : rots[i - 10] + TAU / 2;
const rv = rhomb.map(({ x, y }) => new Vector(
x * Math.cos(rot) - y * Math.sin(rot),
-(x * Math.sin(rot) + y * Math.cos(rot))
));
return [rv[0], rv[3], rv[2], rv[1]];
});
}
var base_rhombuses = calculateBaseRhombuses();
var truncate_float = (prec) => (x) => Math.abs(x) < 10 ** -prec ? 0 .toFixed(prec) : x.toFixed(prec);
function rhomb_key(vs, prec = 10) {
if (vs instanceof Rhombus)
vs = [vs.v1, vs.v2, vs.v3, vs.v4];
const trunc = truncate_float(prec);
return vs.flatMap((v) => [trunc(v.x), trunc(v.y)]).join(",");
}
function calculateTrianglesBB(tris) {
const tl = new Vector(
min(tris, (tri) => min([tri.v1.x, tri.v2.x, tri.v3.x])),
min(tris, (tri) => min([tri.v1.y, tri.v2.y, tri.v3.y]))
);
const br = new Vector(
max(tris, (tri) => max([tri.v1.x, tri.v2.x, tri.v3.x])),
max(tris, (tri) => max([tri.v1.y, tri.v2.y, tri.v3.y]))
);
return { tl, br };
}
function calculateRhombusesBB(rhombs) {
const tl = new Vector(
min(rhombs, (rhomb) => min([rhomb.v1.x, rhomb.v2.x, rhomb.v3.x, rhomb.v4.x])),
min(rhombs, (rhomb) => min([rhomb.v1.y, rhomb.v2.y, rhomb.v3.y, rhomb.v4.y]))
);
const br = new Vector(
max(rhombs, (rhomb) => max([rhomb.v1.x, rhomb.v2.x, rhomb.v3.x, rhomb.v4.x])),
max(rhombs, (rhomb) => max([rhomb.v1.y, rhomb.v2.y, rhomb.v3.y, rhomb.v4.y]))
);
return { tl, br };
}
function scaleVector(tl, scale) {
return (v) => {
return new Vector(
(v.x - tl.x) * scale,
(v.y - tl.y) * scale
);
};
}
function calculatePenroseTiling(minTiles, width, height, boundsShape, startTile, resolveRagged, center, r) {
var ratio = Math.sin(36 * (Math.PI / 180)) / Math.sin(54 * (Math.PI / 180));
var startri = null, hei;
switch (startTile) {
case "C":
hei = Math.min(width * ratio, height);
startri = new TriangleC(new Vector(width / 2 - hei / 2 / ratio, hei / 2), new Vector(width / 2 + hei / 2 / ratio, 0), new Vector(width / 2 + hei / 2 / ratio, hei), startTile);
break;
case "D":
hei = Math.min(width * ratio, height);
startri = new TriangleD(new Vector(width / 2 - hei / 2 / ratio, hei / 2), new Vector(width / 2 + hei / 2 / ratio, 0), new Vector(width / 2 + hei / 2 / ratio, hei), startTile);
break;
case "X":
hei = Math.min(width / 2 * ratio, height);
startri = new TriangleX(new Vector(width / 2, 0), new Vector(width / 2 + hei / ratio, hei), new Vector(width / 2 - hei / ratio, hei), startTile);
break;
case "Y":
hei = Math.min(width / 2 * ratio, height);
startri = new TriangleY(new Vector(width / 2, 0), new Vector(width / 2 + hei / ratio, hei), new Vector(width / 2 - hei / ratio, hei), startTile);
break;
}
var triangles = [startri], polygon;
if (center && r)
polygon = regularPolygon(center, r, boundsShape);
else {
const [xmin, xmax] = extent([startri.v1.x, startri.v2.x, startri.v3.x]);
const [ymin, ymax] = extent([startri.v1.y, startri.v2.y, startri.v3.y]);
r = uniform_default(width / 1e3, width / 8)();
let r_tries = 5, found = false;
do {
let xrand = uniform_default(xmin + r, xmax - r), yrand = uniform_default(ymin + r, ymax - r);
let c_tries = 10;
do {
center = new Vector(xrand(), yrand());
polygon = regularPolygon(center, r, boundsShape);
found = polygon.every((pt) => startri.pointInside(pt));
} while (--c_tries && !found);
if (!found)
r /= 2;
} while (--r_tries && !found);
if (!r_tries) {
console.log("couldn't find polygon of radius", r, "inside", startri.v1.print(), startri.v2.print(), startri.v3.print());
throw new Error("Couldn't find polygon inside triangle");
}
}
const polyTris = triangulate(polygon);
console.assert(!isNaN(minTiles));
var discarded;
[triangles, discarded] = generateTriangles(
triangles,
(tri) => polyTris.some((ptri) => trianglesIntersect(ptri, tri)),
(tris) => tris.length / 2 > minTiles
);
const trihash = {};
for (var t of triangles)
trihash[t.coord] = t;
const disind = [];
const find_tris = [];
for (var [i, t] of triangles.entries()) {
var oh = tatham_neighbor_or_null(t.coord, 0);
var t2;
if (!oh || !(t2 = trihash[oh])) {
if (resolveRagged === "cull")
disind.push(i);
else if (resolveRagged === "fill") {
var nei1 = tatham_neighbor_or_null(t.coord, 1), nei2 = tatham_neighbor_or_null(t.coord, 2);
if (oh && nei1 && nei2 && trihash[nei1] && trihash[nei2])
find_tris.push(oh);
else
disind.push(i);
}
}
}
var found_tris = [];
if (find_tris.length) {
[found_tris] = generateTriangles(
[startri],
(tri) => find_tris.some((find) => find.endsWith(tri.coord)),
(tris) => !tris.length || tris[0].coord.length === find_tris[0].length
);
if (found_tris.length < find_tris.length) {
console.log("did not find other halves of all sought triangles:");
console.log("sought", find_tris);
console.log("found", found_tris.map(({ coord }) => coord));
}
for (const tri of found_tris)
trihash[tri.coord] = tri;
triangles.push(...found_tris);
}
const rhombhash = {};
const tri2rhomb = {};
for (var [i, t] of triangles.entries()) {
var oh = tatham_neighbor_or_null(t.coord, 0);
var t2;
if (oh && (t2 = trihash[oh])) {
const rhombcoord = [t.coord, oh].sort().join(",");
if (rhombhash[rhombcoord])
continue;
else {
tri2rhomb[t.coord] = rhombcoord;
tri2rhomb[oh] = rhombcoord;
const fillColor = find_tris.includes(t.coord) || find_tris.includes(oh) ? lighten(t.fillColor) : t.fillColor;
const rhombus = new Rhombus(t.v1, t.v2, t2.v1, t2.v2, rhombcoord, fillColor);
rhombhash[rhombcoord] = {
rhombus,
tri1: t,
tri2: t2
};
}
}
}
const culledTris = [];
for (i = disind.length - 1; i >= 0; i--) {
culledTris.push(triangles[disind[i]]);
triangles.splice(disind[i], 1);
}
for (const [rhombcoord, { tri1, tri2, rhombus }] of Object.entries(rhombhash)) {
const neighbors = [];
var j = 0;
for (const tri of [tri1, tri2])
for (const side of [1, 2]) {
var nei = tatham_neighbor_or_null(tri.coord, side);
const rhombnei = nei && tri2rhomb[nei] || null;
neighbors.push(rhombnei);
}
rhombhash[rhombcoord].neighbors = neighbors;
}
const culledRhombs = [];
if (resolveRagged === "cull") {
var cullRhombs;
do {
cullRhombs = Object.values(rhombhash).filter(({ neighbors }) => neighbors.filter((n) => n).length < 2);
for (const { rhombus, neighbors } of cullRhombs) {
culledRhombs.push(rhombus);
for (nei of neighbors) {
if (!nei)
continue;
const entry = rhombhash[nei];
for (const i2 of range(4)) {
if (entry.neighbors[i2] === rhombus.coord)
entry.neighbors[i2] = null;
}
}
delete rhombhash[rhombus.coord];
}
} while (cullRhombs.length);
}
discarded.concat(culledTris).forEach((tri) => tri.fillColor = "none");
var elengths = [];
for (const { rhombus: rh } of Object.values(rhombhash))
for (const [v1, v2] of [[rh.v1, rh.v2], [rh.v2, rh.v3], [rh.v3, rh.v4], [rh.v4, rh.v1]])
elengths.push(Math.hypot(v2.x - v1.x, v2.y - v1.y));
const meanEdgeLength = mean(elengths);
const { tl, br } = calculateRhombusesBB(Object.values(rhombhash).map(({ rhombus }) => rhombus));
const scale = scaleVector(tl, 1 / meanEdgeLength);
for (const rhombus of Object.values(rhombhash)) {
const { rhombus: rh, tri1, tri2 } = rhombus;
rh.v1 = scale(rh.v1);
rh.v2 = scale(rh.v2);
rh.v3 = scale(rh.v3);
rh.v4 = scale(rh.v4);
rhombus.tri1scale = new Triangle(
scale(tri1.v1),
scale(tri1.v2),
scale(tri1.v3),
tri1.coord
);
rhombus.tri2scale = new Triangle(
scale(tri2.v1),
scale(tri2.v2),
scale(tri2.v3),
tri2.coord
);
}
elengths = [];
for (const { rhombus: rh } of Object.values(rhombhash))
for (const [v1, v2] of [[rh.v1, rh.v2], [rh.v2, rh.v3], [rh.v3, rh.v4], [rh.v4, rh.v1]])
elengths.push(Math.hypot(v2.x - v1.x, v2.y - v1.y));
const rray = [];
for (const { rhombus: rh } of Object.values(rhombhash)) {
const cx = (rh.v1.x + rh.v3.x) / 2, cy = (rh.v1.y + rh.v3.y) / 2, cx2 = (rh.v2.x + rh.v4.x) / 2, cy2 = (rh.v2.y + rh.v4.y) / 2;
console.assert(Math.abs(cx - cx2) < 1);
console.assert(Math.abs(cy - cy2) < 1);
rhombhash[rh.coord].center = new Vector(cx, cy);
var vs = [
new Vector(rh.v1.x - cx, rh.v1.y - cy),
new Vector(rh.v2.x - cx, rh.v2.y - cy),
new Vector(rh.v3.x - cx, rh.v3.y - cy),
new Vector(rh.v4.x - cx, rh.v4.y - cy)
];
rhombhash[rh.coord].key = rhomb_key(vs);
}
const key_to_base = {};
const base_to_key = [];
for (const [i2, rh] of base_rhombuses.entries()) {
const key = rhomb_key(rh);
key_to_base[key] = i2;
base_to_key.push(key);
}
const not_found = /* @__PURE__ */ new Set(), bases_found = /* @__PURE__ */ new Set();
for (const rhombdef of Object.values(rhombhash)) {
const base = key_to_base[rhombdef.key];
if (base !== void 0) {
bases_found.add(base);
rhombdef.base = base;
} else {
not_found.add(rhombdef.key);
rhombdef.base = null;
}
}
for (const nf of not_found)
console.log("not found", nf);
for (const base of range(10))
if (!bases_found.has(base))
console.log("unused", base_to_key[base]);
return {
center,
r,
polygon,
robinsonTriangles: triangles,
discardedTriangles: discarded,
culledTriangles: culledTris,
p3Rhombuses: rhombhash,
culledRhombuses: culledRhombs,
fillsIdentified: find_tris,
fillsFound: found_tris,
rhombBases: range(10),
scaleFunction: scale
};
}
return __toCommonJS(penrose_fill_polygon_exports);
})();