pantograph2d 0.3.0 → 0.3.1

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