1
import { expect, test } from "vitest";
9
} from "../../src/index";
11
test("fuses two rectangles", () => {
12
const d1 = drawRoundedRectangle(80, 80, 30);
13
const d2 = drawRoundedRectangle(10, 100);
14
expect(d1.fuse(d2).toSVG()).toMatchSVGSnapshot();
17
test("fuses two circles", () => {
18
const d1 = drawCircle(80).translate(-10, 0);
19
const d2 = drawCircle(60).translate(30, 0);
20
expect(d1.fuse(d2).toSVG()).toMatchSVGSnapshot();
23
test("cuts one rectangle from the other", () => {
24
const d1 = drawRoundedRectangle(80, 80, 30);
25
const d2 = drawRoundedRectangle(10, 100);
26
expect(d1.cut(d2).toSVG()).toMatchSVGSnapshot();
29
test("intersects two rectangles", () => {
30
const d1 = drawRoundedRectangle(80, 80, 30);
31
const d2 = drawRoundedRectangle(10, 100);
32
expect(d1.intersect(d2).toSVG()).toMatchSVGSnapshot();
35
test("fuse two rectangles with corners", () => {
36
const d1 = drawRoundedRectangle(80, 80, 30).rotate(20);
37
const d2 = drawRoundedRectangle(40, 100);
38
expect(d1.fuse(d2).toSVG()).toMatchSVGSnapshot();
41
test("cut two rectangles with corners", () => {
42
const d1 = drawRoundedRectangle(80, 80, 30).rotate(20);
43
const d2 = drawRoundedRectangle(40, 100);
44
expect(d1.cut(d2).toSVG()).toMatchSVGSnapshot();
47
test("handles the case when a compound is created from fusion", () => {
48
const d1 = draw([0, -50])
57
const d2 = drawRoundedRectangle(10, 120).translate(5, 0);
58
expect(d1.fuse(d2).toSVG()).toMatchSVGSnapshot();
61
test("cut such that a hole becomes the outside", () => {
62
const d1 = drawRoundedRectangle(80, 80);
63
const d2 = drawCircle(30);
64
const d3 = drawRoundedRectangle(40, 10).translate(20, 0);
65
expect(d1.cut(d2).cut(d3).toSVG()).toMatchSVGSnapshot();
68
const polarCopies = (shape: Drawing, count: number, radius: number) => {
69
const base = shape.translate(0, radius);
70
const angle = 360 / count;
72
const copies: Drawing[] = [];
73
for (let i = 0; i < count; i++) {
74
copies.push(base.rotate(i * angle));
79
const fuseAll = (shapes: Drawing[]) => {
80
let result = shapes[0];
81
shapes.slice(1).forEach((shape) => {
82
result = result.fuse(shape);
87
test.each([5, 11, 13])(
88
"handles a complex example of merging shapes with %d copies",
90
const shape = drawPolysides(6, 6);
91
const outlinedShape = shape.offset(0.5).cut(shape.offset(-0.5));
92
const copies = polarCopies(outlinedShape, n, 9);
94
const fused = fuseAll(copies);
96
expect(fused.toSVG()).toMatchSVGSnapshot();
100
const triangles: [Point2D, Point2D, Point2D][] = [
104
[14.999999999999998, -5.087131436281159],
108
[14.999999999999998, -5.087131436281159],
109
[-15.000000000000004, -5.087131436281141],
113
[-15.000000000000004, -5.087131436281141],
114
[-9.037735159573977, 6.763116311474526],
118
[-9.037735159573977, 6.763116311474526],
119
[1.865174681370263e-14, 15.261394308843476],
122
[-9.037735159573977, 6.763116311474526],
123
[-15.000000000000004, -5.087131436281141],
124
[-9.675180053141624, 7.189474166860152],
127
[-9.675180053141624, 7.189474166860152],
128
[-15.000000000000004, -5.087131436281141],
129
[-8.000000000000007, 25.435657181405805],
132
[-8.000000000000007, 25.435657181405805],
133
[-15.000000000000004, -5.087131436281141],
134
[-7.499999999999897, 35.60992005396811],
137
[-8.000000000000007, 25.435657181405805],
138
[-7.499999999999897, 35.60992005396811],
139
[-7.814835389770436, 27.176719368153215],
142
[-7.814835389770436, 27.176719368153215],
143
[-7.499999999999897, 35.60992005396811],
144
[-7.287644378911123, 28.79320082185884],
147
[-7.287644378911123, 28.79320082185884],
148
[-7.499999999999897, 35.60992005396811],
149
[-6.281519359620161, 30.47602793525325],
152
[-6.281519359620161, 30.47602793525325],
153
[-7.499999999999897, 35.60992005396811],
154
[-5.159435917248953, 31.656137011351642],
157
[-5.159435917248953, 31.656137011351642],
158
[-7.499999999999897, 35.60992005396811],
159
[-4.0166211429761844, 32.47480273276338],
162
[-4.0166211429761844, 32.47480273276338],
163
[-7.499999999999897, 35.60992005396811],
164
[-2.878254499509339, 33.03002377846391],
167
[-2.878254499509339, 33.03002377846391],
168
[-7.499999999999897, 35.60992005396811],
169
[-1.7485555329428917, 33.378267927827935],
172
[-1.7485555329428917, 33.378267927827935],
173
[-7.499999999999897, 35.60992005396811],
174
[-0.6254051035361321, 33.55015764894129],
177
[-0.6254051035361321, 33.55015764894129],
178
[-7.499999999999897, 35.60992005396811],
179
[0.49519544040220814, 33.559459272681295],
182
[0.49519544040220814, 33.559459272681295],
183
[-7.499999999999897, 35.60992005396811],
184
[7.500000000000104, 35.60992005396825],
187
[0.49519544040220814, 33.559459272681295],
188
[7.500000000000104, 35.60992005396825],
189
[1.6178253438012995, 33.40689452102694],
192
[1.6178253438012995, 33.40689452102694],
193
[7.500000000000104, 35.60992005396825],
194
[2.746624486362765, 33.08031785939673],
197
[2.746624486362765, 33.08031785939673],
198
[7.500000000000104, 35.60992005396825],
199
[3.8840547206139435, 32.55139719795343],
202
[3.8840547206139435, 32.55139719795343],
203
[7.500000000000104, 35.60992005396825],
204
[5.027051998596925, 31.767347767310195],
207
[5.027051998596925, 31.767347767310195],
208
[7.500000000000104, 35.60992005396825],
209
[6.031511836253476, 30.782797624973057],
212
[6.031511836253476, 30.782797624973057],
213
[7.500000000000104, 35.60992005396825],
214
[6.748780453713429, 29.806324687291763],
217
[6.748780453713429, 29.806324687291763],
218
[7.500000000000104, 35.60992005396825],
219
[7.3787323507642855, 28.580526519754564],
222
[7.3787323507642855, 28.580526519754564],
223
[7.500000000000104, 35.60992005396825],
224
[7.8259843512896605, 27.124084897826446],
227
[7.8259843512896605, 27.124084897826446],
228
[7.500000000000104, 35.60992005396825],
229
[11.250000000000288, 15.261394308843652],
232
[11.250000000000288, 15.261394308843652],
233
[1.865174681370263e-14, 15.261394308843476],
234
[2.9681946363630565, 17.877212423185448],
237
[11.250000000000288, 15.261394308843652],
238
[2.9681946363630565, 17.877212423185448],
239
[4.137024309826323, 18.469070410223146],
242
[11.250000000000288, 15.261394308843652],
243
[4.137024309826323, 18.469070410223146],
244
[5.230691228137659, 19.27708676467615],
247
[11.250000000000288, 15.261394308843652],
248
[5.230691228137659, 19.27708676467615],
249
[6.218103424245632, 20.31453620788313],
252
[11.250000000000288, 15.261394308843652],
253
[6.218103424245632, 20.31453620788313],
254
[7.0573935209813605, 21.60261968676894],
257
[11.250000000000288, 15.261394308843652],
258
[7.0573935209813605, 21.60261968676894],
259
[7.562614640098201, 22.78119613152436],
262
[11.250000000000288, 15.261394308843652],
263
[7.562614640098201, 22.78119613152436],
264
[7.891565269530931, 24.100075380674866],
267
[2.9681946363630565, 17.877212423185448],
268
[1.865174681370263e-14, 15.261394308843476],
269
[1.7491031281525513, 17.493171273505435],
272
[11.250000000000288, 15.261394308843652],
273
[7.891565269530931, 24.100075380674866],
274
[7.998836758981678, 25.574454740438988],
277
[1.7491031281525513, 17.493171273505435],
278
[1.865174681370263e-14, 15.261394308843476],
279
[0.5016277322923197, 17.31226361259904],
282
[0.5016277322923197, 17.31226361259904],
283
[1.865174681370263e-14, 15.261394308843476],
284
[-0.7537137010686332, 17.332451401390855],
287
[-0.7537137010686332, 17.332451401390855],
288
[1.865174681370263e-14, 15.261394308843476],
289
[-1.9968522144188805, 17.553880836526915],
292
[-1.9968522144188805, 17.553880836526915],
293
[1.865174681370263e-14, 15.261394308843476],
294
[-3.2074886001878475, 17.979094300174644],
297
[-9.675180053141624, 7.189474166860152],
298
[-8.000000000000007, 25.435657181405805],
299
[-7.775336189276026, 23.52026007738302],
302
[-9.675180053141624, 7.189474166860152],
303
[-7.775336189276026, 23.52026007738302],
304
[-7.202956818990907, 21.89398864152197],
307
[-9.675180053141624, 7.189474166860152],
308
[-7.202956818990907, 21.89398864152197],
309
[-6.400061873262468, 20.55209494165617],
312
[-9.675180053141624, 7.189474166860152],
313
[-6.400061873262468, 20.55209494165617],
314
[-5.438236844191913, 19.466079771934012],
317
[-9.675180053141624, 7.189474166860152],
318
[-5.438236844191913, 19.466079771934012],
319
[-4.363146304020197, 18.6133640552573],
322
[14.999999999999998, -5.087131436281159],
324
[17.858120437956206, 1.4481615037588669],
327
[14.999999999999998, -5.087131436281159],
328
[17.858120437956206, 1.4481615037588669],
329
[43.02919708029198, 5.606984283784323],
332
[11.250000000000288, 15.261394308843652],
333
[7.500000000000104, 35.60992005396825],
334
[12.41788321167908, 15.706982463846387],
337
[17.858120437956206, 1.4481615037588669],
338
[12.41788321167908, 15.706982463846387],
339
[26.06903892944051, 10.025733487561286],
342
[12.41788321167908, 15.706982463846387],
343
[7.500000000000104, 35.60992005396825],
344
[18.62074209245758, 28.96323007517763],
347
[18.62074209245758, 28.96323007517763],
348
[7.500000000000104, 35.60992005396825],
349
[21.514598540146046, 40.956977914001115],
352
[26.06903892944051, 10.025733487561286],
353
[12.41788321167908, 15.706982463846387],
354
[29.58409367396621, 16.811669764790274],
357
[21.514598540146046, 40.956977914001115],
358
[43.02919708029198, 5.606984283784323],
359
[26.066377737227047, 29.081589428850528],
362
[-7.499999999999897, 35.60992005396811],
363
[-15.000000000000004, -5.087131436281141],
364
[-16.15510948905104, 17.13286455985496],
367
[-16.15510948905104, 17.13286455985496],
368
[-15.000000000000004, -5.087131436281141],
369
[-28.547445255474443, 0.08169116175055202],
372
[-16.15510948905104, 17.13286455985496],
373
[-28.547445255474443, 0.08169116175055202],
374
[-14.040145985401361, 38.10521372198341],
377
[1.865174681370263e-14, 15.261394308843476],
378
[11.250000000000288, 15.261394308843652],
379
[3.0730973321624303e-13, 14.011394308843466],
383
[1.865174681370263e-14, 15.261394308843476],
384
[3.9929182594206813, 0.7486721736413746],
387
[43.02919708029198, 5.606984283784323],
388
[21.514598540146046, 40.956977914001115],
389
[26.944588276892826, 34.37501582471773],
392
[26.944588276892826, 34.37501582471773],
393
[21.514598540146046, 40.956977914001115],
394
[26.15196489046622, 36.21535391298498],
397
[26.15196489046622, 36.21535391298498],
398
[21.514598540146046, 40.956977914001115],
399
[25.84476047405407, 37.916465383064285],
402
[25.84476047405407, 37.916465383064285],
403
[21.514598540146046, 40.956977914001115],
404
[25.87128579081724, 39.380030657095766],
407
[25.87128579081724, 39.380030657095766],
408
[21.514598540146046, 40.956977914001115],
409
[26.11100308622886, 40.6355406301655],
412
[26.11100308622886, 40.6355406301655],
413
[21.514598540146046, 40.956977914001115],
414
[26.497639445753883, 41.724380698725795],
417
[26.497639445753883, 41.724380698725795],
418
[21.514598540146046, 40.956977914001115],
419
[27.183984543103612, 42.969261082525975],
422
[27.183984543103612, 42.969261082525975],
423
[21.514598540146046, 40.956977914001115],
424
[28.14843005156051, 44.13722039702907],
427
[43.02919708029198, 5.606984283784323],
428
[26.944588276892826, 34.37501582471773],
429
[28.19681524206102, 32.73904123800764],
432
[28.14843005156051, 44.13722039702907],
433
[21.514598540146046, 40.956977914001115],
434
[32.70246150567573, 50.94856048457167],
437
[28.14843005156051, 44.13722039702907],
438
[32.70246150567573, 50.94856048457167],
439
[29.302767287917, 45.08787343227521],
442
[29.302767287917, 45.08787343227521],
443
[32.70246150567573, 50.94856048457167],
444
[30.660487593670485, 45.821930600766585],
447
[30.660487593670485, 45.821930600766585],
448
[32.70246150567573, 50.94856048457167],
449
[32.26313326336891, 46.30353078355222],
452
[32.26313326336891, 46.30353078355222],
453
[32.70246150567573, 50.94856048457167],
454
[34.18909251852194, 46.42993921740859],
457
[34.18909251852194, 46.42993921740859],
458
[32.70246150567573, 50.94856048457167],
459
[36.05878789231104, 46.104567924314324],
462
[36.05878789231104, 46.104567924314324],
463
[32.70246150567573, 50.94856048457167],
464
[37.80932856673241, 45.35236428845875],
467
[37.80932856673241, 45.35236428845875],
468
[32.70246150567573, 50.94856048457167],
469
[38.896755890916, 44.60503880732156],
472
[38.896755890916, 44.60503880732156],
473
[32.70246150567573, 50.94856048457167],
474
[65.4049230113465, 25.590149424922807],
477
[38.896755890916, 44.60503880732156],
478
[65.4049230113465, 25.590149424922807],
479
[39.85252277691971, 43.69306265069026],
482
[39.85252277691971, 43.69306265069026],
483
[65.4049230113465, 25.590149424922807],
484
[53.255864532494236, 31.199931492767178],
487
[39.85252277691971, 43.69306265069026],
488
[53.255864532494236, 31.199931492767178],
489
[40.96082719430757, 42.11477162067656],
492
[40.96082719430757, 42.11477162067656],
493
[53.255864532494236, 31.199931492767178],
494
[41.617184924906304, 40.52050665883264],
497
[41.617184924906304, 40.52050665883264],
498
[53.255864532494236, 31.199931492767178],
499
[41.9121628340546, 38.984911567240694],
502
[41.9121628340546, 38.984911567240694],
503
[53.255864532494236, 31.199931492767178],
504
[41.91819260760609, 37.53434128643309],
507
[41.91819260760609, 37.53434128643309],
508
[53.255864532494236, 31.199931492767178],
509
[41.68434986554822, 36.18220818870151],
512
[41.68434986554822, 36.18220818870151],
513
[53.255864532494236, 31.199931492767178],
514
[41.2448003312233, 34.93910134437748],
517
[53.255864532494236, 31.199931492767178],
518
[65.4049230113465, 25.590149424922807],
519
[53.06443267520815, 30.45732160849354],
522
[53.06443267520815, 30.45732160849354],
523
[65.4049230113465, 25.590149424922807],
524
[52.76874058903504, 29.767968450275603],
527
[52.76874058903504, 29.767968450275603],
528
[65.4049230113465, 25.590149424922807],
529
[52.380391239898955, 29.13607708911145],
532
[52.380391239898955, 29.13607708911145],
533
[65.4049230113465, 25.590149424922807],
534
[51.90795647733907, 28.56573184059028],
537
[53.255864532494236, 31.199931492767178],
538
[53.06443267520815, 30.45732160849354],
539
[51.35824503528044, 28.061326639676654],
542
[41.2448003312233, 34.93910134437748],
543
[51.35824503528044, 28.061326639676654],
544
[40.624963356048475, 33.81540764943102],
547
[40.624963356048475, 33.81540764943102],
548
[51.35824503528044, 28.061326639676654],
549
[39.84503080833293, 32.82201797715716],
552
[39.84503080833293, 32.82201797715716],
553
[51.35824503528044, 28.061326639676654],
554
[38.92211004035242, 31.9708369198837],
557
[51.35824503528044, 28.061326639676654],
558
[43.02919708029198, 5.606984283784323],
559
[41.70981649381012, 19.444571970323707],
562
[38.92211004035242, 31.9708369198837],
563
[41.70981649381012, 19.444571970323707],
564
[37.87158172333802, 31.275150413580757],
567
[37.87158172333802, 31.275150413580757],
568
[41.70981649381012, 19.444571970323707],
569
[36.70686855789293, 30.749684298184345],
572
[41.70981649381012, 19.444571970323707],
573
[43.02919708029198, 5.606984283784323],
574
[41.10110298369262, 19.022202323429074],
577
[41.10110298369262, 19.022202323429074],
578
[43.02919708029198, 5.606984283784323],
579
[40.43965903526714, 18.688838444701922],
582
[40.43965903526714, 18.688838444701922],
583
[43.02919708029198, 5.606984283784323],
584
[39.73056621887958, 18.45214972475783],
587
[39.73056621887958, 18.45214972475783],
588
[43.02919708029198, 5.606984283784323],
589
[38.979096273465785, 18.32244213836834],
592
[36.70686855789293, 30.749684298184345],
593
[38.979096273465785, 18.32244213836834],
594
[35.44127054214691, 30.412578797256806],
597
[35.44127054214691, 30.412578797256806],
598
[38.979096273465785, 18.32244213836834],
599
[34.08776520308343, 30.286619645703045],
602
[34.08776520308343, 30.286619645703045],
603
[38.979096273465785, 18.32244213836834],
604
[32.660067783034044, 30.402504680495312],
607
[32.660067783034044, 30.402504680495312],
608
[38.979096273465785, 18.32244213836834],
609
[31.175949749223747, 30.804016529929132],
612
[31.175949749223747, 30.804016529929132],
613
[38.979096273465785, 18.32244213836834],
614
[29.665104064660564, 31.555294226568517],
617
[65.4049230113465, 25.590149424922807],
618
[32.70246150567573, 50.94856048457167],
619
[51.465608475382794, 42.9325255674089],
622
[65.4049230113465, 25.590149424922807],
623
[51.465608475382794, 42.9325255674089],
624
[72.06640589603971, 38.46938254560742],
627
[72.06640589603971, 38.46938254560742],
628
[51.465608475382794, 42.9325255674089],
629
[35.91834979483797, 57.166121301453884],
632
[72.06640589603971, 38.46938254560742],
633
[35.91834979483797, 57.166121301453884],
634
[43.70408043027748, 54.51325500675499],
637
[43.70408043027748, 54.51325500675499],
638
[35.91834979483797, 57.166121301453884],
639
[42.25506248921485, 55.5666183093269],
642
[42.25506248921485, 55.5666183093269],
643
[35.91834979483797, 57.166121301453884],
644
[41.07676983910428, 56.916008420237006],
647
[41.07676983910428, 56.916008420237006],
648
[35.91834979483797, 57.166121301453884],
649
[40.22828699354392, 58.493761284761845],
652
[40.22828699354392, 58.493761284761845],
653
[35.91834979483797, 57.166121301453884],
654
[39.75216042536598, 60.22076178615197],
657
[39.75216042536598, 60.22076178615197],
658
[35.91834979483797, 57.166121301453884],
659
[39.67226510883031, 62.010410907941555],
662
[39.67226510883031, 62.010410907941555],
663
[35.91834979483797, 57.166121301453884],
664
[38.07725906038438, 64.3486773077192],
667
[39.67226510883031, 62.010410907941555],
668
[38.07725906038438, 64.3486773077192],
669
[39.99260732872179, 63.772968170240155],
672
[72.06640589603971, 38.46938254560742],
673
[43.70408043027748, 54.51325500675499],
674
[45.35116385215525, 53.80873854697423],
677
[72.06640589603971, 38.46938254560742],
678
[45.35116385215525, 53.80873854697423],
679
[47.11372111445385, 53.48839632708271],
682
[72.06640589603971, 38.46938254560742],
683
[47.11372111445385, 53.48839632708271],
684
[48.90337023624344, 53.568291643618366],
687
[72.06640589603971, 38.46938254560742],
688
[48.90337023624344, 53.568291643618366],
689
[50.63037073763354, 54.04441821179629],
692
[72.06640589603971, 38.46938254560742],
693
[50.63037073763354, 54.04441821179629],
694
[52.2081236021584, 54.89290105735665],
697
[72.06640589603971, 38.46938254560742],
698
[52.2081236021584, 54.89290105735665],
699
[53.55751371306851, 56.07119370746722],
702
[72.06640589603971, 38.46938254560742],
703
[53.55751371306851, 56.07119370746722],
704
[76.38422442713265, 52.834494558138005],
707
[76.38422442713265, 52.834494558138005],
708
[53.55751371306851, 56.07119370746722],
709
[54.61087701564045, 57.52021164852984],
712
[76.38422442713265, 52.834494558138005],
713
[54.61087701564045, 57.52021164852984],
714
[55.315393475421125, 59.16729507040763],
718
test("build from triangles", () => {
719
const drawings = triangles
720
.sort(() => Math.random() - 0.4)
721
.map(([a, b, c]) => draw(a).lineTo(b).lineTo(c).close());
723
const fused = fuseAll(drawings);
724
expect(fused.toSVG()).toMatchSVGSnapshot();