pantograph2d 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1388 @@
1
+ function G(n, t, e, r) {
2
+ return n <= r && t >= e;
3
+ }
4
+ class q {
5
+ constructor(t = 1 / 0, e = 1 / 0, r = -1 / 0, s = -1 / 0) {
6
+ this.xMin = t, this.yMin = e, this.xMax = r, this.yMax = s;
7
+ }
8
+ get width() {
9
+ return this.xMax - this.xMin;
10
+ }
11
+ get height() {
12
+ return this.yMax - this.yMin;
13
+ }
14
+ contains(t) {
15
+ const [e, r] = t;
16
+ return G(this.xMin, this.xMax, e, e) && G(this.yMin, this.yMax, r, r);
17
+ }
18
+ overlaps(t) {
19
+ return G(this.xMin, this.xMax, t.xMin, t.xMax) && G(this.yMin, this.yMax, t.yMin, t.yMax);
20
+ }
21
+ addPoint(t) {
22
+ const [e, r] = t;
23
+ return new q(
24
+ Math.min(this.xMin, e),
25
+ Math.min(this.yMin, r),
26
+ Math.max(this.xMax, e),
27
+ Math.max(this.yMax, r)
28
+ );
29
+ }
30
+ merge(t) {
31
+ return new q(
32
+ Math.min(this.xMin, t.xMin),
33
+ Math.min(this.yMin, t.yMin),
34
+ Math.max(this.xMax, t.xMax),
35
+ Math.max(this.yMax, t.yMax)
36
+ );
37
+ }
38
+ }
39
+ function xt(n) {
40
+ const t = [];
41
+ for (let e = 0; e < n; e++)
42
+ for (let r = 0; r <= e; r++)
43
+ t.push([e, r]);
44
+ return t;
45
+ }
46
+ function* tt(n) {
47
+ for (const [t, e] of xt(n.length))
48
+ t !== e && (yield [n[t], n[e]]);
49
+ }
50
+ const Vt = Math.PI / 180, L = (n) => `[${n[0]}, ${n[1]}]`, h = ([n, t], [e, r], s = 1e-9) => Math.abs(n - e) <= s && Math.abs(t - r) <= s, M = ([n, t], [e, r]) => [n + e, t + r], S = ([n, t], [e, r]) => [n - e, t - r], X = ([n, t]) => n * n + t * t, v = ([n, t], e) => [n * e, t * e], j = ([n, t], [e, r] = [0, 0]) => (n - e) ** 2 + (t - r) ** 2, k = (n, t = [0, 0]) => Math.sqrt(j(n, t));
51
+ function $([n, t], [e, r]) {
52
+ return n * r - t * e;
53
+ }
54
+ function nt([n, t], [e, r]) {
55
+ return n * e + t * r;
56
+ }
57
+ function O([n, t]) {
58
+ const e = k([n, t]);
59
+ return [n / e, t / e];
60
+ }
61
+ function U(n, t) {
62
+ const e = Math.cos(t) * n, r = Math.sin(t) * n;
63
+ return [e, r];
64
+ }
65
+ function Et([n, t]) {
66
+ return Math.atan2(t, n);
67
+ }
68
+ function Ot(n) {
69
+ const t = k(n), e = Et(n);
70
+ return [t, e];
71
+ }
72
+ function Mt(n, t, e = 1e-9) {
73
+ const r = $(n, t), s = X(n), i = X(t);
74
+ return r * r < s * i * e * e;
75
+ }
76
+ function B(n) {
77
+ return [-n[1], n[0]];
78
+ }
79
+ function ut(n) {
80
+ return [n[1], -n[0]];
81
+ }
82
+ const _ = (n, t) => {
83
+ const [e, r, s, i, o, a, u, l, c] = n, [f, g, p, d, w, x, C, y, T] = t;
84
+ return [
85
+ e * f + r * d + s * C,
86
+ e * g + r * w + s * y,
87
+ e * p + r * x + s * T,
88
+ i * f + o * d + a * C,
89
+ i * g + o * w + a * y,
90
+ i * p + o * x + a * T,
91
+ u * f + l * d + c * C,
92
+ u * g + l * w + c * y,
93
+ u * p + l * x + c * T
94
+ ];
95
+ };
96
+ class I {
97
+ constructor() {
98
+ this._matrix = [1, 0, 0, 0, 1, 0, 0, 0, 1];
99
+ }
100
+ translate(t, e) {
101
+ return this._matrix = _(this._matrix, [1, 0, t, 0, 1, e, 0, 0, 1]), this;
102
+ }
103
+ rotate(t, e) {
104
+ const r = Math.cos(t), s = Math.sin(t), i = [r, -s, 0, s, r, 0, 0, 0, 1];
105
+ return e && this.translate(e[0], e[1]), this._matrix = _(this._matrix, i), e && this.translate(-e[0], -e[1]), this;
106
+ }
107
+ mirrorX() {
108
+ return this._matrix = _(this._matrix, [1, 0, 0, 0, -1, 0, 0, 0, 1]), this;
109
+ }
110
+ mirrorY() {
111
+ return this._matrix = _(this._matrix, [-1, 0, 0, 0, 1, 0, 0, 0, 1]), this;
112
+ }
113
+ mirrorLine(t, e) {
114
+ const [r, s] = t, i = Math.atan2(s, r);
115
+ return e && this.translate(e[0], e[1]), this.rotate(i), this.mirrorX(), this.rotate(-i), e && this.translate(-e[0], -e[1]), this;
116
+ }
117
+ mirrorCenter(t) {
118
+ return t && this.translate(t[0], t[1]), this._matrix = _(this._matrix, [-1, 0, 0, 0, -1, 0, 0, 0, 1]), t && this.translate(-t[0], -t[1]), this;
119
+ }
120
+ scale(t, e) {
121
+ return e && this.translate(e[0], e[1]), this._matrix = _(this._matrix, [t, 0, 0, 0, t, 0, 0, 0, 1]), e && this.translate(-e[0], -e[1]), this;
122
+ }
123
+ transform(t) {
124
+ const [e, r] = t, [s, i, o, a, u, l] = this._matrix;
125
+ return [s * e + i * r + o, a * e + u * r + l];
126
+ }
127
+ keepsOrientation() {
128
+ const [t, , , , e] = this._matrix;
129
+ return t * e > 0;
130
+ }
131
+ }
132
+ class z {
133
+ translateX(t) {
134
+ const e = new I().translate(t, 0);
135
+ return this.transform(e);
136
+ }
137
+ translateY(t) {
138
+ const e = new I().translate(0, t);
139
+ return this.transform(e);
140
+ }
141
+ translate(t, e) {
142
+ const r = new I().translate(t, e);
143
+ return this.transform(r);
144
+ }
145
+ translateTo([t, e]) {
146
+ const r = new I().translate(t, e);
147
+ return this.transform(r);
148
+ }
149
+ rotate(t, e) {
150
+ const r = new I().rotate(
151
+ t * Vt,
152
+ e
153
+ );
154
+ return this.transform(r);
155
+ }
156
+ scale(t, e) {
157
+ const r = new I().scale(t, e);
158
+ return this.transform(r);
159
+ }
160
+ mirrorCenter(t) {
161
+ const e = new I().mirrorCenter(t);
162
+ return this.transform(e);
163
+ }
164
+ mirror(t = "x", e) {
165
+ const r = new I();
166
+ return t === "x" ? r.mirrorX() : t === "y" ? r.mirrorY() : r.mirrorLine(t, e), this.transform(r);
167
+ }
168
+ }
169
+ class b extends z {
170
+ constructor(t, e = [], { ignoreChecks: r = !1 } = {}) {
171
+ super(), r || Ft(t, e), this.contour = t, this.holes = e;
172
+ }
173
+ get boundingBox() {
174
+ return this.contour.boundingBox;
175
+ }
176
+ get isFull() {
177
+ return this.holes.length === 0;
178
+ }
179
+ get allLoops() {
180
+ return [this.contour, ...this.holes];
181
+ }
182
+ clone() {
183
+ return new b(
184
+ this.contour.clone(),
185
+ this.holes.map((t) => t.clone())
186
+ );
187
+ }
188
+ transform(t) {
189
+ return new b(
190
+ this.contour.transform(t),
191
+ this.holes.map((e) => e.transform(t))
192
+ );
193
+ }
194
+ contains(t) {
195
+ return this.contour.contains(t) && !this.holes.some((e) => e.contains(t));
196
+ }
197
+ intersects(t) {
198
+ return this.allLoops.some(
199
+ (e) => t.allLoops.some((r) => e.intersects(r))
200
+ );
201
+ }
202
+ }
203
+ function Ft(n, t = []) {
204
+ if (!n)
205
+ throw new Error("Figure must have a contour");
206
+ for (const [e, r] of tt([n, ...t]))
207
+ if (e.intersects(r))
208
+ throw new Error("Loops in a figure must not intersect");
209
+ if (t.some((e) => !n.contains(e.firstPoint)))
210
+ throw new Error("Holes must be inside the contour");
211
+ for (const [e, r] of tt(t))
212
+ if (e.contains(r.firstPoint))
213
+ throw console.error(wt(e), wt(r)), new Error("Holes must not be inside other holes");
214
+ }
215
+ const Lt = (n) => {
216
+ const t = n.map((s, i) => n.slice(i + 1).map((o, a) => [a + i + 1, o]).filter(([, o]) => s.boundingBox.overlaps(o.boundingBox)).map(([o]) => o)), e = [], r = Array(t.length);
217
+ return t.forEach((s, i) => {
218
+ let o = r[i];
219
+ o || (o = [], e.push(o)), o.push(n[i]), s.length && s.forEach((a) => {
220
+ r[a] = o;
221
+ });
222
+ }), e;
223
+ }, St = (n) => n.map((t, e) => {
224
+ const s = t.segments[0].midPoint, i = n.filter((o, a) => e === a ? !1 : o.contains(s));
225
+ return {
226
+ loop: t,
227
+ isIn: i
228
+ };
229
+ }), Bt = (n, t) => n.flatMap(({ loop: e }) => st(
230
+ t.filter(
231
+ ({ loop: r, isIn: s }) => r === e || s.indexOf(e) !== -1
232
+ )
233
+ )), Tt = (n, t) => {
234
+ const e = t.filter(({ isIn: s }) => s.length <= 1), r = st(
235
+ St(n.map(({ loop: s }) => s))
236
+ );
237
+ return [e, ...r];
238
+ }, st = (n) => {
239
+ if (!n.length)
240
+ return [];
241
+ const t = n.filter(({ isIn: r }) => !r.length), e = n.filter(({ isIn: r }) => r.length > 1);
242
+ return t.length === 1 && e.length === 0 ? [n] : t.length > 1 ? Bt(t, n) : Tt(e, n);
243
+ };
244
+ function D(n) {
245
+ return Lt(n).map(St).flatMap(st).map((e) => {
246
+ if (e.length === 1)
247
+ return new b(e[0].loop);
248
+ e.sort((i, o) => i.isIn.length - o.isIn.length);
249
+ const [r, ...s] = e.map(({ loop: i }) => i);
250
+ return new b(r, s);
251
+ });
252
+ }
253
+ function _t(n, t) {
254
+ const e = [];
255
+ for (const r of n)
256
+ for (const s of t)
257
+ e.push([r, s]);
258
+ return e;
259
+ }
260
+ function $t(n) {
261
+ return Array.from(Array(n).keys());
262
+ }
263
+ function F(n) {
264
+ const t = Math.min(...n.map((e) => e.length));
265
+ return $t(t).map((e) => n.map((r) => r[e]));
266
+ }
267
+ const lt = (n, t = 1e-9) => {
268
+ let e = n;
269
+ return Math.abs(n) < t && (e = 0), e.toFixed(-Math.log10(t));
270
+ };
271
+ function J(n, t = 1e-9) {
272
+ return Array.from(
273
+ new Map(
274
+ n.map(([e, r]) => [
275
+ `[${lt(e, t)},${lt(r, t)}]`,
276
+ [e, r]
277
+ ])
278
+ ).values()
279
+ );
280
+ }
281
+ class yt extends z {
282
+ constructor(t, e) {
283
+ super(), this.firstPoint = t, this.lastPoint = e, this.precision = 1e-9, this.firstPoint = t, this.lastPoint = e;
284
+ }
285
+ get repr() {
286
+ return `${this.segmentType} ${L(this.firstPoint)} - ${L(
287
+ this.lastPoint
288
+ )}`;
289
+ }
290
+ [Symbol.for("nodejs.util.inspect.custom")]() {
291
+ return this.repr;
292
+ }
293
+ }
294
+ class P extends yt {
295
+ constructor() {
296
+ super(...arguments), this.segmentType = "LINE", this._V = null, this._boundingBox = null;
297
+ }
298
+ isValidParameter(t) {
299
+ const e = this.length * this.precision;
300
+ return t >= -e && 1 - t >= -e;
301
+ }
302
+ paramPoint(t) {
303
+ return M(this.firstPoint, v(this.V, t));
304
+ }
305
+ get length() {
306
+ return k(this.firstPoint, this.lastPoint);
307
+ }
308
+ get squareLength() {
309
+ return j(this.firstPoint, this.lastPoint);
310
+ }
311
+ get V() {
312
+ return this._V === null && (this._V = S(this.lastPoint, this.firstPoint)), this._V;
313
+ }
314
+ get midPoint() {
315
+ return M(this.firstPoint, v(this.V, 0.5));
316
+ }
317
+ isSame(t) {
318
+ return t instanceof P ? h(this.firstPoint, t.firstPoint) && h(this.lastPoint, t.lastPoint) || h(this.lastPoint, t.firstPoint) && h(this.firstPoint, t.lastPoint) : !1;
319
+ }
320
+ clone() {
321
+ return new P(this.firstPoint, this.lastPoint);
322
+ }
323
+ reverse() {
324
+ return new P(this.lastPoint, this.firstPoint);
325
+ }
326
+ get boundingBox() {
327
+ return this._boundingBox === null && (this._boundingBox = new q(
328
+ Math.min(this.firstPoint[0], this.lastPoint[0]) - this.precision,
329
+ Math.min(this.firstPoint[1], this.lastPoint[1]) - this.precision,
330
+ Math.max(this.firstPoint[0], this.lastPoint[0]) + this.precision,
331
+ Math.max(this.firstPoint[1], this.lastPoint[1]) + this.precision
332
+ )), this._boundingBox;
333
+ }
334
+ distanceFrom(t) {
335
+ const e = S(t, this.firstPoint), r = nt(e, this.V) / this.squareLength;
336
+ if (r < 0)
337
+ return k(t, this.firstPoint);
338
+ if (r > 1)
339
+ return k(t, this.lastPoint);
340
+ const s = this.paramPoint(r);
341
+ return k(t, s);
342
+ }
343
+ isOnSegment(t) {
344
+ if (h(t, this.firstPoint, this.precision))
345
+ return !0;
346
+ const e = S(t, this.firstPoint);
347
+ if (!Mt(this.V, e))
348
+ return !1;
349
+ const r = nt(e, this.V) / this.squareLength;
350
+ return this.isValidParameter(r);
351
+ }
352
+ tangentAt(t) {
353
+ if (!this.isOnSegment(t))
354
+ throw new Error("Point is not on segment");
355
+ return O(this.V);
356
+ }
357
+ get tangentAtFirstPoint() {
358
+ return O(this.V);
359
+ }
360
+ get tangentAtLastPoint() {
361
+ return O(this.V);
362
+ }
363
+ splitAt(t) {
364
+ let e;
365
+ if (Array.isArray(t) && t.length === 0)
366
+ return [this];
367
+ Array.isArray(t[0]) ? e = t : e = [t], e.forEach((u) => {
368
+ if (!this.isOnSegment(u))
369
+ throw new Error(
370
+ `Point ${L(u)} is not on segment ${this.repr}`
371
+ );
372
+ });
373
+ const r = [this.firstPoint, ...e, this.lastPoint], s = J(r), i = this.lastPoint[0] - this.firstPoint[0];
374
+ let o = Math.sign(i), a = 0;
375
+ return Math.abs(i) < this.precision && (o = Math.sign(this.lastPoint[1] - this.firstPoint[1]), a = 1), s.sort(
376
+ (u, l) => o * (u[a] - l[a])
377
+ ), s.flatMap((u, l) => l === s.length - 1 ? [] : new P(u, s[l + 1]));
378
+ }
379
+ transform(t) {
380
+ return new P(
381
+ t.transform(this.firstPoint),
382
+ t.transform(this.lastPoint)
383
+ );
384
+ }
385
+ }
386
+ function Q(n) {
387
+ return n < 0 ? n + 2 * Math.PI : n >= 2 * Math.PI ? n % (2 * Math.PI) : n;
388
+ }
389
+ function ct(n, t, e) {
390
+ let r = t - n;
391
+ return e && (r = -r), r < 0 && (r += 2 * Math.PI), r;
392
+ }
393
+ const K = (n, t, e) => {
394
+ const r = $(n.V, t.V), s = X(n.V), i = X(t.V), o = e ? e * e : n.precision * t.precision;
395
+ if (r * r < s * i * o)
396
+ return "parallel";
397
+ const a = S(t.firstPoint, n.firstPoint), u = $(a, t.V) / r, l = $(a, n.V) / r;
398
+ return {
399
+ intersectionParam1: u,
400
+ intersectionParam2: l
401
+ };
402
+ };
403
+ function qt(n, t, e = !1, r) {
404
+ const s = K(n, t, r);
405
+ if (s === "parallel") {
406
+ if (!e)
407
+ return null;
408
+ if (n.isSame(t))
409
+ return n;
410
+ const a = J(
411
+ [
412
+ t.isOnSegment(n.firstPoint) ? n.firstPoint : null,
413
+ t.isOnSegment(n.lastPoint) ? n.lastPoint : null,
414
+ n.isOnSegment(t.firstPoint) ? t.firstPoint : null,
415
+ n.isOnSegment(t.lastPoint) ? t.lastPoint : null
416
+ ].filter((u) => u !== null)
417
+ ).sort((u, l) => u[0] - l[0]);
418
+ if (a.length === 0)
419
+ return null;
420
+ if (a.length === 1)
421
+ return null;
422
+ if (a.length === 2)
423
+ return new P(a[0], a[1]);
424
+ throw console.error(a), new Error(
425
+ "Unexpected number of points while intersecting parallel lines"
426
+ );
427
+ }
428
+ const { intersectionParam1: i, intersectionParam2: o } = s;
429
+ return !n.isValidParameter(i) || !t.isValidParameter(o) ? null : n.paramPoint(i);
430
+ }
431
+ const W = (n, t) => {
432
+ const e = S(n, t);
433
+ return Ot(e);
434
+ };
435
+ class m extends yt {
436
+ constructor(t, e, r, s = !1, { ignoreChecks: i = !1 } = {}) {
437
+ if (super(t, e), this.segmentType = "ARC", this._angularLength = null, this._radius = null, this._firstAngle = null, this._lastAngle = null, this._boundingBox = null, this.center = r, this.clockwise = s, !i) {
438
+ if (h(t, e))
439
+ throw new Error("Invalid arc, cannot be a full circle");
440
+ if (Math.abs(this.radius - k(this.lastPoint, this.center)) > this.precision)
441
+ throw new Error(
442
+ "Invalid arc, radius does not match between start and end"
443
+ );
444
+ }
445
+ }
446
+ isValidParameter(t) {
447
+ return 1 - t >= -this.precision && t >= -this.precision;
448
+ }
449
+ angleToParam(t) {
450
+ return ct(this.firstAngle, Q(t), this.clockwise) / this.angularLength;
451
+ }
452
+ get angularLength() {
453
+ return this._angularLength || (this._angularLength = ct(
454
+ this.firstAngle,
455
+ this.lastAngle,
456
+ this.clockwise
457
+ )), this._angularLength;
458
+ }
459
+ paramPoint(t) {
460
+ return M(
461
+ this.center,
462
+ U(
463
+ this.radius,
464
+ this.firstAngle + t * this.angularLength * (this.clockwise ? -1 : 1)
465
+ )
466
+ );
467
+ }
468
+ pointToParam(t) {
469
+ const [e, r] = W(t, this.center);
470
+ if (Math.abs(e - this.radius) > this.precision)
471
+ throw new Error(
472
+ `Point ${L(t)} is not on segment ${this.repr}`
473
+ );
474
+ const s = this.angleToParam(r);
475
+ if (!this.isValidParameter(s))
476
+ throw new Error(
477
+ `Point ${L(t)} is not on segment ${this.repr}`
478
+ );
479
+ return s;
480
+ }
481
+ get radius() {
482
+ return this._radius === null && (this._radius = k(this.firstPoint, this.center)), this._radius;
483
+ }
484
+ get firstAngle() {
485
+ if (this._firstAngle === null) {
486
+ const [t, e] = S(this.firstPoint, this.center);
487
+ this._firstAngle = Q(Math.atan2(e, t));
488
+ }
489
+ return this._firstAngle;
490
+ }
491
+ get lastAngle() {
492
+ if (this._lastAngle === null) {
493
+ const [t, e] = S(this.lastPoint, this.center);
494
+ this._lastAngle = Q(Math.atan2(e, t));
495
+ }
496
+ return this._lastAngle;
497
+ }
498
+ get length() {
499
+ return this.radius * this.angularLength;
500
+ }
501
+ get squareLength() {
502
+ return this.length * this.length;
503
+ }
504
+ get midPoint() {
505
+ return this.paramPoint(0.5);
506
+ }
507
+ isSame(t) {
508
+ return !(t instanceof m) || !h(this.center, t.center) ? !1 : h(this.firstPoint, t.firstPoint) && h(this.lastPoint, t.lastPoint) && this.clockwise === t.clockwise || h(this.lastPoint, t.firstPoint) && h(this.firstPoint, t.lastPoint) && this.clockwise === !t.clockwise;
509
+ }
510
+ clone() {
511
+ return new m(
512
+ this.firstPoint,
513
+ this.lastPoint,
514
+ this.center,
515
+ this.clockwise
516
+ );
517
+ }
518
+ reverse() {
519
+ return new m(
520
+ this.lastPoint,
521
+ this.firstPoint,
522
+ this.center,
523
+ !this.clockwise
524
+ );
525
+ }
526
+ get boundingBox() {
527
+ if (this._boundingBox === null) {
528
+ const t = this.radius + this.precision, e = (r) => this.isValidParameter(this.angleToParam(r));
529
+ this._boundingBox = new q(
530
+ e(Math.PI) ? this.center[0] - t : Math.min(this.firstPoint[0], this.lastPoint[0]) - this.precision,
531
+ e(Math.PI * 1.5) ? this.center[1] - t : Math.min(this.firstPoint[1], this.lastPoint[1]) - this.precision,
532
+ e(0) ? this.center[0] + t : Math.max(this.firstPoint[0], this.lastPoint[0]) + this.precision,
533
+ e(Math.PI / 2) ? this.center[1] + t : Math.max(this.firstPoint[1], this.lastPoint[1]) + this.precision
534
+ );
535
+ }
536
+ return this._boundingBox;
537
+ }
538
+ distanceFrom(t) {
539
+ const [e, r] = W(t, this.center);
540
+ return this.isValidParameter(this.angleToParam(r)) ? Math.abs(e - this.radius) : Math.sqrt(
541
+ Math.min(
542
+ j(t, this.firstPoint),
543
+ j(t, this.lastPoint)
544
+ )
545
+ );
546
+ }
547
+ isOnSegment(t) {
548
+ const [e, r] = W(t, this.center);
549
+ if (Math.abs(e - this.radius) > this.precision)
550
+ return !1;
551
+ const s = this.angleToParam(r);
552
+ return this.isValidParameter(s);
553
+ }
554
+ tangentAt(t) {
555
+ const [e, r] = W(t, this.center);
556
+ if (Math.abs(e - this.radius) > this.precision)
557
+ throw new Error("Point is not on the arc");
558
+ const s = this.angleToParam(r);
559
+ if (!this.isValidParameter(s))
560
+ throw new Error("Point is not on the arc");
561
+ const i = U(
562
+ 1,
563
+ r + (this.clockwise ? Math.PI / 2 : -Math.PI / 2)
564
+ );
565
+ return O(i);
566
+ }
567
+ get tangentAtFirstPoint() {
568
+ const t = U(1, this.firstAngle);
569
+ return (this.clockwise ? ut : B)(O(t));
570
+ }
571
+ get tangentAtLastPoint() {
572
+ const t = U(1, this.lastAngle);
573
+ return (this.clockwise ? ut : B)(O(t));
574
+ }
575
+ splitAt(t) {
576
+ let e;
577
+ if (Array.isArray(t) && t.length === 0)
578
+ return [this];
579
+ Array.isArray(t[0]) ? e = t : e = [t];
580
+ const s = [0, 1, ...e.map((a) => this.pointToParam(a))], i = new Map(
581
+ F([s, [this.firstPoint, this.lastPoint, ...e]])
582
+ );
583
+ s.sort((a, u) => a - u);
584
+ let o = null;
585
+ return s.flatMap((a, u) => {
586
+ if (u === s.length - 1)
587
+ return [];
588
+ const l = s[u + 1];
589
+ if (l - a < this.precision)
590
+ return o === null && (o = a), [];
591
+ const c = o === null ? a : o, f = new m(
592
+ i.get(c) || this.paramPoint(c),
593
+ i.get(l) || this.paramPoint(l),
594
+ this.center,
595
+ this.clockwise
596
+ );
597
+ return o = null, f;
598
+ });
599
+ }
600
+ transform(t) {
601
+ return new m(
602
+ t.transform(this.firstPoint),
603
+ t.transform(this.lastPoint),
604
+ t.transform(this.center),
605
+ t.keepsOrientation() ? this.clockwise : !this.clockwise
606
+ );
607
+ }
608
+ }
609
+ function rn(n, t, e) {
610
+ const r = new P(t, n), s = new P(t, e), i = B(r.tangentAtFirstPoint), o = B(s.tangentAtLastPoint), a = K(
611
+ { firstPoint: r.midPoint, V: i, precision: 1e-9 },
612
+ { firstPoint: s.midPoint, V: o, precision: 1e-9 }
613
+ );
614
+ if (a === "parallel")
615
+ throw new Error("Cannot create an arc from three colinear points");
616
+ const u = $(
617
+ S(n, t),
618
+ S(e, t)
619
+ ) > 0;
620
+ return new m(
621
+ n,
622
+ e,
623
+ M(r.midPoint, v(i, a.intersectionParam1)),
624
+ u,
625
+ { ignoreChecks: !0 }
626
+ );
627
+ }
628
+ function sn(n, t, e) {
629
+ const r = new P(t, n), s = B(r.tangentAtFirstPoint), i = K(
630
+ { firstPoint: r.midPoint, V: s, precision: 1e-9 },
631
+ {
632
+ firstPoint: n,
633
+ V: B(e),
634
+ precision: 1e-9
635
+ }
636
+ );
637
+ if (i === "parallel")
638
+ throw new Error("Cannot create an arc from three colinear points");
639
+ const o = M(
640
+ r.midPoint,
641
+ v(s, i.intersectionParam1)
642
+ ), a = $(
643
+ S(o, n),
644
+ M(n, e)
645
+ ) > 0;
646
+ return new m(n, t, o, a, {
647
+ ignoreChecks: !0
648
+ });
649
+ }
650
+ function Dt(n, t) {
651
+ const e = S(t, n.firstPoint), r = nt(e, n.V) / n.squareLength;
652
+ return n.paramPoint(r);
653
+ }
654
+ function ht(n, t, e) {
655
+ const r = e || n.precision, s = Dt(n, t.center), i = k(s, t.center);
656
+ if (i > t.radius + r)
657
+ return [];
658
+ if (Math.abs(i - t.radius) < r) {
659
+ const f = s;
660
+ return n.isOnSegment(f) && t.isOnSegment(f) ? [f] : [];
661
+ }
662
+ const o = [], a = Math.sqrt(
663
+ t.radius * t.radius - i * i
664
+ ), u = n.tangentAtFirstPoint, l = M(s, v(u, a));
665
+ n.isOnSegment(l) && t.isOnSegment(l) && o.push(l);
666
+ const c = M(s, v(u, -a));
667
+ return n.isOnSegment(c) && t.isOnSegment(c) && o.push(c), o;
668
+ }
669
+ const jt = (n) => {
670
+ const { firstPoint: t, lastPoint: e, center: r, clockwise: s } = n;
671
+ return new m(e, t, r, s, {
672
+ ignoreChecks: !0
673
+ });
674
+ }, Nt = (n, t) => {
675
+ if (n.isSame(t))
676
+ return [n];
677
+ const e = J(
678
+ [
679
+ t.isOnSegment(n.firstPoint) ? n.firstPoint : null,
680
+ t.isOnSegment(n.lastPoint) ? n.lastPoint : null,
681
+ n.isOnSegment(t.firstPoint) ? t.firstPoint : null,
682
+ n.isOnSegment(t.lastPoint) ? t.lastPoint : null
683
+ ].filter((r) => r !== null)
684
+ // We sort by the param value of the first arc. This means that the points
685
+ // will be sorted with the same orientation than arc1
686
+ ).sort((r, s) => n.pointToParam(r) - n.pointToParam(s));
687
+ if (e.length === 0)
688
+ return [];
689
+ if (e.length === 1)
690
+ return [];
691
+ if (e.length === 2)
692
+ return n.isSame(jt(t)) ? [] : [new m(e[0], e[1], n.center, n.clockwise)];
693
+ if (e.length === 3) {
694
+ const r = h(e[0], t.lastPoint) || h(e[0], t.firstPoint) ? 1 : 0;
695
+ return [
696
+ new m(
697
+ e[0 + r],
698
+ e[1 + r],
699
+ n.center,
700
+ n.clockwise
701
+ )
702
+ ];
703
+ } else if (e.length === 4)
704
+ return [
705
+ new m(e[0], e[1], n.center, n.clockwise),
706
+ new m(e[2], e[3], n.center, n.clockwise)
707
+ ];
708
+ throw new Error("Bug in the arc arc overlap algorithm");
709
+ };
710
+ function Rt(n, t, e = !1, r) {
711
+ const s = r || n.precision, i = k(n.center, t.center), o = n.radius + t.radius;
712
+ if (i > o + s)
713
+ return [];
714
+ const a = Math.abs(n.radius - t.radius);
715
+ if (i < a - s)
716
+ return [];
717
+ if (i < s)
718
+ return a > s ? [] : e ? Nt(n, t) : [];
719
+ const u = O(S(t.center, n.center)), l = i > o - s;
720
+ if (
721
+ // circles are outside each other
722
+ l || // circles are inside each other
723
+ Math.abs(i - a) < s
724
+ ) {
725
+ const C = l || n.radius > t.radius ? 1 : -1, y = M(
726
+ n.center,
727
+ v(u, C * n.radius)
728
+ );
729
+ return n.isOnSegment(y) && t.isOnSegment(y) ? [y] : [];
730
+ }
731
+ const c = n.radius * n.radius / (2 * i) - t.radius * t.radius / (2 * i) + i / 2, f = M(
732
+ n.center,
733
+ v(u, c)
734
+ ), g = Math.sqrt(
735
+ n.radius * n.radius - c * c
736
+ ), p = B(u), d = M(f, v(p, g)), w = M(f, v(p, -g)), x = [];
737
+ return n.isOnSegment(d) && t.isOnSegment(d) && x.push(d), n.isOnSegment(w) && t.isOnSegment(w) && x.push(w), x;
738
+ }
739
+ function it(n, t, e) {
740
+ if (n instanceof P && t instanceof P) {
741
+ const r = qt(
742
+ n,
743
+ t,
744
+ !0,
745
+ e
746
+ );
747
+ return r === null ? { intersections: [], overlaps: [], count: 0 } : r instanceof P ? { intersections: [], overlaps: [r], count: 1 } : { intersections: [r], overlaps: [], count: 1 };
748
+ }
749
+ if (!n.boundingBox.overlaps(t.boundingBox))
750
+ return { intersections: [], overlaps: [], count: 0 };
751
+ if (n instanceof P && t instanceof m) {
752
+ const r = ht(n, t, e);
753
+ return { intersections: r, overlaps: [], count: r.length };
754
+ }
755
+ if (n instanceof m && t instanceof P) {
756
+ const r = ht(t, n, e);
757
+ return { intersections: r, overlaps: [], count: r.length };
758
+ }
759
+ if (n instanceof m && t instanceof m) {
760
+ const r = Rt(
761
+ n,
762
+ t,
763
+ !0,
764
+ e
765
+ );
766
+ return r.length ? r[0] instanceof m ? {
767
+ intersections: [],
768
+ overlaps: r,
769
+ count: r.length
770
+ } : {
771
+ intersections: r,
772
+ overlaps: [],
773
+ count: r.length
774
+ } : { intersections: [], overlaps: [], count: 0 };
775
+ }
776
+ throw new Error("Not implemented");
777
+ }
778
+ class bt extends z {
779
+ constructor(t, { ignoreChecks: e = !1 } = {}) {
780
+ super(), this._boundingBox = null, e || At(t), this.segments = t;
781
+ }
782
+ get repr() {
783
+ return this.segments.map((t) => t.repr).join(`
784
+ `) + `
785
+ `;
786
+ }
787
+ get firstPoint() {
788
+ return this.segments[0].firstPoint;
789
+ }
790
+ get lastPoint() {
791
+ return this.segments[this.segments.length - 1].lastPoint;
792
+ }
793
+ get segmentsCount() {
794
+ return this.segments.length;
795
+ }
796
+ onStroke(t) {
797
+ return this.segments.some((e) => e.isOnSegment(t));
798
+ }
799
+ intersects(t) {
800
+ return this.boundingBox.overlaps(t.boundingBox) ? this.segments.some(
801
+ (e) => t.segments.some(
802
+ (r) => it(e, r).count > 0
803
+ )
804
+ ) : !1;
805
+ }
806
+ get boundingBox() {
807
+ if (this._boundingBox === null) {
808
+ let t = this.segments[0].boundingBox;
809
+ this.segments.slice(1).forEach((e) => {
810
+ t = t.merge(e.boundingBox);
811
+ }), this._boundingBox = t;
812
+ }
813
+ return this._boundingBox;
814
+ }
815
+ [Symbol.for("nodejs.util.inspect.custom")]() {
816
+ return this.repr;
817
+ }
818
+ }
819
+ function Gt(n, t = "Stroke") {
820
+ xt(n.length).forEach(
821
+ ([e, r]) => {
822
+ if (e === r)
823
+ return;
824
+ const s = n[e], i = n[r], o = it(s, i);
825
+ if (o.count !== 0) {
826
+ if (o.count === 1 && !o.overlaps.length) {
827
+ const a = e - r, u = o.intersections[0];
828
+ if (a === 1 && h(s.firstPoint, u) || a === -1 && h(s.lastPoint, u) || a === n.length - 1 && h(s.lastPoint, u) && h(i.firstPoint, u) || -a === n.length - 1 && h(s.firstPoint, u) && h(i.lastPoint, u))
829
+ return;
830
+ }
831
+ throw new Error(
832
+ `${t} segments must not intersect, but segments ${s.repr} and ${i.repr} do`
833
+ );
834
+ }
835
+ }
836
+ );
837
+ }
838
+ function At(n, t = "Stroke") {
839
+ if (n.length === 0)
840
+ throw new Error(`${t} must have at least one segment`);
841
+ F([n.slice(0, -1), n.slice(1)]).forEach(
842
+ ([e, r]) => {
843
+ if (!h(e.lastPoint, r.firstPoint))
844
+ throw new Error(
845
+ `${t} segments must be connected, but ${e.repr} and ${r.repr} are not`
846
+ );
847
+ }
848
+ ), Gt(n, t);
849
+ }
850
+ function ft(n, t) {
851
+ return !!(n instanceof P && t instanceof P && Mt(n.V, t.V) || n instanceof m && t instanceof m && h(n.center, t.center) && n.radius - t.radius < n.precision);
852
+ }
853
+ function gt(n, t) {
854
+ if (n instanceof P && t instanceof P)
855
+ return new P(n.firstPoint, t.lastPoint);
856
+ if (n instanceof m && t instanceof m)
857
+ return new m(
858
+ n.firstPoint,
859
+ t.lastPoint,
860
+ n.center,
861
+ n.clockwise
862
+ );
863
+ throw new Error("Not implemented");
864
+ }
865
+ function vt(n) {
866
+ let t = !1;
867
+ const e = [];
868
+ for (const r of n.segments) {
869
+ if (e.length === 0) {
870
+ e.push(r);
871
+ continue;
872
+ }
873
+ const s = e[e.length - 1];
874
+ ft(s, r) ? (t = !0, e.pop(), e.push(gt(s, r))) : e.push(r);
875
+ }
876
+ if (h(n.firstPoint, n.lastPoint) && ft(
877
+ e[0],
878
+ e[e.length - 1]
879
+ )) {
880
+ t = !0;
881
+ const r = e.pop();
882
+ e[0] = gt(r, e[0]);
883
+ }
884
+ return t ? e : null;
885
+ }
886
+ class A extends bt {
887
+ constructor() {
888
+ super(...arguments), this.strokeType = "STRAND";
889
+ }
890
+ reverse() {
891
+ const t = this.segments.map((e) => e.reverse());
892
+ return t.reverse(), new A(t, { ignoreChecks: !0 });
893
+ }
894
+ clone() {
895
+ return new A(
896
+ this.segments.map((t) => t.clone()),
897
+ { ignoreChecks: !0 }
898
+ );
899
+ }
900
+ extend(t) {
901
+ if (!h(this.lastPoint, t.firstPoint))
902
+ throw console.error(this.repr, t.repr), new Error("Cannot extend strand: connection point is not the same");
903
+ return new A([...this.segments, ...t.segments]);
904
+ }
905
+ simplify() {
906
+ const t = vt(this);
907
+ return t ? new A(t, { ignoreChecks: !0 }) : this;
908
+ }
909
+ transform(t) {
910
+ return new A(
911
+ this.segments.map((e) => e.transform(t)),
912
+ { ignoreChecks: !0 }
913
+ );
914
+ }
915
+ }
916
+ const Ut = (n, t) => {
917
+ const e = K(t, {
918
+ V: [1, 0],
919
+ firstPoint: n,
920
+ precision: t.precision
921
+ });
922
+ if (e === "parallel")
923
+ return 0;
924
+ const { intersectionParam1: r, intersectionParam2: s } = e;
925
+ if (!t.isValidParameter(r) || s <= -t.precision)
926
+ return 0;
927
+ if (Math.abs(r) < t.precision || Math.abs(r - 1) < t.precision) {
928
+ const [, i] = t.midPoint;
929
+ return n[1] - i < 0 ? 1 : 0;
930
+ }
931
+ return 1;
932
+ }, Wt = (n, t) => {
933
+ const e = t.precision, r = Math.abs(n[1] - t.center[1]);
934
+ if (r > t.radius + e)
935
+ return 0;
936
+ const s = j(n, t.center), i = t.radius * t.radius, o = e * e;
937
+ if (Math.abs(s - i) < o)
938
+ return 0;
939
+ const a = s - i > o;
940
+ if (a && t.center[0] < n[0])
941
+ return 0;
942
+ const u = Math.sqrt(
943
+ t.radius * t.radius - r * r
944
+ );
945
+ let l = 0;
946
+ const c = (f) => {
947
+ t.isOnSegment(f) && (h(f, t.firstPoint) ? l += t.tangentAtFirstPoint[1] > 0 ? 1 : 0 : h(f, t.lastPoint) ? l += t.tangentAtLastPoint[1] > 0 ? 0 : 1 : l += 1);
948
+ };
949
+ return c([t.center[0] + u, n[1]]), a && c([t.center[0] - u, n[1]]), l;
950
+ };
951
+ function Xt(n, t) {
952
+ if (t instanceof P)
953
+ return Ut(n, t);
954
+ if (t instanceof m)
955
+ return Wt(n, t);
956
+ throw new Error("Not implemented");
957
+ }
958
+ class E extends bt {
959
+ constructor(t, { ignoreChecks: e = !1 } = {}) {
960
+ super(t, { ignoreChecks: !0 }), this.strokeType = "LOOP", e || Ht(t);
961
+ }
962
+ clone() {
963
+ return new E(
964
+ this.segments.map((t) => t.clone()),
965
+ { ignoreChecks: !0 }
966
+ );
967
+ }
968
+ reverse() {
969
+ const t = this.segments.map((e) => e.reverse());
970
+ return t.reverse(), new E(t, { ignoreChecks: !0 });
971
+ }
972
+ transform(t) {
973
+ return new E(
974
+ this.segments.map((e) => e.transform(t)),
975
+ { ignoreChecks: !0 }
976
+ );
977
+ }
978
+ contains(t) {
979
+ return this.onStroke(t) || !this.boundingBox.contains(t) ? !1 : this.segments.reduce((r, s) => r + Xt(t, s), 0) % 2 === 1;
980
+ }
981
+ simplify() {
982
+ const t = vt(this);
983
+ return t ? new E(t, { ignoreChecks: !0 }) : this;
984
+ }
985
+ }
986
+ function Ht(n) {
987
+ if (At(n, "Loop"), !h(n[0].firstPoint, n[n.length - 1].lastPoint))
988
+ throw new Error("Loop segment must be closed");
989
+ }
990
+ const mt = (n, t) => {
991
+ const e = n.findIndex((i) => h(t, i.firstPoint)), r = n.slice(0, e);
992
+ return n.slice(e).concat(r);
993
+ }, Pt = (n, t) => {
994
+ let e = n;
995
+ const r = (a) => h(a.firstPoint, t.firstPoint) && h(a.lastPoint, t.lastPoint);
996
+ let s = n.findIndex(r);
997
+ if (s === -1) {
998
+ const a = n.map((u) => u.reverse());
999
+ if (a.reverse(), s = a.findIndex(r), s === -1)
1000
+ throw console.error(
1001
+ a.map((u) => u.repr),
1002
+ t.repr
1003
+ ), new Error("Failed to rotate to segment start");
1004
+ e = a;
1005
+ }
1006
+ const i = e.slice(0, s);
1007
+ return e.slice(s).concat(i);
1008
+ };
1009
+ function* pt(n, t, e) {
1010
+ const r = (o) => t.some((a) => h(a, o.lastPoint)), s = (o) => e.some((a) => o.isSame(a));
1011
+ let i = [];
1012
+ for (const o of n)
1013
+ r(o) ? (i.push(o), yield new A(i, { ignoreChecks: !0 }), i = []) : s(o) ? (i.length && (yield new A(i, { ignoreChecks: !0 }), i = []), yield new A([o], { ignoreChecks: !0 })) : i.push(o);
1014
+ i.length && (yield new A(i, { ignoreChecks: !0 }));
1015
+ }
1016
+ function Yt(n, t, e) {
1017
+ return n.filter((r) => {
1018
+ const s = t.filter((a) => h(a.firstPoint, r) || h(a.lastPoint, r));
1019
+ if (s.length % 2)
1020
+ throw new Error("Bug in the intersection algo on non crossing point");
1021
+ const i = s.map((a) => e.contains(a.midPoint));
1022
+ return !(i.every((a) => a) || !i.some((a) => a));
1023
+ });
1024
+ }
1025
+ function zt(n, t, e) {
1026
+ let r = [];
1027
+ const s = [], i = new Array(n.segments.length).fill(0).map(() => []), o = new Array(t.segments.length).fill(0).map(() => []);
1028
+ if (n.segments.forEach((g, p) => {
1029
+ t.segments.forEach((d, w) => {
1030
+ const { intersections: x, overlaps: C } = it(
1031
+ g,
1032
+ d,
1033
+ e
1034
+ );
1035
+ r.push(...x), i[p].push(...x), o[w].push(...x), s.push(...C);
1036
+ const y = C.flatMap((T) => [
1037
+ T.firstPoint,
1038
+ T.lastPoint
1039
+ ]);
1040
+ r.push(...y), i[p].push(...y), o[w].push(...y);
1041
+ });
1042
+ }), r = J(r, e), !r.length || r.length === 1)
1043
+ return null;
1044
+ const a = ([g, p]) => p.length ? g.splitAt(p) : [g];
1045
+ let u = F([n.segments, i]).flatMap(a), l = F([t.segments, o]).flatMap(a);
1046
+ if (r = Yt(
1047
+ r,
1048
+ u,
1049
+ t
1050
+ ), !r.length && !s.length)
1051
+ return null;
1052
+ if (s.length) {
1053
+ const g = s[0];
1054
+ u = Pt(
1055
+ u,
1056
+ g
1057
+ ), l = Pt(
1058
+ l,
1059
+ g
1060
+ );
1061
+ } else {
1062
+ const g = r[0];
1063
+ u = mt(u, g), l = mt(l, g);
1064
+ }
1065
+ const c = Array.from(
1066
+ pt(
1067
+ u,
1068
+ r,
1069
+ s
1070
+ )
1071
+ );
1072
+ let f = Array.from(
1073
+ pt(
1074
+ l,
1075
+ r,
1076
+ s
1077
+ )
1078
+ );
1079
+ return (!h(
1080
+ f[0].lastPoint,
1081
+ c[0].lastPoint
1082
+ ) || s.length > 0 && f[0].segmentsCount !== 1) && (f = f.reverse().map((g) => g.reverse())), F([c, f]).map(([g, p]) => g.segmentsCount === 1 && s.some((d) => g.segments[0].isSame(d)) ? [g, "same"] : [g, p]);
1083
+ }
1084
+ function Z(n) {
1085
+ let t = n[0];
1086
+ for (const e of n.slice(1))
1087
+ t = t.extend(e);
1088
+ if (!h(t.firstPoint, t.lastPoint))
1089
+ throw console.error(
1090
+ L(t.firstPoint),
1091
+ L(t.lastPoint)
1092
+ ), new Error("Bug in the intersection algo on non closing strand");
1093
+ return new E(t.segments);
1094
+ }
1095
+ function Jt(n) {
1096
+ if (!n.length)
1097
+ return [];
1098
+ const t = n.map((o) => o.firstPoint);
1099
+ let e = n.map((o) => o.lastPoint);
1100
+ e = e.slice(-1).concat(e.slice(0, -1));
1101
+ const r = F([t, e]).flatMap(
1102
+ ([o, a], u) => h(o, a) ? [] : u
1103
+ );
1104
+ if (!r.length)
1105
+ return [Z(n)];
1106
+ const s = F([
1107
+ r.slice(0, -1),
1108
+ r.slice(1)
1109
+ ]).map(([o, a]) => Z(n.slice(o, a)));
1110
+ let i = n.slice(
1111
+ r[r.length - 1]
1112
+ );
1113
+ return r[0] !== 0 && (i = i.concat(n.slice(0, r[0]))), s.push(Z(i)), s;
1114
+ }
1115
+ const dt = (n, t) => {
1116
+ if (n.length === 0)
1117
+ return [t];
1118
+ const e = n.at(-1);
1119
+ return h(e.lastPoint, t.firstPoint) ? n.slice(0, -1).concat([e.extend(t)]) : h(e.lastPoint, t.lastPoint) ? n.slice(0, -1).concat([e.extend(t.reverse())]) : n.concat([t]);
1120
+ }, Kt = (n, t) => n.length === 0 ? [t] : h(n[0].firstPoint, t.lastPoint) ? [t.extend(n[0])].concat(n.slice(1)) : [t].concat(n);
1121
+ function ot(n, t, {
1122
+ firstInside: e,
1123
+ secondInside: r
1124
+ }) {
1125
+ const s = zt(n, t);
1126
+ if (!s) {
1127
+ const u = n.segments[0].midPoint, l = t.contains(u), c = t.segments[0].midPoint, f = n.contains(c);
1128
+ return {
1129
+ identical: !1,
1130
+ firstCurveInSecond: l,
1131
+ secondCurveInFirst: f
1132
+ };
1133
+ }
1134
+ if (s.every(([, u]) => u === "same"))
1135
+ return { identical: !0 };
1136
+ let i = null, o = null;
1137
+ const a = s.flatMap(([u, l]) => {
1138
+ let c = [], f = 0;
1139
+ if (l === "same")
1140
+ return o === 1 ? (o = 1, u) : o === 2 || o === 0 ? (o = null, []) : o === null ? (i ? i = i.extend(u) : i = u, []) : (console.error("weird situation"), []);
1141
+ const g = u.segments[0].midPoint, p = t.contains(g);
1142
+ (e === "keep" && p || e === "remove" && !p) && (f += 1, c = dt(c, u));
1143
+ const d = l.segments[0].midPoint, w = n.contains(d);
1144
+ if (r === "keep" && w || r === "remove" && !w) {
1145
+ const x = l;
1146
+ f += 1, f === 2 && c.length ? (c = dt(c, x), i = null) : c = [x];
1147
+ }
1148
+ return o === null && f === 1 && i && (c = Kt(c, i)), f === 1 && (o = f, i = null), c.length ? c : (i = null, []);
1149
+ });
1150
+ return Jt(a);
1151
+ }
1152
+ const Qt = (n, t) => {
1153
+ const e = ot(n, t, {
1154
+ firstInside: "remove",
1155
+ secondInside: "remove"
1156
+ });
1157
+ return Array.isArray(e) ? e : e.identical ? [n] : e.firstCurveInSecond ? [t] : e.secondCurveInFirst ? [n] : [n, t];
1158
+ }, H = (n, t) => {
1159
+ const e = ot(n, t, {
1160
+ firstInside: "remove",
1161
+ secondInside: "keep"
1162
+ });
1163
+ return Array.isArray(e) ? e : e.identical ? [] : e.firstCurveInSecond ? [] : e.secondCurveInFirst ? [n, t] : [n];
1164
+ }, at = (n, t) => {
1165
+ const e = ot(n, t, {
1166
+ firstInside: "keep",
1167
+ secondInside: "keep"
1168
+ });
1169
+ return Array.isArray(e) ? e : e.identical ? [n] : e.firstCurveInSecond ? [n] : e.secondCurveInFirst ? [t] : [];
1170
+ };
1171
+ function Zt(n) {
1172
+ const t = /* @__PURE__ */ new Map(), e = [];
1173
+ return n.forEach((r, s) => {
1174
+ let i;
1175
+ t.has(s) ? i = t.get(s) : (i = { current: [r], fusedWith: /* @__PURE__ */ new Set([s]) }, e.push(i)), n.slice(s + 1).forEach((o, a) => {
1176
+ const u = i.current, l = s + a + 1;
1177
+ if (i.fusedWith.has(l))
1178
+ return;
1179
+ let c = [o], f = !1;
1180
+ if (t.has(l) && (c = t.get(l).current, f = !0), !u.some(
1181
+ (d) => c.some((w) => d.intersects(w))
1182
+ ))
1183
+ return;
1184
+ let p;
1185
+ u.length > 1 || c.length > 1 ? p = N(u, c) : p = kt(u[0], c[0]), i.fusedWith.add(l), i.current = p, f || t.set(l, i);
1186
+ });
1187
+ }), e.flatMap(({ current: r }) => r);
1188
+ }
1189
+ function kt(n, t) {
1190
+ const e = Qt(n.contour, t.contour), r = t.holes.flatMap((o) => H(o, n.contour)), s = n.holes.flatMap((o) => H(o, t.contour)), i = _t(n.holes, t.holes).flatMap(
1191
+ ([o, a]) => at(o, a)
1192
+ );
1193
+ return D([
1194
+ ...e,
1195
+ ...r,
1196
+ ...s,
1197
+ ...i
1198
+ ]);
1199
+ }
1200
+ function Y(n, t) {
1201
+ if (n.isFull && t.isFull)
1202
+ return D(H(n.contour, t.contour));
1203
+ if (n.isFull) {
1204
+ const r = H(n.contour, t.contour), s = t.holes.flatMap(
1205
+ (i) => at(i, n.contour)
1206
+ );
1207
+ return D([...r, ...s]);
1208
+ } else if (t.isFull && !n.contour.intersects(t.contour))
1209
+ if (n.contour.contains(t.contour.firstPoint)) {
1210
+ const r = N(
1211
+ n.holes.map((s) => new b(s)),
1212
+ [t]
1213
+ );
1214
+ return D([
1215
+ n.contour,
1216
+ ...r.flatMap((s) => s.allLoops)
1217
+ ]);
1218
+ } else
1219
+ return [n];
1220
+ let e = Y(new b(n.contour), t);
1221
+ return n.holes.forEach((r) => {
1222
+ e = e.flatMap((s) => Y(s, new b(r)));
1223
+ }), e;
1224
+ }
1225
+ function tn(n, t) {
1226
+ const e = at(n.contour, t.contour);
1227
+ if (!e.length)
1228
+ return [];
1229
+ let r = D(e);
1230
+ return r = R(
1231
+ r,
1232
+ n.holes.map((s) => new b(s))
1233
+ ), R(
1234
+ r,
1235
+ t.holes.map((s) => new b(s))
1236
+ );
1237
+ }
1238
+ function N(n, t) {
1239
+ if (!n.length)
1240
+ return t;
1241
+ if (!t.length)
1242
+ return n;
1243
+ if (n.length === 1 && t.length > 1 || t.length === 1 && n.length > 1)
1244
+ return Zt([...n, ...t]);
1245
+ if (n.length > 1 && t.length > 1) {
1246
+ let e = N([n[0]], t);
1247
+ return n.slice(1).forEach((r) => {
1248
+ e = N([r], e);
1249
+ }), e;
1250
+ }
1251
+ return n.length === 1 && t.length === 1 ? kt(n[0], t[0]) : [];
1252
+ }
1253
+ function R(n, t) {
1254
+ if (!n.length)
1255
+ return [];
1256
+ if (!t.length)
1257
+ return n;
1258
+ if (n.length === 1 && t.length === 1)
1259
+ return Y(n[0], t[0]);
1260
+ if (n.length > 1)
1261
+ return n.flatMap((r) => R([r], t));
1262
+ let e = Y(n[0], t[0]);
1263
+ return t.slice(1).forEach((r) => {
1264
+ e = R(e, [r]);
1265
+ }), e;
1266
+ }
1267
+ function et(n, t) {
1268
+ return !n.length || !t.length ? [] : n.length === 1 && t.length === 1 ? tn(n[0], t[0]) : n.length > 1 ? n.flatMap((e) => et([e], t)) : t.flatMap((e) => et(n, [e]));
1269
+ }
1270
+ class V extends z {
1271
+ constructor(t = [], { ignoreChecks: e = !1 } = {}) {
1272
+ super(), this._boundingBox = null, e || nn(t), this.figures = t;
1273
+ }
1274
+ get isEmpty() {
1275
+ return this.figures.length === 0;
1276
+ }
1277
+ get boundingBox() {
1278
+ if (this.isEmpty)
1279
+ return new q();
1280
+ if (this._boundingBox === null) {
1281
+ let t = this.figures[0].boundingBox;
1282
+ for (const e of this.figures.slice(1))
1283
+ t = t.merge(e.boundingBox);
1284
+ this._boundingBox = t;
1285
+ }
1286
+ return this._boundingBox;
1287
+ }
1288
+ clone() {
1289
+ return new V(this.figures.map((t) => t.clone()));
1290
+ }
1291
+ transform(t) {
1292
+ return new V(this.figures.map((e) => e.transform(t)));
1293
+ }
1294
+ contains(t) {
1295
+ return this.figures.some((e) => e.contains(t));
1296
+ }
1297
+ intersects(t) {
1298
+ return this.figures.some(
1299
+ (e) => t.figures.some((r) => e.intersects(r))
1300
+ );
1301
+ }
1302
+ fuse(t) {
1303
+ return new V(N(this.figures, t.figures));
1304
+ }
1305
+ cut(t) {
1306
+ return new V(R(this.figures, t.figures));
1307
+ }
1308
+ intersect(t) {
1309
+ return new V(et(this.figures, t.figures));
1310
+ }
1311
+ }
1312
+ function nn(n) {
1313
+ for (const [t, e] of tt(n))
1314
+ if (t.intersects(e))
1315
+ throw new Error("Diagram figures must not intersect");
1316
+ }
1317
+ function Ct(n) {
1318
+ if (n instanceof P)
1319
+ return {
1320
+ type: n.segmentType,
1321
+ firstPoint: n.firstPoint,
1322
+ lastPoint: n.lastPoint
1323
+ };
1324
+ if (n instanceof m)
1325
+ return {
1326
+ type: n.segmentType,
1327
+ firstPoint: n.firstPoint,
1328
+ lastPoint: n.lastPoint,
1329
+ center: n.center,
1330
+ clockwise: n.clockwise
1331
+ };
1332
+ throw new Error("Unknown segment type");
1333
+ }
1334
+ function rt(n) {
1335
+ return {
1336
+ type: "LOOP",
1337
+ segments: n.segments.map(Ct)
1338
+ };
1339
+ }
1340
+ function It(n) {
1341
+ return {
1342
+ type: "FIGURE",
1343
+ contour: rt(n.contour),
1344
+ holes: n.holes.map(rt)
1345
+ };
1346
+ }
1347
+ function en(n) {
1348
+ return {
1349
+ type: "DIAGRAM",
1350
+ figures: n.figures.map(It)
1351
+ };
1352
+ }
1353
+ function wt(n) {
1354
+ if (n instanceof V)
1355
+ return en(n);
1356
+ if (n instanceof b)
1357
+ return It(n);
1358
+ if (n instanceof E)
1359
+ return rt(n);
1360
+ if (n instanceof m || n instanceof P)
1361
+ return Ct(n);
1362
+ throw new Error("Unknown shape type");
1363
+ }
1364
+ export {
1365
+ m as A,
1366
+ q as B,
1367
+ V as D,
1368
+ b as F,
1369
+ P as L,
1370
+ A as S,
1371
+ I as T,
1372
+ M as a,
1373
+ v as b,
1374
+ sn as c,
1375
+ k as d,
1376
+ S as e,
1377
+ E as f,
1378
+ U as g,
1379
+ Vt as h,
1380
+ N as i,
1381
+ R as j,
1382
+ et as k,
1383
+ wt as l,
1384
+ B as p,
1385
+ h as s,
1386
+ rn as t
1387
+ };
1388
+ //# sourceMappingURL=exportJSON-b3e1c256.js.map