pantograph2d 0.7.0 → 0.8.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.
- package/LICENSE +4 -658
- package/dist/QuadraticBezier-BAVasVfu.cjs +9 -0
- package/dist/QuadraticBezier-BAVasVfu.cjs.map +1 -0
- package/dist/QuadraticBezier-BzVqiExF.js +4736 -0
- package/dist/QuadraticBezier-BzVqiExF.js.map +1 -0
- package/dist/draw-DqsKIWJD.js +337 -0
- package/dist/draw-DqsKIWJD.js.map +1 -0
- package/dist/draw-NcYfDZjT.cjs +2 -0
- package/dist/draw-NcYfDZjT.cjs.map +1 -0
- package/dist/models-Dxwusy3i.cjs +4 -0
- package/dist/models-Dxwusy3i.cjs.map +1 -0
- package/dist/models-WQ-B7GSL.js +1749 -0
- package/dist/models-WQ-B7GSL.js.map +1 -0
- package/dist/pantograph/drawShape.cjs +1 -1
- package/dist/pantograph/drawShape.cjs.map +1 -1
- package/dist/pantograph/drawShape.js +12 -13
- package/dist/pantograph/drawShape.js.map +1 -1
- package/dist/pantograph/models.cjs +1 -1
- package/dist/pantograph/models.js +15 -12
- package/dist/pantograph/models.js.map +1 -1
- package/dist/pantograph/svg.cjs +2 -0
- package/dist/pantograph/svg.cjs.map +1 -0
- package/dist/pantograph/svg.js +10 -0
- package/dist/pantograph/svg.js.map +1 -0
- package/dist/pantograph.cjs +2 -8
- package/dist/pantograph.cjs.map +1 -1
- package/dist/pantograph.js +406 -452
- package/dist/pantograph.js.map +1 -1
- package/dist/svg-BylQQgsL.js +62 -0
- package/dist/svg-BylQQgsL.js.map +1 -0
- package/dist/svg-D0nYyMk7.cjs +8 -0
- package/dist/svg-D0nYyMk7.cjs.map +1 -0
- package/dist/types/examples/importSVG.d.ts +4 -0
- package/dist/types/{algorithms → src/algorithms}/boolean/figureBooleans.d.ts +1 -1
- package/dist/types/{algorithms → src/algorithms}/boolean/loopBooleans.d.ts +1 -1
- package/dist/types/{algorithms → src/algorithms}/boolean/strandBoolean.d.ts +4 -4
- package/dist/types/{algorithms → src/algorithms}/boolean/strandsBetweenIntersections.d.ts +3 -3
- package/dist/types/{algorithms → src/algorithms}/distances/arcArcDistance.d.ts +1 -1
- package/dist/types/{algorithms → src/algorithms}/distances/genericDistance.d.ts +1 -1
- package/dist/types/{algorithms → src/algorithms}/distances/index.d.ts +1 -1
- package/dist/types/src/algorithms/distances/lineArcDistance.d.ts +3 -0
- package/dist/types/{algorithms → src/algorithms}/distances/lineLineDistance.d.ts +1 -1
- package/dist/types/{algorithms → src/algorithms}/filletSegments.d.ts +1 -1
- package/dist/types/{algorithms → src/algorithms}/intersections/arcArcIntersection.d.ts +2 -2
- package/dist/types/src/algorithms/intersections/arcEllipseArcIntersection.d.ts +3 -0
- package/dist/types/src/algorithms/intersections/arcsCubicBezierIntersection.d.ts +5 -0
- package/dist/types/src/algorithms/intersections/arcsQuadraticBezierIntersection.d.ts +5 -0
- package/dist/types/{algorithms → src/algorithms}/intersections/bezierClip.d.ts +3 -3
- package/dist/types/{algorithms → src/algorithms}/intersections/cubicBezierCubicBezierIntersection.d.ts +2 -2
- package/dist/types/{algorithms → src/algorithms}/intersections/ellipseArcEllipseArcIntersection.d.ts +2 -2
- package/dist/types/src/algorithms/intersections/ellipseEllipseIntersection.d.ts +4 -0
- package/dist/types/{algorithms → src/algorithms}/intersections/index.d.ts +2 -2
- package/dist/types/src/algorithms/intersections/lineArcIntersection.d.ts +4 -0
- package/dist/types/src/algorithms/intersections/lineBezierIntersection.d.ts +5 -0
- package/dist/types/src/algorithms/intersections/lineEllipseArcIntersection.d.ts +4 -0
- package/dist/types/{algorithms → src/algorithms}/intersections/lineLineIntersection.d.ts +4 -4
- package/dist/types/{algorithms → src/algorithms}/intersections/quadraticBezierQuadraticBezierIntersection.d.ts +2 -2
- package/dist/types/src/algorithms/intersections/rayIntersections.d.ts +3 -0
- package/dist/types/{algorithms → src/algorithms}/offsets/offsetFigure.d.ts +2 -2
- package/dist/types/{algorithms → src/algorithms}/offsets/offsetSegment.d.ts +4 -4
- package/dist/types/{algorithms → src/algorithms}/offsets/offsetStroke.d.ts +5 -5
- package/dist/types/{algorithms → src/algorithms}/organiseLoops.d.ts +2 -2
- package/dist/types/src/algorithms/simplify.d.ts +3 -0
- package/dist/types/src/algorithms/solvers/findZeroViaNewton.d.ts +1 -0
- package/dist/types/src/algorithms/solvers/zeroViaNewton.d.ts +1 -0
- package/dist/types/{algorithms → src/algorithms}/stitchSegments.d.ts +1 -1
- package/dist/types/src/api/drawShape.d.ts +2 -0
- package/dist/types/src/api/models.d.ts +1 -0
- package/dist/types/src/api/svg.d.ts +1 -0
- package/dist/types/{booleanOperations.d.ts → src/booleanOperations.d.ts} +5 -5
- package/dist/types/{draw.d.ts → src/draw.d.ts} +4 -4
- package/dist/types/{drawShape → src/drawShape}/drawCircle.d.ts +1 -1
- package/dist/types/src/drawShape/drawEllipse.d.ts +2 -0
- package/dist/types/{drawShape → src/drawShape}/drawRect.d.ts +1 -1
- package/dist/types/src/drawShape/drawSVGPath.d.ts +2 -0
- package/dist/types/src/drawShape/index.d.ts +4 -0
- package/dist/types/{export → src/export}/json/exportJSON.d.ts +94 -94
- package/dist/types/{export → src/export}/json/jsonDiagram.d.ts +31 -31
- package/dist/types/{export → src/export}/json/jsonFigure.d.ts +31 -31
- package/dist/types/{export → src/export}/json/jsonLoop.d.ts +16 -16
- package/dist/types/{export → src/export}/json/jsonSegment.d.ts +16 -16
- package/dist/types/src/export/svg/api.d.ts +6 -0
- package/dist/types/{export → src/export}/svg/exportSVG.d.ts +6 -6
- package/dist/types/{export → src/export}/svg/svgDiagram.d.ts +1 -1
- package/dist/types/{export → src/export}/svg/svgFigure.d.ts +1 -1
- package/dist/types/{export → src/export}/svg/svgLoop.d.ts +1 -1
- package/dist/types/{export → src/export}/svg/svgSegment.d.ts +1 -1
- package/dist/types/{export → src/export}/svg/svgStrand.d.ts +1 -1
- package/dist/types/{export → src/export}/svg/wrapSVG.d.ts +2 -2
- package/dist/types/src/import/json/importJSON.d.ts +9 -0
- package/dist/types/{main.d.ts → src/main.d.ts} +7 -7
- package/dist/types/{models → src/models}/BoundingBox.d.ts +1 -1
- package/dist/types/{models → src/models}/Diagram.d.ts +7 -7
- package/dist/types/{models → src/models}/Figure.d.ts +7 -7
- package/dist/types/{models → src/models}/Loop.d.ts +4 -4
- package/dist/types/{models → src/models}/Strand.d.ts +2 -2
- package/dist/types/{models → src/models}/Stroke.d.ts +5 -5
- package/dist/types/{models → src/models}/TransformationMatrix.d.ts +1 -1
- package/dist/types/src/models/exports.d.ts +15 -0
- package/dist/types/src/models/segments/AbstractSegment.d.ts +25 -0
- package/dist/types/{models → src/models}/segments/Arc.d.ts +4 -4
- package/dist/types/{models → src/models}/segments/CubicBezier.d.ts +4 -4
- package/dist/types/{models → src/models}/segments/EllipseArc.d.ts +5 -5
- package/dist/types/{models → src/models}/segments/Line.d.ts +4 -4
- package/dist/types/{models → src/models}/segments/QuadraticBezier.d.ts +4 -4
- package/dist/types/{models → src/models}/segments/Segment.d.ts +4 -4
- package/dist/types/{models → src/models}/segments/utils/deCasteljau.d.ts +1 -1
- package/dist/types/src/models/segments/utils/isSegment.d.ts +8 -0
- package/dist/types/{models → src/models}/utils/Transformable.d.ts +2 -2
- package/dist/types/{offsetOperations.d.ts → src/offsetOperations.d.ts} +4 -4
- package/dist/types/src/operations.d.ts +2 -0
- package/dist/types/src/utils/applyMixins.d.ts +1 -0
- package/dist/types/src/utils/listOfFigures.d.ts +4 -0
- package/dist/types/src/utils/projectPointOnLine.d.ts +3 -0
- package/dist/types/{utils → src/utils}/removeDuplicatePoints.d.ts +1 -1
- package/dist/types/{vectorOperations.d.ts → src/vectorOperations.d.ts} +1 -1
- package/package.json +33 -25
- package/dist/Diagram-8c3c5e1b.js +0 -5301
- package/dist/Diagram-8c3c5e1b.js.map +0 -1
- package/dist/Diagram-d8288579.cjs +0 -11
- package/dist/Diagram-d8288579.cjs.map +0 -1
- package/dist/draw-3ee546c3.cjs +0 -2
- package/dist/draw-3ee546c3.cjs.map +0 -1
- package/dist/draw-825692f1.js +0 -336
- package/dist/draw-825692f1.js.map +0 -1
- package/dist/types/algorithms/distances/lineArcDistance.d.ts +0 -3
- package/dist/types/algorithms/intersections/arcEllipseArcIntersection.d.ts +0 -3
- package/dist/types/algorithms/intersections/arcsCubicBezierIntersection.d.ts +0 -5
- package/dist/types/algorithms/intersections/arcsQuadraticBezierIntersection.d.ts +0 -5
- package/dist/types/algorithms/intersections/ellipseEllipseIntersection.d.ts +0 -4
- package/dist/types/algorithms/intersections/lineArcIntersection.d.ts +0 -4
- package/dist/types/algorithms/intersections/lineBezierIntersection.d.ts +0 -5
- package/dist/types/algorithms/intersections/lineEllipseArcIntersection.d.ts +0 -4
- package/dist/types/algorithms/intersections/rayIntersections.d.ts +0 -3
- package/dist/types/algorithms/simplify.d.ts +0 -3
- package/dist/types/api/drawShape.d.ts +0 -2
- package/dist/types/api/models.d.ts +0 -1
- package/dist/types/import/json/importJSON.d.ts +0 -9
- package/dist/types/models/exports.d.ts +0 -13
- package/dist/types/models/segments/utils/isSegment.d.ts +0 -8
- package/dist/types/operations.d.ts +0 -2
- package/dist/types/utils/listOfFigures.d.ts +0 -4
- package/dist/types/utils/projectPointOnLine.d.ts +0 -3
- /package/dist/types/{algorithms → src/algorithms}/optimisation/Brent.d.ts +0 -0
- /package/dist/types/{algorithms → src/algorithms}/optimisation/DiRect.d.ts +0 -0
- /package/dist/types/{algorithms → src/algorithms}/solvers/solvePolynomials.d.ts +0 -0
- /package/dist/types/{definitions.d.ts → src/definitions.d.ts} +0 -0
- /package/dist/types/{utils → src/utils}/allCombinations.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/allPairs.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/angularDistance.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/range.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/removeDuplicateValues.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/unitAngle.d.ts +0 -0
- /package/dist/types/{utils → src/utils}/zip.d.ts +0 -0
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";function Yt(i,t,n,e){return i<=e&&t>=n}class ct{constructor(t=1/0,n=1/0,e=-1/0,s=-1/0){this.xMin=t,this.yMin=n,this.xMax=e,this.yMax=s}get width(){return this.xMax-this.xMin}get height(){return this.yMax-this.yMin}get center(){return[(this.xMin+this.xMax)/2,(this.yMin+this.yMax)/2]}grow(t){return new ct(this.xMin-t,this.yMin-t,this.xMax+t,this.yMax+t)}contains(t){const[n,e]=t;return Yt(this.xMin,this.xMax,n,n)&&Yt(this.yMin,this.yMax,e,e)}overlaps(t){return Yt(this.xMin,this.xMax,t.xMin,t.xMax)&&Yt(this.yMin,this.yMax,t.yMin,t.yMax)}addPoint(t){const[n,e]=t;return new ct(Math.min(this.xMin,n),Math.min(this.yMin,e),Math.max(this.xMax,n),Math.max(this.yMax,e))}merge(t){return new ct(Math.min(this.xMin,t.xMin),Math.min(this.yMin,t.yMin),Math.max(this.xMax,t.xMax),Math.max(this.yMax,t.yMax))}intersection(t){return new ct(Math.max(this.xMin,t.xMin),Math.max(this.yMin,t.yMin),Math.min(this.xMax,t.xMax),Math.min(this.yMax,t.yMax))}}function we(i){return i.reduce((t,n)=>t.addPoint(n),new ct)}const xe=(i,t=1e-9)=>{let n=i;return Math.abs(i)<t&&(n=0),n.toFixed(-Math.log10(t))};function pt(i,t=1e-9){return Array.from(new Map(i.map(([n,e])=>[`[${xe(n,t)},${xe(e,t)}]`,[n,e]])).values())}const ee=Math.PI/180,tn=180/Math.PI,W=i=>`[${i[0]}, ${i[1]}]`,b=([i,t],[n,e],s=1e-9)=>Math.abs(i-n)<=s&&Math.abs(t-e)<=s,J=([i,t],[n,e])=>[i+n,t+e],$=([i,t],[n,e])=>[i-n,t-e],ut=([i,t])=>i*i+t*t,bn=([i,t])=>Math.sqrt(ut([i,t])),nt=([i,t],n)=>[i*n,t*n],yt=([i,t],[n,e]=[0,0])=>(i-n)**2+(t-e)**2,G=(i,t=[0,0])=>Math.sqrt(yt(i,t));function Pt([i,t],[n,e]){return i*e-t*n}function ae([i,t],[n,e]){return i*n+t*e}function Q([i,t]){const n=G([i,t]);return[i/n,t/n]}function Ct(i,t){const n=Math.cos(t)*i,e=Math.sin(t)*i;return[n,e]}function en([i,t]){return Math.atan2(t,i)}function nn(i){const t=G(i),n=en(i);return[t,n]}function de(i,t,n=1e-9){const e=Pt(i,t),s=ut(i),r=ut(t);return e*e<s*r*n*n}function et(i){return[-i[1],i[0]]}function Tt(i){return[i[1],-i[0]]}function En(i,t,n){return b(n,i)||de($(n,i),t)}const St=(i,t)=>{const[n,e,s,r,o,l,a,h,c]=i,[u,f,p,g,w,P,m,d,y]=t;return[n*u+e*g+s*m,n*f+e*w+s*d,n*p+e*P+s*y,r*u+o*g+l*m,r*f+o*w+l*d,r*p+o*P+l*y,a*u+h*g+c*m,a*f+h*w+c*d,a*p+h*P+c*y]},Sn=i=>{const[t,n,e,s,r,o,l,a,h]=i,c=t*(r*h-o*a)-n*(s*h-o*l)+e*(s*a-r*l);return[(r*h-o*a)/c,(e*a-n*h)/c,(n*o-e*r)/c,(o*l-s*h)/c,(t*h-e*l)/c,(e*s-t*o)/c,(s*a-r*l)/c,(n*l-t*a)/c,(t*r-n*s)/c]},_n=i=>{const[t,n,e,s,r,o,l,a,h]=i;return[t,s,l,n,r,a,e,o,h]};class H{constructor(t){this._matrix=[1,0,0,0,1,0,0,0,1],t&&(this._matrix=[...t])}clone(){return new H(this._matrix)}transpose(){return this._matrix=_n(this._matrix),this}inverse(){return this._matrix=Sn(this._matrix),this}translate(t,n){return this._matrix=St(this._matrix,[1,0,t,0,1,n,0,0,1]),this}rotate(t,n){const e=Math.cos(t),s=Math.sin(t),r=[e,-s,0,s,e,0,0,0,1];return n&&this.translate(n[0],n[1]),this._matrix=St(this._matrix,r),n&&this.translate(-n[0],-n[1]),this}mirrorX(){return this._matrix=St(this._matrix,[1,0,0,0,-1,0,0,0,1]),this}mirrorY(){return this._matrix=St(this._matrix,[-1,0,0,0,1,0,0,0,1]),this}mirrorLine(t,n){const[e,s]=t,r=Math.atan2(s,e);return n&&this.translate(n[0],n[1]),this.rotate(r),this.mirrorX(),this.rotate(-r),n&&this.translate(-n[0],-n[1]),this}mirrorCenter(t){return t&&this.translate(t[0],t[1]),this._matrix=St(this._matrix,[-1,0,0,0,-1,0,0,0,1]),t&&this.translate(-t[0],-t[1]),this}scale(t,n){return n&&this.translate(n[0],n[1]),this._matrix=St(this._matrix,[t,0,0,0,t,0,0,0,1]),n&&this.translate(-n[0],-n[1]),this}transform(t){const[n,e]=t,[s,r,o,l,a,h]=this._matrix;return[s*n+r*e+o,l*n+a*e+h]}transformAngle(t){const[n,e]=this.transform([Math.cos(t),Math.sin(t)]),[s,r]=this.transform([0,0]);return Math.atan2(e-r,n-s)}keepsOrientation(){const[t,,,,n]=this._matrix;return t*n>0}scaleFactor(){const[t,,,n]=this._matrix;return Math.sqrt(t*t+n*n)}}class Bt{translateX(t){const n=new H().translate(t,0);return this.transform(n)}translateY(t){const n=new H().translate(0,t);return this.transform(n)}translate(t,n){const e=new H().translate(t,n);return this.transform(e)}translateTo([t,n]){const e=new H().translate(t,n);return this.transform(e)}rotate(t,n){const e=new H().rotate(t*ee,n);return this.transform(e)}scale(t,n){const e=new H().scale(t,n);return this.transform(e)}mirrorCenter(t){const n=new H().mirrorCenter(t);return this.transform(n)}mirror(t="x",n){const e=new H;return t==="x"?e.mirrorX():t==="y"?e.mirrorY():e.mirrorLine(t,n),this.transform(e)}}class Ot extends Bt{constructor(t,n){super(),this.firstPoint=t,this.lastPoint=n,this.precision=1e-9,this.firstPoint=t,this.lastPoint=n}get repr(){return`${this.segmentType} ${W(this.firstPoint)} - ${W(this.lastPoint)}`}get info(){return this.repr}[Symbol.for("nodejs.util.inspect.custom")](){return this.repr}}class B extends Ot{constructor(){super(...arguments),this.segmentType="LINE",this._V=null,this._slope=null,this._yIntercept=null,this._boundingBox=null}isValidParameter(t){const n=this.length*this.precision;return t>=-n&&1-t>=-n}paramPoint(t){return J(this.firstPoint,nt(this.V,t))}get length(){return G(this.firstPoint,this.lastPoint)}get squareLength(){return yt(this.firstPoint,this.lastPoint)}get V(){return this._V===null&&(this._V=$(this.lastPoint,this.firstPoint)),this._V}get slope(){if(this._slope===null){const[t,n]=this.V;this._slope=n/t}return this._slope}get yIntercept(){return this._yIntercept===null&&(this._yIntercept=this.firstPoint[1]-this.slope*this.firstPoint[0]),this._yIntercept}get midPoint(){return J(this.firstPoint,nt(this.V,.5))}isSame(t){return t instanceof B?b(this.firstPoint,t.firstPoint)&&b(this.lastPoint,t.lastPoint)||b(this.lastPoint,t.firstPoint)&&b(this.firstPoint,t.lastPoint):!1}clone(){return new B(this.firstPoint,this.lastPoint)}reverse(){return new B(this.lastPoint,this.firstPoint)}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=new ct(Math.min(this.firstPoint[0],this.lastPoint[0])-this.precision,Math.min(this.firstPoint[1],this.lastPoint[1])-this.precision,Math.max(this.firstPoint[0],this.lastPoint[0])+this.precision,Math.max(this.firstPoint[1],this.lastPoint[1])+this.precision)),this._boundingBox}distanceFrom(t){const n=$(t,this.firstPoint),e=ae(n,this.V)/this.squareLength;if(e<0)return G(t,this.firstPoint);if(e>1)return G(t,this.lastPoint);const s=this.paramPoint(e);return G(t,s)}isOnSegment(t){if(b(t,this.firstPoint,this.precision))return!0;const n=$(t,this.firstPoint);if(!de(this.V,n))return!1;const e=ae(n,this.V)/this.squareLength;return this.isValidParameter(e)}gradientAt(t){return this.V}tangentAt(t){if(!this.isOnSegment(t))throw new Error("Point is not on segment");return Q(this.V)}get normalVector(){return et(Q(this.V))}get tangentAtFirstPoint(){return Q(this.V)}get tangentAtLastPoint(){return Q(this.V)}splitAt(t){let n;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?n=t:n=[t],n.forEach(a=>{if(!this.isOnSegment(a))throw new Error(`Point ${W(a)} is not on segment ${this.repr}`)});const e=[this.firstPoint,...n,this.lastPoint],s=pt(e),r=this.lastPoint[0]-this.firstPoint[0];let o=Math.sign(r),l=0;return Math.abs(r)<this.precision&&(o=Math.sign(this.lastPoint[1]-this.firstPoint[1]),l=1),s.sort((a,h)=>o*(a[l]-h[l])),s.flatMap((a,h)=>h===s.length-1?[]:new B(a,s[h+1]))}transform(t){return new B(t.transform(this.firstPoint),t.transform(this.lastPoint))}}function kn(i){return Array.from(Array(i).keys())}function ht(i){const t=Math.min(...i.map(n=>n.length));return kn(t).map(n=>i.map(e=>e[n]))}function bt(i,t=1e-9){return i<0?i+2*Math.PI:i>=2*Math.PI?i%(2*Math.PI):i>2*Math.PI-t?0:i}function Zt(i,t,n,e=1e-9){let s=t-i;return n&&(s=-s),s<0&&(s+=2*Math.PI),s>2*Math.PI-e?0:s}const Lt=(i,t,n)=>{const e=Pt(i.V,t.V),s=ut(i.V),r=ut(t.V),o=n?n*n:i.precision*t.precision;if(e*e<s*r*o)return"parallel";const l=$(t.firstPoint,i.firstPoint),a=Pt(l,t.V)/e,h=Pt(l,i.V)/e;return{intersectionParam1:a,intersectionParam2:h}};function sn(i,t,n=!1,e){const s=Lt(i,t,e);if(s==="parallel"){if(!n)return null;if(i.isSame(t))return i;const l=pt([t.isOnSegment(i.firstPoint)?i.firstPoint:null,t.isOnSegment(i.lastPoint)?i.lastPoint:null,i.isOnSegment(t.firstPoint)?t.firstPoint:null,i.isOnSegment(t.lastPoint)?t.lastPoint:null].filter(a=>a!==null)).sort((a,h)=>a[0]-h[0]);if(l.length===0)return null;if(l.length===1)return null;if(l.length===2)return new B(l[0],l[1]);throw console.error(l),new Error("Unexpected number of points while intersecting parallel lines")}const{intersectionParam1:r,intersectionParam2:o}=s;return!i.isValidParameter(r)||!t.isValidParameter(o)?null:i.paramPoint(r)}const Ut=(i,t)=>{const n=$(i,t);return nn(n)};class R extends Ot{constructor(t,n,e,s=!1,{ignoreChecks:r=!1}={}){if(super(t,n),this.segmentType="ARC",this._coefficients=null,this._angularLength=null,this._radius=null,this._firstAngle=null,this._lastAngle=null,this._boundingBox=null,this.center=e,this.clockwise=s,!r){if(b(t,n))throw new Error("Invalid arc, cannot be a full circle");if(Math.abs(this.radius-G(this.lastPoint,this.center))>this.precision)throw new Error(`Invalid arc, radius does not match between ${W(t)} and ${W(n)}} (center ${W(e)})`)}}get info(){return`ARC(${W(this.firstPoint)}, ${W(this.lastPoint)}, ${W(this.center)}, ${this.clockwise?"CW":"CCW"})`}get coefficients(){if(this._coefficients===null){const[t,n]=this.center,e=this.radius*this.radius;this._coefficients={x2:1/e,xy:0,y2:1/e,x:-(2*t)/e,y:-(2*n)/e,c:(t*t+n*n-e)/e}}return this._coefficients}isValidParameter(t){return 1-t>=-this.precision&&t>=-this.precision}angleToParam(t){return Zt(this.firstAngle,bt(t),this.clockwise)/this.angularLength}get angularLength(){return this._angularLength||(this._angularLength=Zt(this.firstAngle,this.lastAngle,this.clockwise)),this._angularLength}paramPoint(t){return J(this.center,Ct(this.radius,this.firstAngle+t*this.angularLength*(this.clockwise?-1:1)))}pointToParam(t){const[n,e]=Ut(t,this.center);if(Math.abs(n-this.radius)>this.precision)throw new Error(`Point ${W(t)} is not on segment ${this.repr}`);const s=this.angleToParam(e);if(!this.isValidParameter(s))throw new Error(`Point ${W(t)} is not on segment ${this.repr}`);return s}get radius(){return this._radius===null&&(this._radius=G(this.firstPoint,this.center)),this._radius}get firstAngle(){if(this._firstAngle===null){const[t,n]=$(this.firstPoint,this.center);this._firstAngle=bt(Math.atan2(n,t))}return this._firstAngle}get lastAngle(){if(this._lastAngle===null){const[t,n]=$(this.lastPoint,this.center);this._lastAngle=bt(Math.atan2(n,t))}return this._lastAngle}get length(){return this.radius*this.angularLength}get squareLength(){return this.length*this.length}get midPoint(){return this.paramPoint(.5)}isSame(t){return!(t instanceof R)||!b(this.center,t.center)?!1:b(this.firstPoint,t.firstPoint)&&b(this.lastPoint,t.lastPoint)&&this.clockwise===t.clockwise||b(this.lastPoint,t.firstPoint)&&b(this.firstPoint,t.lastPoint)&&this.clockwise===!t.clockwise}clone(){return new R(this.firstPoint,this.lastPoint,this.center,this.clockwise)}reverse(){return new R(this.lastPoint,this.firstPoint,this.center,!this.clockwise)}get boundingBox(){if(this._boundingBox===null){const t=this.radius+this.precision,n=e=>this.isValidParameter(this.angleToParam(e));this._boundingBox=new ct(n(Math.PI)?this.center[0]-t:Math.min(this.firstPoint[0],this.lastPoint[0])-this.precision,n(Math.PI*1.5)?this.center[1]-t:Math.min(this.firstPoint[1],this.lastPoint[1])-this.precision,n(0)?this.center[0]+t:Math.max(this.firstPoint[0],this.lastPoint[0])+this.precision,n(Math.PI/2)?this.center[1]+t:Math.max(this.firstPoint[1],this.lastPoint[1])+this.precision)}return this._boundingBox}distanceFrom(t){const[n,e]=Ut(t,this.center);return this.isValidParameter(this.angleToParam(e))?Math.abs(n-this.radius):Math.sqrt(Math.min(yt(t,this.firstPoint),yt(t,this.lastPoint)))}isOnSegment(t){if(b(t,this.firstPoint)||b(t,this.lastPoint))return!0;const[n,e]=Ut(t,this.center);if(Math.abs(n-this.radius)>this.precision)return!1;const s=this.angleToParam(e);return this.isValidParameter(s)}gradientAt(t){const n=this.firstAngle+t*this.angularLength*(this.clockwise?-1:1),e=this.radius*this.angularLength,s=-e*Math.sin(n),r=e*Math.cos(n);return this.clockwise?[-s,-r]:[s,r]}tangentAt(t){const[n,e]=Ut(t,this.center);if(Math.abs(n-this.radius)>this.precision)throw new Error("Point is not on the arc");const s=this.angleToParam(e);if(!this.isValidParameter(s))throw new Error("Point is not on the arc");const r=Ct(1,e);return(this.clockwise?Tt:et)(Q(r))}get tangentAtFirstPoint(){const t=Ct(1,this.firstAngle);return(this.clockwise?Tt:et)(Q(t))}get tangentAtLastPoint(){const t=Ct(1,this.lastAngle);return(this.clockwise?Tt:et)(Q(t))}splitAt(t){let n;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?n=t:n=[t];const s=[0,1,...n.map(l=>this.pointToParam(l))],r=new Map(ht([s,[this.firstPoint,this.lastPoint,...n]]));s.sort((l,a)=>l-a);let o=null;return s.flatMap((l,a)=>{if(a===s.length-1)return[];const h=s[a+1];if(h-l<this.precision)return o===null&&(o=l),[];const c=o===null?l:o,u=new R(r.get(c)||this.paramPoint(c),r.get(h)||this.paramPoint(h),this.center,this.clockwise);return o=null,u})}transform(t){return new R(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.center),t.keepsOrientation()?this.clockwise:!this.clockwise)}}function In(i,t,n){const e=new B(t,i),s=new B(t,n),r=et(e.tangentAtFirstPoint),o=et(s.tangentAtLastPoint),l=Lt({firstPoint:e.midPoint,V:r,precision:1e-9},{firstPoint:s.midPoint,V:o,precision:1e-9});if(l==="parallel")throw new Error("Cannot create an arc from three colinear points");const a=Pt($(i,t),$(n,t))>0;return new R(i,n,J(e.midPoint,nt(r,l.intersectionParam1)),a,{ignoreChecks:!0})}function Cn(i,t,n){const e=new B(t,i),s=et(e.tangentAtFirstPoint),r=Lt({firstPoint:e.midPoint,V:s,precision:1e-9},{firstPoint:i,V:et(n),precision:1e-9});if(r==="parallel")throw new Error("Cannot create an arc from three colinear points");const o=J(e.midPoint,nt(s,r.intersectionParam1)),l=Pt($(o,i),$(o,J(i,n)))<0;return new R(i,t,o,l,{ignoreChecks:!0})}const je=1e-21,Xt=1.618034;function ve(i,t=0,n=1,e=110,s=1e3){let r,o,l,a,h,c,u,f,p,g,w,P,m;for(o=i(t),l=i(n),o<l&&([t,n]=[n,t],[o,l]=[l,o]),m=n+Xt*(n-t),a=i(m),h=3,u=0;a<l;){if(f=(n-t)*(l-a),p=(n-m)*(l-o),g=p-f,Math.abs(g)<je?r=2*je:r=2*g,w=n-((n-m)*p-(n-t)*f)/r,P=n+e*(m-n),u>s)throw new Error("Too many iterations.");if(u+=1,(w-m)*(n-w)>0){if(c=i(w),h+=1,c<a)return t=n,n=w,o=l,l=c,[t,n,m,o,l,a,h];if(c>l)return m=w,a=c,[t,n,m,o,l,a,h];w=m+Xt*(m-n),c=i(w),h+=1}else(w-P)*(P-m)>=0?(w=P,c=i(w),h+=1):(w-P)*(m-w)>0?(c=i(w),h+=1,c<a&&(n=m,m=w,w=m+Xt*(m-n),l=a,a=c,c=i(w),h+=1)):(w=m+Xt*(m-n),c=i(w),h+=1);t=n,n=m,m=w,o=l,l=a,a=c}return[t,n,m,o,l,a,h]}const Fe=.381966,xn=1e-11;class jn{constructor(t,n=148e-10,e=500){this.func=t,this.tol=n,this.maxiter=e,this.func=t,this.tol=n,this.maxiter=e,this.xmin=1/0,this.fval=1/0,this.iter=0,this.funcalls=0,this.brack=null}setBracket(t=null){this.brack=t}getBracketInfo(){let t,n,e,s,r,o,l;const a=this.func,h=this.brack;if(h===null)[r,o,l,t,n,e,s]=ve(a);else if(h.length===2)[r,o,l,t,n,e,s]=ve(a,h[0],h[1]);else if(h.length===3){if([r,o,l]=h,r>l&&([l,r]=[r,l]),!(r<o&&o<l))throw new Error("Not a bracketing interval.");if(t=a(r),n=a(o),e=a(l),!(n<t&&n<e))throw new Error("Not a bracketing interval.");s=3}else throw new Error("Bracketing interval must be length 2 or 3 sequence.");return[r,o,l,t,n,e,s]}optimize(){const t=this.func;let[n,e,s,,,,r]=this.getBracketInfo(),o=e,l=e,a=e,h=t(o),c=h,u=h,f,p;n<s?(f=n,p=s):(f=s,p=n);let g=0;r=1;let w=0,P=0,m;for(;w<this.maxiter;){const d=this.tol*Math.abs(o)+xn,y=2*d,M=.5*(f+p);if(Math.abs(o-M)<y-.5*(p-f))break;if(Math.abs(g)<=d)o>=M?g=f-o:g=p-o,P=Fe*g;else{const C=(o-l)*(u-c);let _=(o-a)*(u-h),E=(o-a)*_-(o-l)*C;_=2*(_-C),_>0&&(E=-E),_=Math.abs(_);const v=g;g=P,E>_*(f-o)&&E<_*(p-o)&&Math.abs(E)<Math.abs(.5*_*v)?(P=E*1/_,m=o+P,(m-f<y||p-m<y)&&(M-o>=0?P=d:P=-d)):(o>=M?g=f-o:g=p-o,P=Fe*g)}Math.abs(P)<d?P>=0?m=o+d:m=o-d:m=o+P;const S=t(m);r+=1,S>u?(m<o?f=m:p=m,S<=h||l===o?(a=l,l=m,c=h,h=S):(S<=c||a===o||a===l)&&(a=m,c=S)):(m>=o?f=o:p=o,a=l,l=o,o=m,c=h,h=u,u=S),w+=1}this.xmin=o,this.fval=u,this.iter=w,this.funcalls=r}results(){return{argMin:this.xmin,fMin:this.fval,iterations:this.iter,funcCalls:this.funcalls}}run(){return this.optimize(),this.results()}}function rn(i,t=1e-8,n=1e3){const e=new jn(i,t,n);return e.setBracket([0,1]),e.run()}const vn=Object.prototype.toString;function st(i){const t=vn.call(i);return t.endsWith("Array]")&&!t.includes("Big")}function Fn(i){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!st(i))throw new TypeError("input must be an array");if(i.length===0)throw new TypeError("input must not be empty");var n=t.fromIndex,e=n===void 0?0:n,s=t.toIndex,r=s===void 0?i.length:s;if(e<0||e>=i.length||!Number.isInteger(e))throw new Error("fromIndex must be a positive integer smaller than length");if(r<=e||r>i.length||!Number.isInteger(r))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var o=i[e],l=e+1;l<r;l++)i[l]>o&&(o=i[l]);return o}function Tn(i){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!st(i))throw new TypeError("input must be an array");if(i.length===0)throw new TypeError("input must not be empty");var n=t.fromIndex,e=n===void 0?0:n,s=t.toIndex,r=s===void 0?i.length:s;if(e<0||e>=i.length||!Number.isInteger(e))throw new Error("fromIndex must be a positive integer smaller than length");if(r<=e||r>i.length||!Number.isInteger(r))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var o=i[e],l=e+1;l<r;l++)i[l]<o&&(o=i[l]);return o}function Te(i){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(st(i)){if(i.length===0)throw new TypeError("input must not be empty")}else throw new TypeError("input must be an array");var n;if(t.output!==void 0){if(!st(t.output))throw new TypeError("output option must be an array if specified");n=t.output}else n=new Array(i.length);var e=Tn(i),s=Fn(i);if(e===s)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var r=t.min,o=r===void 0?t.autoMinMax?e:0:r,l=t.max,a=l===void 0?t.autoMinMax?s:1:l;if(o>=a)throw new RangeError("min option must be smaller than max option");for(var h=(a-o)/(s-e),c=0;c<i.length;c++)n[c]=(i[c]-e)*h+o;return n}const Gt=" ".repeat(2),on=" ".repeat(4);function Rn(){return ln(this)}function ln(i,t={}){const{maxRows:n=15,maxColumns:e=10,maxNumSize:s=8,padMinus:r="auto"}=t;return`${i.constructor.name} {
|
|
2
|
-
${Gt}[
|
|
3
|
-
${on}${An(i,n,e,s,r)}
|
|
4
|
-
${Gt}]
|
|
5
|
-
${Gt}rows: ${i.rows}
|
|
6
|
-
${Gt}columns: ${i.columns}
|
|
7
|
-
}`}function An(i,t,n,e,s){const{rows:r,columns:o}=i,l=Math.min(r,t),a=Math.min(o,n),h=[];if(s==="auto"){s=!1;t:for(let c=0;c<l;c++)for(let u=0;u<a;u++)if(i.get(c,u)<0){s=!0;break t}}for(let c=0;c<l;c++){let u=[];for(let f=0;f<a;f++)u.push(qn(i.get(c,f),e,s));h.push(`${u.join(" ")}`)}return a!==o&&(h[h.length-1]+=` ... ${o-n} more columns`),l!==r&&h.push(`... ${r-t} more rows`),h.join(`
|
|
8
|
-
${on}`)}function qn(i,t,n){return(i>=0&&n?` ${Re(i,t-1)}`:Re(i,t)).padEnd(t)}function Re(i,t){let n=i.toString();if(n.length<=t)return n;let e=i.toFixed(t);if(e.length>t&&(e=i.toFixed(Math.max(0,t-(e.length-t)))),e.length<=t&&!e.startsWith("0.000")&&!e.startsWith("-0.000"))return e;let s=i.toExponential(t);return s.length>t&&(s=i.toExponential(Math.max(0,t-(s.length-t)))),s.slice(0)}function Bn(i,t){i.prototype.add=function(e){return typeof e=="number"?this.addS(e):this.addM(e)},i.prototype.addS=function(e){for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)+e);return this},i.prototype.addM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)+e.get(s,r));return this},i.add=function(e,s){return new t(e).add(s)},i.prototype.sub=function(e){return typeof e=="number"?this.subS(e):this.subM(e)},i.prototype.subS=function(e){for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)-e);return this},i.prototype.subM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)-e.get(s,r));return this},i.sub=function(e,s){return new t(e).sub(s)},i.prototype.subtract=i.prototype.sub,i.prototype.subtractS=i.prototype.subS,i.prototype.subtractM=i.prototype.subM,i.subtract=i.sub,i.prototype.mul=function(e){return typeof e=="number"?this.mulS(e):this.mulM(e)},i.prototype.mulS=function(e){for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)*e);return this},i.prototype.mulM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)*e.get(s,r));return this},i.mul=function(e,s){return new t(e).mul(s)},i.prototype.multiply=i.prototype.mul,i.prototype.multiplyS=i.prototype.mulS,i.prototype.multiplyM=i.prototype.mulM,i.multiply=i.mul,i.prototype.div=function(e){return typeof e=="number"?this.divS(e):this.divM(e)},i.prototype.divS=function(e){for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)/e);return this},i.prototype.divM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)/e.get(s,r));return this},i.div=function(e,s){return new t(e).div(s)},i.prototype.divide=i.prototype.div,i.prototype.divideS=i.prototype.divS,i.prototype.divideM=i.prototype.divM,i.divide=i.div,i.prototype.mod=function(e){return typeof e=="number"?this.modS(e):this.modM(e)},i.prototype.modS=function(e){for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)%e);return this},i.prototype.modM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)%e.get(s,r));return this},i.mod=function(e,s){return new t(e).mod(s)},i.prototype.modulus=i.prototype.mod,i.prototype.modulusS=i.prototype.modS,i.prototype.modulusM=i.prototype.modM,i.modulus=i.mod,i.prototype.and=function(e){return typeof e=="number"?this.andS(e):this.andM(e)},i.prototype.andS=function(e){for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)&e);return this},i.prototype.andM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)&e.get(s,r));return this},i.and=function(e,s){return new t(e).and(s)},i.prototype.or=function(e){return typeof e=="number"?this.orS(e):this.orM(e)},i.prototype.orS=function(e){for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)|e);return this},i.prototype.orM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)|e.get(s,r));return this},i.or=function(e,s){return new t(e).or(s)},i.prototype.xor=function(e){return typeof e=="number"?this.xorS(e):this.xorM(e)},i.prototype.xorS=function(e){for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)^e);return this},i.prototype.xorM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)^e.get(s,r));return this},i.xor=function(e,s){return new t(e).xor(s)},i.prototype.leftShift=function(e){return typeof e=="number"?this.leftShiftS(e):this.leftShiftM(e)},i.prototype.leftShiftS=function(e){for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)<<e);return this},i.prototype.leftShiftM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)<<e.get(s,r));return this},i.leftShift=function(e,s){return new t(e).leftShift(s)},i.prototype.signPropagatingRightShift=function(e){return typeof e=="number"?this.signPropagatingRightShiftS(e):this.signPropagatingRightShiftM(e)},i.prototype.signPropagatingRightShiftS=function(e){for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)>>e);return this},i.prototype.signPropagatingRightShiftM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)>>e.get(s,r));return this},i.signPropagatingRightShift=function(e,s){return new t(e).signPropagatingRightShift(s)},i.prototype.rightShift=function(e){return typeof e=="number"?this.rightShiftS(e):this.rightShiftM(e)},i.prototype.rightShiftS=function(e){for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)>>>e);return this},i.prototype.rightShiftM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,this.get(s,r)>>>e.get(s,r));return this},i.rightShift=function(e,s){return new t(e).rightShift(s)},i.prototype.zeroFillRightShift=i.prototype.rightShift,i.prototype.zeroFillRightShiftS=i.prototype.rightShiftS,i.prototype.zeroFillRightShiftM=i.prototype.rightShiftM,i.zeroFillRightShift=i.rightShift,i.prototype.not=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,~this.get(e,s));return this},i.not=function(e){return new t(e).not()},i.prototype.abs=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.abs(this.get(e,s)));return this},i.abs=function(e){return new t(e).abs()},i.prototype.acos=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.acos(this.get(e,s)));return this},i.acos=function(e){return new t(e).acos()},i.prototype.acosh=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.acosh(this.get(e,s)));return this},i.acosh=function(e){return new t(e).acosh()},i.prototype.asin=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.asin(this.get(e,s)));return this},i.asin=function(e){return new t(e).asin()},i.prototype.asinh=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.asinh(this.get(e,s)));return this},i.asinh=function(e){return new t(e).asinh()},i.prototype.atan=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.atan(this.get(e,s)));return this},i.atan=function(e){return new t(e).atan()},i.prototype.atanh=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.atanh(this.get(e,s)));return this},i.atanh=function(e){return new t(e).atanh()},i.prototype.cbrt=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.cbrt(this.get(e,s)));return this},i.cbrt=function(e){return new t(e).cbrt()},i.prototype.ceil=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.ceil(this.get(e,s)));return this},i.ceil=function(e){return new t(e).ceil()},i.prototype.clz32=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.clz32(this.get(e,s)));return this},i.clz32=function(e){return new t(e).clz32()},i.prototype.cos=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.cos(this.get(e,s)));return this},i.cos=function(e){return new t(e).cos()},i.prototype.cosh=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.cosh(this.get(e,s)));return this},i.cosh=function(e){return new t(e).cosh()},i.prototype.exp=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.exp(this.get(e,s)));return this},i.exp=function(e){return new t(e).exp()},i.prototype.expm1=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.expm1(this.get(e,s)));return this},i.expm1=function(e){return new t(e).expm1()},i.prototype.floor=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.floor(this.get(e,s)));return this},i.floor=function(e){return new t(e).floor()},i.prototype.fround=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.fround(this.get(e,s)));return this},i.fround=function(e){return new t(e).fround()},i.prototype.log=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.log(this.get(e,s)));return this},i.log=function(e){return new t(e).log()},i.prototype.log1p=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.log1p(this.get(e,s)));return this},i.log1p=function(e){return new t(e).log1p()},i.prototype.log10=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.log10(this.get(e,s)));return this},i.log10=function(e){return new t(e).log10()},i.prototype.log2=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.log2(this.get(e,s)));return this},i.log2=function(e){return new t(e).log2()},i.prototype.round=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.round(this.get(e,s)));return this},i.round=function(e){return new t(e).round()},i.prototype.sign=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.sign(this.get(e,s)));return this},i.sign=function(e){return new t(e).sign()},i.prototype.sin=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.sin(this.get(e,s)));return this},i.sin=function(e){return new t(e).sin()},i.prototype.sinh=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.sinh(this.get(e,s)));return this},i.sinh=function(e){return new t(e).sinh()},i.prototype.sqrt=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.sqrt(this.get(e,s)));return this},i.sqrt=function(e){return new t(e).sqrt()},i.prototype.tan=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.tan(this.get(e,s)));return this},i.tan=function(e){return new t(e).tan()},i.prototype.tanh=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.tanh(this.get(e,s)));return this},i.tanh=function(e){return new t(e).tanh()},i.prototype.trunc=function(){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,Math.trunc(this.get(e,s)));return this},i.trunc=function(e){return new t(e).trunc()},i.pow=function(e,s){return new t(e).pow(s)},i.prototype.pow=function(e){return typeof e=="number"?this.powS(e):this.powM(e)},i.prototype.powS=function(e){for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,Math.pow(this.get(s,r),e));return this},i.prototype.powM=function(e){if(e=t.checkMatrix(e),this.rows!==e.rows||this.columns!==e.columns)throw new RangeError("Matrices dimensions must be equal");for(let s=0;s<this.rows;s++)for(let r=0;r<this.columns;r++)this.set(s,r,Math.pow(this.get(s,r),e.get(s,r)));return this}}function ot(i,t,n){let e=n?i.rows:i.rows-1;if(t<0||t>e)throw new RangeError("Row index out of range")}function lt(i,t,n){let e=n?i.columns:i.columns-1;if(t<0||t>e)throw new RangeError("Column index out of range")}function kt(i,t){if(t.to1DArray&&(t=t.to1DArray()),t.length!==i.columns)throw new RangeError("vector size must be the same as the number of columns");return t}function It(i,t){if(t.to1DArray&&(t=t.to1DArray()),t.length!==i.rows)throw new RangeError("vector size must be the same as the number of rows");return t}function On(i,t){if(!st(t))throw new TypeError("row indices must be an array");for(let n=0;n<t.length;n++)if(t[n]<0||t[n]>=i.rows)throw new RangeError("row indices are out of range")}function Ln(i,t){if(!st(t))throw new TypeError("column indices must be an array");for(let n=0;n<t.length;n++)if(t[n]<0||t[n]>=i.columns)throw new RangeError("column indices are out of range")}function Ae(i,t,n,e,s){if(arguments.length!==5)throw new RangeError("expected 4 arguments");if(Wt("startRow",t),Wt("endRow",n),Wt("startColumn",e),Wt("endColumn",s),t>n||e>s||t<0||t>=i.rows||n<0||n>=i.rows||e<0||e>=i.columns||s<0||s>=i.columns)throw new RangeError("Submatrix indices are out of range")}function ne(i,t=0){let n=[];for(let e=0;e<i;e++)n.push(t);return n}function Wt(i,t){if(typeof t!="number")throw new TypeError(`${i} must be a number`)}function _t(i){if(i.isEmpty())throw new Error("Empty matrix has no elements to index")}function $n(i){let t=ne(i.rows);for(let n=0;n<i.rows;++n)for(let e=0;e<i.columns;++e)t[n]+=i.get(n,e);return t}function zn(i){let t=ne(i.columns);for(let n=0;n<i.rows;++n)for(let e=0;e<i.columns;++e)t[e]+=i.get(n,e);return t}function Nn(i){let t=0;for(let n=0;n<i.rows;n++)for(let e=0;e<i.columns;e++)t+=i.get(n,e);return t}function Dn(i){let t=ne(i.rows,1);for(let n=0;n<i.rows;++n)for(let e=0;e<i.columns;++e)t[n]*=i.get(n,e);return t}function Vn(i){let t=ne(i.columns,1);for(let n=0;n<i.rows;++n)for(let e=0;e<i.columns;++e)t[e]*=i.get(n,e);return t}function Yn(i){let t=1;for(let n=0;n<i.rows;n++)for(let e=0;e<i.columns;e++)t*=i.get(n,e);return t}function Un(i,t,n){const e=i.rows,s=i.columns,r=[];for(let o=0;o<e;o++){let l=0,a=0,h=0;for(let c=0;c<s;c++)h=i.get(o,c)-n[o],l+=h,a+=h*h;t?r.push((a-l*l/s)/(s-1)):r.push((a-l*l/s)/s)}return r}function Xn(i,t,n){const e=i.rows,s=i.columns,r=[];for(let o=0;o<s;o++){let l=0,a=0,h=0;for(let c=0;c<e;c++)h=i.get(c,o)-n[o],l+=h,a+=h*h;t?r.push((a-l*l/e)/(e-1)):r.push((a-l*l/e)/e)}return r}function Gn(i,t,n){const e=i.rows,s=i.columns,r=e*s;let o=0,l=0,a=0;for(let h=0;h<e;h++)for(let c=0;c<s;c++)a=i.get(h,c)-n,o+=a,l+=a*a;return t?(l-o*o/r)/(r-1):(l-o*o/r)/r}function Wn(i,t){for(let n=0;n<i.rows;n++)for(let e=0;e<i.columns;e++)i.set(n,e,i.get(n,e)-t[n])}function Qn(i,t){for(let n=0;n<i.rows;n++)for(let e=0;e<i.columns;e++)i.set(n,e,i.get(n,e)-t[e])}function Jn(i,t){for(let n=0;n<i.rows;n++)for(let e=0;e<i.columns;e++)i.set(n,e,i.get(n,e)-t)}function Zn(i){const t=[];for(let n=0;n<i.rows;n++){let e=0;for(let s=0;s<i.columns;s++)e+=Math.pow(i.get(n,s),2)/(i.columns-1);t.push(Math.sqrt(e))}return t}function Kn(i,t){for(let n=0;n<i.rows;n++)for(let e=0;e<i.columns;e++)i.set(n,e,i.get(n,e)/t[n])}function Hn(i){const t=[];for(let n=0;n<i.columns;n++){let e=0;for(let s=0;s<i.rows;s++)e+=Math.pow(i.get(s,n),2)/(i.rows-1);t.push(Math.sqrt(e))}return t}function ts(i,t){for(let n=0;n<i.rows;n++)for(let e=0;e<i.columns;e++)i.set(n,e,i.get(n,e)/t[e])}function es(i){const t=i.size-1;let n=0;for(let e=0;e<i.columns;e++)for(let s=0;s<i.rows;s++)n+=Math.pow(i.get(s,e),2)/t;return Math.sqrt(n)}function ns(i,t){for(let n=0;n<i.rows;n++)for(let e=0;e<i.columns;e++)i.set(n,e,i.get(n,e)/t)}class T{static from1DArray(t,n,e){if(t*n!==e.length)throw new RangeError("data length does not match given dimensions");let r=new j(t,n);for(let o=0;o<t;o++)for(let l=0;l<n;l++)r.set(o,l,e[o*n+l]);return r}static rowVector(t){let n=new j(1,t.length);for(let e=0;e<t.length;e++)n.set(0,e,t[e]);return n}static columnVector(t){let n=new j(t.length,1);for(let e=0;e<t.length;e++)n.set(e,0,t[e]);return n}static zeros(t,n){return new j(t,n)}static ones(t,n){return new j(t,n).fill(1)}static rand(t,n,e={}){if(typeof e!="object")throw new TypeError("options must be an object");const{random:s=Math.random}=e;let r=new j(t,n);for(let o=0;o<t;o++)for(let l=0;l<n;l++)r.set(o,l,s());return r}static randInt(t,n,e={}){if(typeof e!="object")throw new TypeError("options must be an object");const{min:s=0,max:r=1e3,random:o=Math.random}=e;if(!Number.isInteger(s))throw new TypeError("min must be an integer");if(!Number.isInteger(r))throw new TypeError("max must be an integer");if(s>=r)throw new RangeError("min must be smaller than max");let l=r-s,a=new j(t,n);for(let h=0;h<t;h++)for(let c=0;c<n;c++){let u=s+Math.round(o()*l);a.set(h,c,u)}return a}static eye(t,n,e){n===void 0&&(n=t),e===void 0&&(e=1);let s=Math.min(t,n),r=this.zeros(t,n);for(let o=0;o<s;o++)r.set(o,o,e);return r}static diag(t,n,e){let s=t.length;n===void 0&&(n=s),e===void 0&&(e=n);let r=Math.min(s,n,e),o=this.zeros(n,e);for(let l=0;l<r;l++)o.set(l,l,t[l]);return o}static min(t,n){t=this.checkMatrix(t),n=this.checkMatrix(n);let e=t.rows,s=t.columns,r=new j(e,s);for(let o=0;o<e;o++)for(let l=0;l<s;l++)r.set(o,l,Math.min(t.get(o,l),n.get(o,l)));return r}static max(t,n){t=this.checkMatrix(t),n=this.checkMatrix(n);let e=t.rows,s=t.columns,r=new this(e,s);for(let o=0;o<e;o++)for(let l=0;l<s;l++)r.set(o,l,Math.max(t.get(o,l),n.get(o,l)));return r}static checkMatrix(t){return T.isMatrix(t)?t:new j(t)}static isMatrix(t){return t!=null&&t.klass==="Matrix"}get size(){return this.rows*this.columns}apply(t){if(typeof t!="function")throw new TypeError("callback must be a function");for(let n=0;n<this.rows;n++)for(let e=0;e<this.columns;e++)t.call(this,n,e);return this}to1DArray(){let t=[];for(let n=0;n<this.rows;n++)for(let e=0;e<this.columns;e++)t.push(this.get(n,e));return t}to2DArray(){let t=[];for(let n=0;n<this.rows;n++){t.push([]);for(let e=0;e<this.columns;e++)t[n].push(this.get(n,e))}return t}toJSON(){return this.to2DArray()}isRowVector(){return this.rows===1}isColumnVector(){return this.columns===1}isVector(){return this.rows===1||this.columns===1}isSquare(){return this.rows===this.columns}isEmpty(){return this.rows===0||this.columns===0}isSymmetric(){if(this.isSquare()){for(let t=0;t<this.rows;t++)for(let n=0;n<=t;n++)if(this.get(t,n)!==this.get(n,t))return!1;return!0}return!1}isEchelonForm(){let t=0,n=0,e=-1,s=!0,r=!1;for(;t<this.rows&&s;){for(n=0,r=!1;n<this.columns&&r===!1;)this.get(t,n)===0?n++:this.get(t,n)===1&&n>e?(r=!0,e=n):(s=!1,r=!0);t++}return s}isReducedEchelonForm(){let t=0,n=0,e=-1,s=!0,r=!1;for(;t<this.rows&&s;){for(n=0,r=!1;n<this.columns&&r===!1;)this.get(t,n)===0?n++:this.get(t,n)===1&&n>e?(r=!0,e=n):(s=!1,r=!0);for(let o=n+1;o<this.rows;o++)this.get(t,o)!==0&&(s=!1);t++}return s}echelonForm(){let t=this.clone(),n=0,e=0;for(;n<t.rows&&e<t.columns;){let s=n;for(let r=n;r<t.rows;r++)t.get(r,e)>t.get(s,e)&&(s=r);if(t.get(s,e)===0)e++;else{t.swapRows(n,s);let r=t.get(n,e);for(let o=e;o<t.columns;o++)t.set(n,o,t.get(n,o)/r);for(let o=n+1;o<t.rows;o++){let l=t.get(o,e)/t.get(n,e);t.set(o,e,0);for(let a=e+1;a<t.columns;a++)t.set(o,a,t.get(o,a)-t.get(n,a)*l)}n++,e++}}return t}reducedEchelonForm(){let t=this.echelonForm(),n=t.columns,e=t.rows,s=e-1;for(;s>=0;)if(t.maxRow(s)===0)s--;else{let r=0,o=!1;for(;r<e&&o===!1;)t.get(s,r)===1?o=!0:r++;for(let l=0;l<s;l++){let a=t.get(l,r);for(let h=r;h<n;h++){let c=t.get(l,h)-a*t.get(s,h);t.set(l,h,c)}}s--}return t}set(){throw new Error("set method is unimplemented")}get(){throw new Error("get method is unimplemented")}repeat(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{rows:n=1,columns:e=1}=t;if(!Number.isInteger(n)||n<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(e)||e<=0)throw new TypeError("columns must be a positive integer");let s=new j(this.rows*n,this.columns*e);for(let r=0;r<n;r++)for(let o=0;o<e;o++)s.setSubMatrix(this,this.rows*r,this.columns*o);return s}fill(t){for(let n=0;n<this.rows;n++)for(let e=0;e<this.columns;e++)this.set(n,e,t);return this}neg(){return this.mulS(-1)}getRow(t){ot(this,t);let n=[];for(let e=0;e<this.columns;e++)n.push(this.get(t,e));return n}getRowVector(t){return j.rowVector(this.getRow(t))}setRow(t,n){ot(this,t),n=kt(this,n);for(let e=0;e<this.columns;e++)this.set(t,e,n[e]);return this}swapRows(t,n){ot(this,t),ot(this,n);for(let e=0;e<this.columns;e++){let s=this.get(t,e);this.set(t,e,this.get(n,e)),this.set(n,e,s)}return this}getColumn(t){lt(this,t);let n=[];for(let e=0;e<this.rows;e++)n.push(this.get(e,t));return n}getColumnVector(t){return j.columnVector(this.getColumn(t))}setColumn(t,n){lt(this,t),n=It(this,n);for(let e=0;e<this.rows;e++)this.set(e,t,n[e]);return this}swapColumns(t,n){lt(this,t),lt(this,n);for(let e=0;e<this.rows;e++){let s=this.get(e,t);this.set(e,t,this.get(e,n)),this.set(e,n,s)}return this}addRowVector(t){t=kt(this,t);for(let n=0;n<this.rows;n++)for(let e=0;e<this.columns;e++)this.set(n,e,this.get(n,e)+t[e]);return this}subRowVector(t){t=kt(this,t);for(let n=0;n<this.rows;n++)for(let e=0;e<this.columns;e++)this.set(n,e,this.get(n,e)-t[e]);return this}mulRowVector(t){t=kt(this,t);for(let n=0;n<this.rows;n++)for(let e=0;e<this.columns;e++)this.set(n,e,this.get(n,e)*t[e]);return this}divRowVector(t){t=kt(this,t);for(let n=0;n<this.rows;n++)for(let e=0;e<this.columns;e++)this.set(n,e,this.get(n,e)/t[e]);return this}addColumnVector(t){t=It(this,t);for(let n=0;n<this.rows;n++)for(let e=0;e<this.columns;e++)this.set(n,e,this.get(n,e)+t[n]);return this}subColumnVector(t){t=It(this,t);for(let n=0;n<this.rows;n++)for(let e=0;e<this.columns;e++)this.set(n,e,this.get(n,e)-t[n]);return this}mulColumnVector(t){t=It(this,t);for(let n=0;n<this.rows;n++)for(let e=0;e<this.columns;e++)this.set(n,e,this.get(n,e)*t[n]);return this}divColumnVector(t){t=It(this,t);for(let n=0;n<this.rows;n++)for(let e=0;e<this.columns;e++)this.set(n,e,this.get(n,e)/t[n]);return this}mulRow(t,n){ot(this,t);for(let e=0;e<this.columns;e++)this.set(t,e,this.get(t,e)*n);return this}mulColumn(t,n){lt(this,t);for(let e=0;e<this.rows;e++)this.set(e,t,this.get(e,t)*n);return this}max(t){if(this.isEmpty())return NaN;switch(t){case"row":{const n=new Array(this.rows).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.get(e,s)>n[e]&&(n[e]=this.get(e,s));return n}case"column":{const n=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.get(e,s)>n[s]&&(n[s]=this.get(e,s));return n}case void 0:{let n=this.get(0,0);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.get(e,s)>n&&(n=this.get(e,s));return n}default:throw new Error(`invalid option: ${t}`)}}maxIndex(){_t(this);let t=this.get(0,0),n=[0,0];for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.get(e,s)>t&&(t=this.get(e,s),n[0]=e,n[1]=s);return n}min(t){if(this.isEmpty())return NaN;switch(t){case"row":{const n=new Array(this.rows).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.get(e,s)<n[e]&&(n[e]=this.get(e,s));return n}case"column":{const n=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.get(e,s)<n[s]&&(n[s]=this.get(e,s));return n}case void 0:{let n=this.get(0,0);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.get(e,s)<n&&(n=this.get(e,s));return n}default:throw new Error(`invalid option: ${t}`)}}minIndex(){_t(this);let t=this.get(0,0),n=[0,0];for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.get(e,s)<t&&(t=this.get(e,s),n[0]=e,n[1]=s);return n}maxRow(t){if(ot(this,t),this.isEmpty())return NaN;let n=this.get(t,0);for(let e=1;e<this.columns;e++)this.get(t,e)>n&&(n=this.get(t,e));return n}maxRowIndex(t){ot(this,t),_t(this);let n=this.get(t,0),e=[t,0];for(let s=1;s<this.columns;s++)this.get(t,s)>n&&(n=this.get(t,s),e[1]=s);return e}minRow(t){if(ot(this,t),this.isEmpty())return NaN;let n=this.get(t,0);for(let e=1;e<this.columns;e++)this.get(t,e)<n&&(n=this.get(t,e));return n}minRowIndex(t){ot(this,t),_t(this);let n=this.get(t,0),e=[t,0];for(let s=1;s<this.columns;s++)this.get(t,s)<n&&(n=this.get(t,s),e[1]=s);return e}maxColumn(t){if(lt(this,t),this.isEmpty())return NaN;let n=this.get(0,t);for(let e=1;e<this.rows;e++)this.get(e,t)>n&&(n=this.get(e,t));return n}maxColumnIndex(t){lt(this,t),_t(this);let n=this.get(0,t),e=[0,t];for(let s=1;s<this.rows;s++)this.get(s,t)>n&&(n=this.get(s,t),e[0]=s);return e}minColumn(t){if(lt(this,t),this.isEmpty())return NaN;let n=this.get(0,t);for(let e=1;e<this.rows;e++)this.get(e,t)<n&&(n=this.get(e,t));return n}minColumnIndex(t){lt(this,t),_t(this);let n=this.get(0,t),e=[0,t];for(let s=1;s<this.rows;s++)this.get(s,t)<n&&(n=this.get(s,t),e[0]=s);return e}diag(){let t=Math.min(this.rows,this.columns),n=[];for(let e=0;e<t;e++)n.push(this.get(e,e));return n}norm(t="frobenius"){let n=0;if(t==="max")return this.max();if(t==="frobenius"){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)n=n+this.get(e,s)*this.get(e,s);return Math.sqrt(n)}else throw new RangeError(`unknown norm type: ${t}`)}cumulativeSum(){let t=0;for(let n=0;n<this.rows;n++)for(let e=0;e<this.columns;e++)t+=this.get(n,e),this.set(n,e,t);return this}dot(t){T.isMatrix(t)&&(t=t.to1DArray());let n=this.to1DArray();if(n.length!==t.length)throw new RangeError("vectors do not have the same size");let e=0;for(let s=0;s<n.length;s++)e+=n[s]*t[s];return e}mmul(t){t=j.checkMatrix(t);let n=this.rows,e=this.columns,s=t.columns,r=new j(n,s),o=new Float64Array(e);for(let l=0;l<s;l++){for(let a=0;a<e;a++)o[a]=t.get(a,l);for(let a=0;a<n;a++){let h=0;for(let c=0;c<e;c++)h+=this.get(a,c)*o[c];r.set(a,l,h)}}return r}strassen2x2(t){t=j.checkMatrix(t);let n=new j(2,2);const e=this.get(0,0),s=t.get(0,0),r=this.get(0,1),o=t.get(0,1),l=this.get(1,0),a=t.get(1,0),h=this.get(1,1),c=t.get(1,1),u=(e+h)*(s+c),f=(l+h)*s,p=e*(o-c),g=h*(a-s),w=(e+r)*c,P=(l-e)*(s+o),m=(r-h)*(a+c),d=u+g-w+m,y=p+w,M=f+g,S=u-f+p+P;return n.set(0,0,d),n.set(0,1,y),n.set(1,0,M),n.set(1,1,S),n}strassen3x3(t){t=j.checkMatrix(t);let n=new j(3,3);const e=this.get(0,0),s=this.get(0,1),r=this.get(0,2),o=this.get(1,0),l=this.get(1,1),a=this.get(1,2),h=this.get(2,0),c=this.get(2,1),u=this.get(2,2),f=t.get(0,0),p=t.get(0,1),g=t.get(0,2),w=t.get(1,0),P=t.get(1,1),m=t.get(1,2),d=t.get(2,0),y=t.get(2,1),M=t.get(2,2),S=(e+s+r-o-l-c-u)*P,C=(e-o)*(-p+P),_=l*(-f+p+w-P-m-d+M),E=(-e+o+l)*(f-p+P),v=(o+l)*(-f+p),L=e*f,U=(-e+h+c)*(f-g+m),K=(-e+h)*(g-m),q=(h+c)*(-f+g),x=(e+s+r-l-a-h-c)*m,I=c*(-f+g+w-P-m-d+y),k=(-r+c+u)*(P+d-y),wt=(r-u)*(P-y),A=r*d,O=(c+u)*(-d+y),F=(-r+l+a)*(m+d-M),D=(r-a)*(m-M),X=(l+a)*(-d+M),V=s*w,Z=a*y,ft=o*g,at=h*p,dt=u*M,ie=L+A+V,re=S+E+v+L+k+A+O,oe=L+U+q+x+A+F+X,le=C+_+E+L+A+F+D,$t=C+E+v+L+Z,zt=A+F+D+X+ft,Nt=L+U+K+I+k+wt+A,Dt=k+wt+A+O+at,Vt=L+U+K+q+dt;return n.set(0,0,ie),n.set(0,1,re),n.set(0,2,oe),n.set(1,0,le),n.set(1,1,$t),n.set(1,2,zt),n.set(2,0,Nt),n.set(2,1,Dt),n.set(2,2,Vt),n}mmulStrassen(t){t=j.checkMatrix(t);let n=this.clone(),e=n.rows,s=n.columns,r=t.rows,o=t.columns;s!==r&&console.warn(`Multiplying ${e} x ${s} and ${r} x ${o} matrix: dimensions do not match.`);function l(u,f,p){let g=u.rows,w=u.columns;if(g===f&&w===p)return u;{let P=T.zeros(f,p);return P=P.setSubMatrix(u,0,0),P}}let a=Math.max(e,r),h=Math.max(s,o);n=l(n,a,h),t=l(t,a,h);function c(u,f,p,g){if(p<=512||g<=512)return u.mmul(f);p%2===1&&g%2===1?(u=l(u,p+1,g+1),f=l(f,p+1,g+1)):p%2===1?(u=l(u,p+1,g),f=l(f,p+1,g)):g%2===1&&(u=l(u,p,g+1),f=l(f,p,g+1));let w=parseInt(u.rows/2,10),P=parseInt(u.columns/2,10),m=u.subMatrix(0,w-1,0,P-1),d=f.subMatrix(0,w-1,0,P-1),y=u.subMatrix(0,w-1,P,u.columns-1),M=f.subMatrix(0,w-1,P,f.columns-1),S=u.subMatrix(w,u.rows-1,0,P-1),C=f.subMatrix(w,f.rows-1,0,P-1),_=u.subMatrix(w,u.rows-1,P,u.columns-1),E=f.subMatrix(w,f.rows-1,P,f.columns-1),v=c(T.add(m,_),T.add(d,E),w,P),L=c(T.add(S,_),d,w,P),U=c(m,T.sub(M,E),w,P),K=c(_,T.sub(C,d),w,P),q=c(T.add(m,y),E,w,P),x=c(T.sub(S,m),T.add(d,M),w,P),I=c(T.sub(y,_),T.add(C,E),w,P),k=T.add(v,K);k.sub(q),k.add(I);let wt=T.add(U,q),A=T.add(L,K),O=T.sub(v,L);O.add(U),O.add(x);let F=T.zeros(2*k.rows,2*k.columns);return F=F.setSubMatrix(k,0,0),F=F.setSubMatrix(wt,k.rows,0),F=F.setSubMatrix(A,0,k.columns),F=F.setSubMatrix(O,k.rows,k.columns),F.subMatrix(0,p-1,0,g-1)}return c(n,t,a,h)}scaleRows(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{min:n=0,max:e=1}=t;if(!Number.isFinite(n))throw new TypeError("min must be a number");if(!Number.isFinite(e))throw new TypeError("max must be a number");if(n>=e)throw new RangeError("min must be smaller than max");let s=new j(this.rows,this.columns);for(let r=0;r<this.rows;r++){const o=this.getRow(r);o.length>0&&Te(o,{min:n,max:e,output:o}),s.setRow(r,o)}return s}scaleColumns(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{min:n=0,max:e=1}=t;if(!Number.isFinite(n))throw new TypeError("min must be a number");if(!Number.isFinite(e))throw new TypeError("max must be a number");if(n>=e)throw new RangeError("min must be smaller than max");let s=new j(this.rows,this.columns);for(let r=0;r<this.columns;r++){const o=this.getColumn(r);o.length&&Te(o,{min:n,max:e,output:o}),s.setColumn(r,o)}return s}flipRows(){const t=Math.ceil(this.columns/2);for(let n=0;n<this.rows;n++)for(let e=0;e<t;e++){let s=this.get(n,e),r=this.get(n,this.columns-1-e);this.set(n,e,r),this.set(n,this.columns-1-e,s)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let n=0;n<this.columns;n++)for(let e=0;e<t;e++){let s=this.get(e,n),r=this.get(this.rows-1-e,n);this.set(e,n,r),this.set(this.rows-1-e,n,s)}return this}kroneckerProduct(t){t=j.checkMatrix(t);let n=this.rows,e=this.columns,s=t.rows,r=t.columns,o=new j(n*s,e*r);for(let l=0;l<n;l++)for(let a=0;a<e;a++)for(let h=0;h<s;h++)for(let c=0;c<r;c++)o.set(s*l+h,r*a+c,this.get(l,a)*t.get(h,c));return o}kroneckerSum(t){if(t=j.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let n=this.rows,e=t.rows,s=this.kroneckerProduct(j.eye(e,e)),r=j.eye(n,n).kroneckerProduct(t);return s.add(r)}transpose(){let t=new j(this.columns,this.rows);for(let n=0;n<this.rows;n++)for(let e=0;e<this.columns;e++)t.set(e,n,this.get(n,e));return t}sortRows(t=qe){for(let n=0;n<this.rows;n++)this.setRow(n,this.getRow(n).sort(t));return this}sortColumns(t=qe){for(let n=0;n<this.columns;n++)this.setColumn(n,this.getColumn(n).sort(t));return this}subMatrix(t,n,e,s){Ae(this,t,n,e,s);let r=new j(n-t+1,s-e+1);for(let o=t;o<=n;o++)for(let l=e;l<=s;l++)r.set(o-t,l-e,this.get(o,l));return r}subMatrixRow(t,n,e){if(n===void 0&&(n=0),e===void 0&&(e=this.columns-1),n>e||n<0||n>=this.columns||e<0||e>=this.columns)throw new RangeError("Argument out of range");let s=new j(t.length,e-n+1);for(let r=0;r<t.length;r++)for(let o=n;o<=e;o++){if(t[r]<0||t[r]>=this.rows)throw new RangeError(`Row index out of range: ${t[r]}`);s.set(r,o-n,this.get(t[r],o))}return s}subMatrixColumn(t,n,e){if(n===void 0&&(n=0),e===void 0&&(e=this.rows-1),n>e||n<0||n>=this.rows||e<0||e>=this.rows)throw new RangeError("Argument out of range");let s=new j(e-n+1,t.length);for(let r=0;r<t.length;r++)for(let o=n;o<=e;o++){if(t[r]<0||t[r]>=this.columns)throw new RangeError(`Column index out of range: ${t[r]}`);s.set(o-n,r,this.get(o,t[r]))}return s}setSubMatrix(t,n,e){if(t=j.checkMatrix(t),t.isEmpty())return this;let s=n+t.rows-1,r=e+t.columns-1;Ae(this,n,s,e,r);for(let o=0;o<t.rows;o++)for(let l=0;l<t.columns;l++)this.set(n+o,e+l,t.get(o,l));return this}selection(t,n){On(this,t),Ln(this,n);let e=new j(t.length,n.length);for(let s=0;s<t.length;s++){let r=t[s];for(let o=0;o<n.length;o++){let l=n[o];e.set(s,o,this.get(r,l))}}return e}trace(){let t=Math.min(this.rows,this.columns),n=0;for(let e=0;e<t;e++)n+=this.get(e,e);return n}clone(){let t=new j(this.rows,this.columns);for(let n=0;n<this.rows;n++)for(let e=0;e<this.columns;e++)t.set(n,e,this.get(n,e));return t}sum(t){switch(t){case"row":return $n(this);case"column":return zn(this);case void 0:return Nn(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return Dn(this);case"column":return Vn(this);case void 0:return Yn(this);default:throw new Error(`invalid option: ${t}`)}}mean(t){const n=this.sum(t);switch(t){case"row":{for(let e=0;e<this.rows;e++)n[e]/=this.columns;return n}case"column":{for(let e=0;e<this.columns;e++)n[e]/=this.rows;return n}case void 0:return n/this.size;default:throw new Error(`invalid option: ${t}`)}}variance(t,n={}){if(typeof t=="object"&&(n=t,t=void 0),typeof n!="object")throw new TypeError("options must be an object");const{unbiased:e=!0,mean:s=this.mean(t)}=n;if(typeof e!="boolean")throw new TypeError("unbiased must be a boolean");switch(t){case"row":{if(!st(s))throw new TypeError("mean must be an array");return Un(this,e,s)}case"column":{if(!st(s))throw new TypeError("mean must be an array");return Xn(this,e,s)}case void 0:{if(typeof s!="number")throw new TypeError("mean must be a number");return Gn(this,e,s)}default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,n){typeof t=="object"&&(n=t,t=void 0);const e=this.variance(t,n);if(t===void 0)return Math.sqrt(e);for(let s=0;s<e.length;s++)e[s]=Math.sqrt(e[s]);return e}center(t,n={}){if(typeof t=="object"&&(n=t,t=void 0),typeof n!="object")throw new TypeError("options must be an object");const{center:e=this.mean(t)}=n;switch(t){case"row":{if(!st(e))throw new TypeError("center must be an array");return Wn(this,e),this}case"column":{if(!st(e))throw new TypeError("center must be an array");return Qn(this,e),this}case void 0:{if(typeof e!="number")throw new TypeError("center must be a number");return Jn(this,e),this}default:throw new Error(`invalid option: ${t}`)}}scale(t,n={}){if(typeof t=="object"&&(n=t,t=void 0),typeof n!="object")throw new TypeError("options must be an object");let e=n.scale;switch(t){case"row":{if(e===void 0)e=Zn(this);else if(!st(e))throw new TypeError("scale must be an array");return Kn(this,e),this}case"column":{if(e===void 0)e=Hn(this);else if(!st(e))throw new TypeError("scale must be an array");return ts(this,e),this}case void 0:{if(e===void 0)e=es(this);else if(typeof e!="number")throw new TypeError("scale must be a number");return ns(this,e),this}default:throw new Error(`invalid option: ${t}`)}}toString(t){return ln(this,t)}}T.prototype.klass="Matrix";typeof Symbol<"u"&&(T.prototype[Symbol.for("nodejs.util.inspect.custom")]=Rn);function qe(i,t){return i-t}function ss(i){return i.every(t=>typeof t=="number")}T.random=T.rand;T.randomInt=T.randInt;T.diagonal=T.diag;T.prototype.diagonal=T.prototype.diag;T.identity=T.eye;T.prototype.negate=T.prototype.neg;T.prototype.tensorProduct=T.prototype.kroneckerProduct;class j extends T{constructor(t,n){if(super(),j.isMatrix(t))return t.clone();if(Number.isInteger(t)&&t>=0)if(this.data=[],Number.isInteger(n)&&n>=0)for(let e=0;e<t;e++)this.data.push(new Float64Array(n));else throw new TypeError("nColumns must be a positive integer");else if(st(t)){const e=t;if(t=e.length,n=t?e[0].length:0,typeof n!="number")throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let s=0;s<t;s++){if(e[s].length!==n)throw new RangeError("Inconsistent array dimensions");if(!ss(e[s]))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(e[s]))}}else throw new TypeError("First argument must be a positive number or an array");this.rows=t,this.columns=n}set(t,n,e){return this.data[t][n]=e,this}get(t,n){return this.data[t][n]}removeRow(t){return ot(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,n){return n===void 0&&(n=t,t=this.rows),ot(this,t,!0),n=Float64Array.from(kt(this,n)),this.data.splice(t,0,n),this.rows+=1,this}removeColumn(t){lt(this,t);for(let n=0;n<this.rows;n++){const e=new Float64Array(this.columns-1);for(let s=0;s<t;s++)e[s]=this.data[n][s];for(let s=t+1;s<this.columns;s++)e[s-1]=this.data[n][s];this.data[n]=e}return this.columns-=1,this}addColumn(t,n){typeof n>"u"&&(n=t,t=this.columns),lt(this,t,!0),n=It(this,n);for(let e=0;e<this.rows;e++){const s=new Float64Array(this.columns+1);let r=0;for(;r<t;r++)s[r]=this.data[e][r];for(s[r++]=n[e];r<this.columns+1;r++)s[r]=this.data[e][r-1];this.data[e]=s}return this.columns+=1,this}}Bn(T,j);class is extends T{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,n,e){return this.data[t][n]=e,this}get(t,n){return this.data[t][n]}}function Be(i,t){let n=0;return Math.abs(i)>Math.abs(t)?(n=t/i,Math.abs(i)*Math.sqrt(1+n*n)):t!==0?(n=i/t,Math.abs(t)*Math.sqrt(1+n*n)):0}class rs{constructor(t,n={}){const{assumeSymmetric:e=!1}=n;if(t=is.checkMatrix(t),!t.isSquare())throw new Error("Matrix is not a square matrix");if(t.isEmpty())throw new Error("Matrix must be non-empty");let s=t.columns,r=new j(s,s),o=new Float64Array(s),l=new Float64Array(s),a=t,h,c,u=!1;if(e?u=!0:u=t.isSymmetric(),u){for(h=0;h<s;h++)for(c=0;c<s;c++)r.set(h,c,a.get(h,c));os(s,l,o,r),ls(s,l,o,r)}else{let f=new j(s,s),p=new Float64Array(s);for(c=0;c<s;c++)for(h=0;h<s;h++)f.set(h,c,a.get(h,c));hs(s,f,p,r),as(s,l,o,r,f)}this.n=s,this.e=l,this.d=o,this.V=r}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let t=this.n,n=this.e,e=this.d,s=new j(t,t),r,o;for(r=0;r<t;r++){for(o=0;o<t;o++)s.set(r,o,0);s.set(r,r,e[r]),n[r]>0?s.set(r,r+1,n[r]):n[r]<0&&s.set(r,r-1,n[r])}return s}}function os(i,t,n,e){let s,r,o,l,a,h,c,u;for(a=0;a<i;a++)n[a]=e.get(i-1,a);for(l=i-1;l>0;l--){for(u=0,o=0,h=0;h<l;h++)u=u+Math.abs(n[h]);if(u===0)for(t[l]=n[l-1],a=0;a<l;a++)n[a]=e.get(l-1,a),e.set(l,a,0),e.set(a,l,0);else{for(h=0;h<l;h++)n[h]/=u,o+=n[h]*n[h];for(s=n[l-1],r=Math.sqrt(o),s>0&&(r=-r),t[l]=u*r,o=o-s*r,n[l-1]=s-r,a=0;a<l;a++)t[a]=0;for(a=0;a<l;a++){for(s=n[a],e.set(a,l,s),r=t[a]+e.get(a,a)*s,h=a+1;h<=l-1;h++)r+=e.get(h,a)*n[h],t[h]+=e.get(h,a)*s;t[a]=r}for(s=0,a=0;a<l;a++)t[a]/=o,s+=t[a]*n[a];for(c=s/(o+o),a=0;a<l;a++)t[a]-=c*n[a];for(a=0;a<l;a++){for(s=n[a],r=t[a],h=a;h<=l-1;h++)e.set(h,a,e.get(h,a)-(s*t[h]+r*n[h]));n[a]=e.get(l-1,a),e.set(l,a,0)}}n[l]=o}for(l=0;l<i-1;l++){if(e.set(i-1,l,e.get(l,l)),e.set(l,l,1),o=n[l+1],o!==0){for(h=0;h<=l;h++)n[h]=e.get(h,l+1)/o;for(a=0;a<=l;a++){for(r=0,h=0;h<=l;h++)r+=e.get(h,l+1)*e.get(h,a);for(h=0;h<=l;h++)e.set(h,a,e.get(h,a)-r*n[h])}}for(h=0;h<=l;h++)e.set(h,l+1,0)}for(a=0;a<i;a++)n[a]=e.get(i-1,a),e.set(i-1,a,0);e.set(i-1,i-1,1),t[0]=0}function ls(i,t,n,e){let s,r,o,l,a,h,c,u,f,p,g,w,P,m,d,y;for(o=1;o<i;o++)t[o-1]=t[o];t[i-1]=0;let M=0,S=0,C=Number.EPSILON;for(h=0;h<i;h++){for(S=Math.max(S,Math.abs(n[h])+Math.abs(t[h])),c=h;c<i&&!(Math.abs(t[c])<=C*S);)c++;if(c>h)do{for(s=n[h],u=(n[h+1]-s)/(2*t[h]),f=Be(u,1),u<0&&(f=-f),n[h]=t[h]/(u+f),n[h+1]=t[h]*(u+f),p=n[h+1],r=s-n[h],o=h+2;o<i;o++)n[o]-=r;for(M=M+r,u=n[c],g=1,w=g,P=g,m=t[h+1],d=0,y=0,o=c-1;o>=h;o--)for(P=w,w=g,y=d,s=g*t[o],r=g*u,f=Be(u,t[o]),t[o+1]=d*f,d=t[o]/f,g=u/f,u=g*n[o]-d*s,n[o+1]=r+d*(g*s+d*n[o]),a=0;a<i;a++)r=e.get(a,o+1),e.set(a,o+1,d*e.get(a,o)+g*r),e.set(a,o,g*e.get(a,o)-d*r);u=-d*y*P*m*t[h]/p,t[h]=d*u,n[h]=g*u}while(Math.abs(t[h])>C*S);n[h]=n[h]+M,t[h]=0}for(o=0;o<i-1;o++){for(a=o,u=n[o],l=o+1;l<i;l++)n[l]<u&&(a=l,u=n[l]);if(a!==o)for(n[a]=n[o],n[o]=u,l=0;l<i;l++)u=e.get(l,o),e.set(l,o,e.get(l,a)),e.set(l,a,u)}}function hs(i,t,n,e){let s=0,r=i-1,o,l,a,h,c,u,f;for(u=s+1;u<=r-1;u++){for(f=0,h=u;h<=r;h++)f=f+Math.abs(t.get(h,u-1));if(f!==0){for(a=0,h=r;h>=u;h--)n[h]=t.get(h,u-1)/f,a+=n[h]*n[h];for(l=Math.sqrt(a),n[u]>0&&(l=-l),a=a-n[u]*l,n[u]=n[u]-l,c=u;c<i;c++){for(o=0,h=r;h>=u;h--)o+=n[h]*t.get(h,c);for(o=o/a,h=u;h<=r;h++)t.set(h,c,t.get(h,c)-o*n[h])}for(h=0;h<=r;h++){for(o=0,c=r;c>=u;c--)o+=n[c]*t.get(h,c);for(o=o/a,c=u;c<=r;c++)t.set(h,c,t.get(h,c)-o*n[c])}n[u]=f*n[u],t.set(u,u-1,f*l)}}for(h=0;h<i;h++)for(c=0;c<i;c++)e.set(h,c,h===c?1:0);for(u=r-1;u>=s+1;u--)if(t.get(u,u-1)!==0){for(h=u+1;h<=r;h++)n[h]=t.get(h,u-1);for(c=u;c<=r;c++){for(l=0,h=u;h<=r;h++)l+=n[h]*e.get(h,c);for(l=l/n[u]/t.get(u,u-1),h=u;h<=r;h++)e.set(h,c,e.get(h,c)+l*n[h])}}}function as(i,t,n,e,s){let r=i-1,o=0,l=i-1,a=Number.EPSILON,h=0,c=0,u=0,f=0,p=0,g=0,w=0,P=0,m,d,y,M,S,C,_,E,v,L,U,K,q,x,I;for(m=0;m<i;m++)for((m<o||m>l)&&(n[m]=s.get(m,m),t[m]=0),d=Math.max(m-1,0);d<i;d++)c=c+Math.abs(s.get(m,d));for(;r>=o;){for(M=r;M>o&&(g=Math.abs(s.get(M-1,M-1))+Math.abs(s.get(M,M)),g===0&&(g=c),!(Math.abs(s.get(M,M-1))<a*g));)M--;if(M===r)s.set(r,r,s.get(r,r)+h),n[r]=s.get(r,r),t[r]=0,r--,P=0;else if(M===r-1){if(_=s.get(r,r-1)*s.get(r-1,r),u=(s.get(r-1,r-1)-s.get(r,r))/2,f=u*u+_,w=Math.sqrt(Math.abs(f)),s.set(r,r,s.get(r,r)+h),s.set(r-1,r-1,s.get(r-1,r-1)+h),E=s.get(r,r),f>=0){for(w=u>=0?u+w:u-w,n[r-1]=E+w,n[r]=n[r-1],w!==0&&(n[r]=E-_/w),t[r-1]=0,t[r]=0,E=s.get(r,r-1),g=Math.abs(E)+Math.abs(w),u=E/g,f=w/g,p=Math.sqrt(u*u+f*f),u=u/p,f=f/p,d=r-1;d<i;d++)w=s.get(r-1,d),s.set(r-1,d,f*w+u*s.get(r,d)),s.set(r,d,f*s.get(r,d)-u*w);for(m=0;m<=r;m++)w=s.get(m,r-1),s.set(m,r-1,f*w+u*s.get(m,r)),s.set(m,r,f*s.get(m,r)-u*w);for(m=o;m<=l;m++)w=e.get(m,r-1),e.set(m,r-1,f*w+u*e.get(m,r)),e.set(m,r,f*e.get(m,r)-u*w)}else n[r-1]=E+u,n[r]=E+u,t[r-1]=w,t[r]=-w;r=r-2,P=0}else{if(E=s.get(r,r),v=0,_=0,M<r&&(v=s.get(r-1,r-1),_=s.get(r,r-1)*s.get(r-1,r)),P===10){for(h+=E,m=o;m<=r;m++)s.set(m,m,s.get(m,m)-E);g=Math.abs(s.get(r,r-1))+Math.abs(s.get(r-1,r-2)),E=v=.75*g,_=-.4375*g*g}if(P===30&&(g=(v-E)/2,g=g*g+_,g>0)){for(g=Math.sqrt(g),v<E&&(g=-g),g=E-_/((v-E)/2+g),m=o;m<=r;m++)s.set(m,m,s.get(m,m)-g);h+=g,E=v=_=.964}for(P=P+1,S=r-2;S>=M&&(w=s.get(S,S),p=E-w,g=v-w,u=(p*g-_)/s.get(S+1,S)+s.get(S,S+1),f=s.get(S+1,S+1)-w-p-g,p=s.get(S+2,S+1),g=Math.abs(u)+Math.abs(f)+Math.abs(p),u=u/g,f=f/g,p=p/g,!(S===M||Math.abs(s.get(S,S-1))*(Math.abs(f)+Math.abs(p))<a*(Math.abs(u)*(Math.abs(s.get(S-1,S-1))+Math.abs(w)+Math.abs(s.get(S+1,S+1))))));)S--;for(m=S+2;m<=r;m++)s.set(m,m-2,0),m>S+2&&s.set(m,m-3,0);for(y=S;y<=r-1&&(x=y!==r-1,y!==S&&(u=s.get(y,y-1),f=s.get(y+1,y-1),p=x?s.get(y+2,y-1):0,E=Math.abs(u)+Math.abs(f)+Math.abs(p),E!==0&&(u=u/E,f=f/E,p=p/E)),E!==0);y++)if(g=Math.sqrt(u*u+f*f+p*p),u<0&&(g=-g),g!==0){for(y!==S?s.set(y,y-1,-g*E):M!==S&&s.set(y,y-1,-s.get(y,y-1)),u=u+g,E=u/g,v=f/g,w=p/g,f=f/u,p=p/u,d=y;d<i;d++)u=s.get(y,d)+f*s.get(y+1,d),x&&(u=u+p*s.get(y+2,d),s.set(y+2,d,s.get(y+2,d)-u*w)),s.set(y,d,s.get(y,d)-u*E),s.set(y+1,d,s.get(y+1,d)-u*v);for(m=0;m<=Math.min(r,y+3);m++)u=E*s.get(m,y)+v*s.get(m,y+1),x&&(u=u+w*s.get(m,y+2),s.set(m,y+2,s.get(m,y+2)-u*p)),s.set(m,y,s.get(m,y)-u),s.set(m,y+1,s.get(m,y+1)-u*f);for(m=o;m<=l;m++)u=E*e.get(m,y)+v*e.get(m,y+1),x&&(u=u+w*e.get(m,y+2),e.set(m,y+2,e.get(m,y+2)-u*p)),e.set(m,y,e.get(m,y)-u),e.set(m,y+1,e.get(m,y+1)-u*f)}}}if(c!==0){for(r=i-1;r>=0;r--)if(u=n[r],f=t[r],f===0)for(M=r,s.set(r,r,1),m=r-1;m>=0;m--){for(_=s.get(m,m)-u,p=0,d=M;d<=r;d++)p=p+s.get(m,d)*s.get(d,r);if(t[m]<0)w=_,g=p;else if(M=m,t[m]===0?s.set(m,r,_!==0?-p/_:-p/(a*c)):(E=s.get(m,m+1),v=s.get(m+1,m),f=(n[m]-u)*(n[m]-u)+t[m]*t[m],C=(E*g-w*p)/f,s.set(m,r,C),s.set(m+1,r,Math.abs(E)>Math.abs(w)?(-p-_*C)/E:(-g-v*C)/w)),C=Math.abs(s.get(m,r)),a*C*C>1)for(d=m;d<=r;d++)s.set(d,r,s.get(d,r)/C)}else if(f<0)for(M=r-1,Math.abs(s.get(r,r-1))>Math.abs(s.get(r-1,r))?(s.set(r-1,r-1,f/s.get(r,r-1)),s.set(r-1,r,-(s.get(r,r)-u)/s.get(r,r-1))):(I=Qt(0,-s.get(r-1,r),s.get(r-1,r-1)-u,f),s.set(r-1,r-1,I[0]),s.set(r-1,r,I[1])),s.set(r,r-1,0),s.set(r,r,1),m=r-2;m>=0;m--){for(L=0,U=0,d=M;d<=r;d++)L=L+s.get(m,d)*s.get(d,r-1),U=U+s.get(m,d)*s.get(d,r);if(_=s.get(m,m)-u,t[m]<0)w=_,p=L,g=U;else if(M=m,t[m]===0?(I=Qt(-L,-U,_,f),s.set(m,r-1,I[0]),s.set(m,r,I[1])):(E=s.get(m,m+1),v=s.get(m+1,m),K=(n[m]-u)*(n[m]-u)+t[m]*t[m]-f*f,q=(n[m]-u)*2*f,K===0&&q===0&&(K=a*c*(Math.abs(_)+Math.abs(f)+Math.abs(E)+Math.abs(v)+Math.abs(w))),I=Qt(E*p-w*L+f*U,E*g-w*U-f*L,K,q),s.set(m,r-1,I[0]),s.set(m,r,I[1]),Math.abs(E)>Math.abs(w)+Math.abs(f)?(s.set(m+1,r-1,(-L-_*s.get(m,r-1)+f*s.get(m,r))/E),s.set(m+1,r,(-U-_*s.get(m,r)-f*s.get(m,r-1))/E)):(I=Qt(-p-v*s.get(m,r-1),-g-v*s.get(m,r),w,f),s.set(m+1,r-1,I[0]),s.set(m+1,r,I[1]))),C=Math.max(Math.abs(s.get(m,r-1)),Math.abs(s.get(m,r))),a*C*C>1)for(d=m;d<=r;d++)s.set(d,r-1,s.get(d,r-1)/C),s.set(d,r,s.get(d,r)/C)}for(m=0;m<i;m++)if(m<o||m>l)for(d=m;d<i;d++)e.set(m,d,s.get(m,d));for(d=i-1;d>=o;d--)for(m=o;m<=l;m++){for(w=0,y=o;y<=Math.min(d,l);y++)w=w+e.get(m,y)*s.get(y,d);e.set(m,d,w)}}}function Qt(i,t,n,e){let s,r;return Math.abs(n)>Math.abs(e)?(s=e/n,r=n+s*e,[(i+s*t)/r,(t-s*i)/r]):(s=n/e,r=e+s*n,[(s*i+t)/r,(s*t-i)/r])}function se(i,t,n){const e=i/n,s=t/n;if(!(isFinite(e)&&isFinite(s))){const a=-i/t;return isFinite(a)?[a]:i==0&&t==0?[0]:[]}const r=s*s-4*e;let o=0;if(isFinite(r)){if(r<0)return[];if(r==0)return[-.5*s];o=-.5*(s+xt(Math.sqrt(r),s))}else o=-s;const l=e/o;return isFinite(l)?l>o?[o,l]:[l,o]:[o]}function hn(i,t,n,e){const s=n/(3*e),r=t/(3*e),o=i/e;if(!(isFinite(o)&&isFinite(r)&&isFinite(s)))return se(i,t,n);const l=-s*s+r,a=-r*s+o,h=s*o-r*r,c=4*l*h-a*a,u=-2*s*l+a;if(c<0){const f=Math.sqrt(-.25*c),p=-.5*u;return[Math.cbrt(p+f)+Math.cbrt(p-f)-s]}else if(c==0){const f=xt(Math.sqrt(-l),u);return[f-s,-2*f-s]}else{const f=Math.atan2(Math.sqrt(c),-u)/3,p=Math.cos(f),g=Math.sin(f)*Math.sqrt(3),w=.5*(-p+g),P=.5*(-p-g),m=2*Math.sqrt(-l);return[m*p-s,m*w-s,m*P-s]}}function us(i,t,n,e,s){if(s==0)return hn(i,t,n,e);const r=e/s,o=n/s,l=t/s,a=i/s;let h=Oe(r,o,l,a,!1);if(h!==null)return h;const c=716e74;for(let u=0;u<2;u++)if(h=Oe(r/c,o/(c*c),l/(c*c*c),a/(c*c*c*c),u!=0),h!==null){for(let f=0;f<h.length;f++)h[f]*=c;return h}return[]}function Mt(i,t){return t==0?Math.abs(i):Math.abs((i-t)/t)}function Oe(i,t,n,e,s){const r=cs(i,t,n,e,s);if(r!==null&&r.length==4){let o=[];for(let l=0;l<2;l++){const a=r[l*2],h=r[l*2+1];o=o.concat(se(h,a,1))}return o}return null}function cs(i,t,n,e,s){function r(A,O,F,D){const X=Mt(A+F,i),V=Mt(O+A*F+D,t),Z=Mt(O*F+A*D,n);return X+V+Z}function o(A,O,F,D){return r(A,O,F,D)+Mt(O*D,e)}const l=9*i*i-24*t,a=l>=0?-2*t/(3*i+xt(Math.sqrt(l),i)):-.25*i,h=i+4*a,c=t+3*a*(i+2*a),u=n+a*(2*t+a*(3*i+4*a)),f=e+a*(n+a*(t+a*(i+a)));let p=0,g=0;const w=349e100;if(s){const A=h/w,O=c/w,F=u/w,D=f/w;p=A*F-4/w*D-1/3*O*O,g=(A*F-8/w*D-2/9*O*O)*(1/3)*O-F*(F/w)-A*A*D}else p=h*u-4*f-1/3*c*c,g=(h*u+8*f-2/9*c*c)*(1/3)*c-u*u-h*h*f;if(!isFinite(p)&&isFinite(g))return null;let P=fs(p,g);s&&(P*=w);const m=i*.5,d=1/6*t+.5*P,y=n-i*d,M=2/3*t-P-m*m,S=.5*y/M,C=2*(e-d*d)/y,_=.5*y/C;let E=0,v=0,L=0;for(let A=0;A<3;A++){const O=A==1?_:M,F=A==0?S:C,D=Mt(O+m*m+2*d,t),X=Mt(2*(O*F+m*d),n),V=Mt(O*F*F+d*d,e),Z=D+X+V;(A==0||Z<L)&&(E=O,v=F,L=Z)}const U=E,K=v;let q=0,x=0,I=0,k=0;if(U<0){const A=Math.sqrt(-U);if(q=m+A,x=d+A*K,I=m-A,k=d-A*K,Math.abs(k)<Math.abs(x)?k=e/x:Math.abs(k)>Math.abs(x)&&(x=e/k),Math.abs(q)!=Math.abs(I)){let O=null,F=null;if(Math.abs(q)<Math.abs(I)){const X=(n-x*I)/k,V=(t-k-x)/I;O=[i-I,X,V],F=[I,I,I]}else{const X=(n-q*k)/x,V=(t-k-x)/q,Z=i-q;O=[q,q,q],F=[Z,X,V]}let D=0;for(let X=0;X<3;X++){const V=O[X],Z=F[X];if(isFinite(V)&&isFinite(Z)){const ft=r(V,x,Z,k);(X==0||ft<D)&&(q=V,I=Z,D=ft)}}}}else if(U==0){const A=e-d*d;q=m,x=d+Math.sqrt(-A),I=m,k=d-Math.sqrt(-A),Math.abs(x)>Math.abs(k)?k=e/x:Math.abs(k)>Math.abs(x)&&(x=e/k)}else return[];let wt=o(q,x,I,k);for(let A=0;A<8&&wt!=0;A++){const O=x*k-e,F=x*I+q*k-n,D=x+q*I+k-t,X=q+I-i,V=q-I,Z=x*x-x*(I*V+2*k)+k*(q*V+k);if(Z==0)break;const ft=1/Z,at=k-x,dt=x*I-q*k,ie=V*O+at*F+dt*D-(x*at+q*dt)*X,re=(q*V+at)*O-x*(V*F+at*D+dt*X),oe=-V*O-at*F-dt*D+(I*dt+k*at)*X,le=-(I*V+at)*O+k*(V*F+at*D+dt*X),$t=q-ft*ie,zt=x-ft*re,Nt=I-ft*oe,Dt=k-ft*le,Vt=o($t,zt,Nt,Dt);if(Vt<wt)q=$t,x=zt,I=Nt,k=Dt,wt=Vt;else break}return[q,x,I,k]}function fs(i,t){const n=-.3333333333333333*i,e=.5*t;let s,r=null;if((Math.abs(n)>=1e102||Math.abs(e)>=1e164)&&(Math.abs(n)<Math.abs(e)?r=1-n*(n/e)*(n/e):r=Math.sign(n)*(e/n*(e/n)/n-1)),r!==null&&e==0)i>0?s=0:s=Math.sqrt(-i);else if(r!==null?r<0:e*e<n*n*n){const h=r!==null?e/n/Math.sqrt(n):e/Math.sqrt(n*n*n);s=-2*Math.sqrt(n)*xt(Math.cos(Math.acos(Math.abs(h))*(1/3)),h)}else{let h;r!==null?Math.abs(n)<Math.abs(e)?h=-e*(1+Math.sqrt(r)):h=-e-xt(Math.sqrt(Math.abs(n))*n*Math.sqrt(r),e):h=Math.cbrt(-e-xt(Math.sqrt(e*e-n*n*n),e));const c=h==0?0:n/h;s=h+c}let o=s,l=(o*o+i)*o+t;const a=222045e-21;if(Math.abs(l)<a*Math.max(o*o*o,i*o,t))return o;for(let h=0;h<8;h++){const c=3*o*o+i;if(c==0)break;const u=o-l/c,f=(u*u+i)*u+t;if(f==0)return u;if(Math.abs(f)>=Math.abs(l))break;o=u,l=f}return o}function xt(i,t){const n=Math.abs(i);return t<0?-n:n}function Pe(i,t=1e-9){if(i[i.length-1]==0)return Pe(i.slice(0,-1),t);const n=new j([i.slice(0,-1).reverse().map(s=>-s/i[i.length-1]),...Array.from({length:i.length-2},(s,r)=>{const o=new Array(i.length-1).fill(0);return o[r]=1,o})]),e=new rs(n);return e.realEigenvalues.filter((s,r)=>Math.abs(e.imaginaryEigenvalues[r])<t)}const ms=(i,t,n)=>[i[0]*(1-n)+t[0]*n,i[1]*(1-n)+t[1]*n];function gs(i){const t=[];for(let n=0;n<i.length-1;n++)t.push([i[n],i[n+1]]);return t}function ye(i,t){const n=gs(i).map(([e,s])=>ms(e,s,t));return i.length===2?[n]:[...ye(n,t),n]}class Y extends Ot{constructor(t,n,e,s){super(t,n),this.segmentType="CUBIC_BEZIER",this._boundingBox=null,this._polynomialCoefficients=null,this.firstControlPoint=e,this.lastControlPoint=s}get midPoint(){return this.paramPoint(.5)}get boundingBox(){if(this._boundingBox===null){const t=n=>{const[e,s,r,o]=[this.firstPoint[n],this.firstControlPoint[n],this.lastControlPoint[n],this.lastPoint[n]],l=-e+3*s-3*r+o,a=2*e-4*s+2*r,h=-e+s;return se(h,a,l).filter(c=>c>=-this.precision&&c<=1+this.precision)};this._boundingBox=we([this.firstPoint,...t(0).map(n=>this.paramPoint(n)),...t(1).map(n=>this.paramPoint(n)),this.lastPoint]).grow(this.precision)}return this._boundingBox}clone(){return new Y(this.firstPoint,this.lastPoint,this.firstControlPoint,this.lastControlPoint)}reverse(){return new Y(this.lastPoint,this.firstPoint,this.lastControlPoint,this.firstControlPoint)}isSame(t){return t.segmentType!=="CUBIC_BEZIER"?!1:b(this.firstPoint,t.firstPoint)&&b(this.lastPoint,t.lastPoint)&&b(this.firstControlPoint,t.firstControlPoint)&&b(this.lastControlPoint,t.lastControlPoint)}distanceFrom(t){const e=rn(s=>yt(this.paramPoint(s),t),this.precision);return e.argMin<-this.precision||e.argMin>1+this.precision?Math.min(G(this.firstPoint,t),G(this.lastPoint,t)):Math.sqrt(e.fMin)}isOnSegment(t){if(!this.boundingBox.contains(t))return!1;try{const n=this.pointToParam(t);return n>=-this.precision&&n<=1+this.precision}catch{return!1}}gradientAt(t){const n=1-t,e=n*n,s=t*t,r=3*e,o=6*n*t,l=3*s,a=$(this.firstControlPoint,this.firstPoint),h=$(this.lastControlPoint,this.firstControlPoint),c=$(this.lastPoint,this.lastControlPoint);return[r*a[0]+o*h[0]+l*c[0],r*a[1]+o*h[1]+l*c[1]]}tangentAt(t){const n=this.pointToParam(t);return Q(this.gradientAt(n))}get tangentAtFirstPoint(){return Q($(this.firstControlPoint,this.firstPoint))}get tangentAtLastPoint(){return Q($(this.lastControlPoint,this.lastPoint))}normalAt(t){const n=this.tangentAt(t);return et(n)}splitAtParameters(t,n=null){const e=[...t];e.sort((r,o)=>r-o);let s={originalParam:0,param:0,p0:this.firstPoint,p1:this.firstControlPoint,p2:this.lastControlPoint};return e.flatMap((r,o)=>{if(r-s.originalParam<this.precision)return[];let l=!1;r>1-this.precision&&(l=!0);const a=(r-s.originalParam)/(1-s.originalParam),h=ye([s.p0,s.p1,s.p2,this.lastPoint],l?1:a),c=l?this.lastPoint:(n==null?void 0:n.get(r))??h[0][0],u=h[1][0],f=h[2][0],p=h[1][1],g=h[2][2],w=new Y(s.p0,c,f,u);return s={param:a,originalParam:r,p0:c,p1:p,p2:g},o===e.length-1&&!l?[w,new Y(s.p0,this.lastPoint,s.p1,s.p2)]:w})}splitAt(t){let n;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?n=t:n=[t];const e=n.map(r=>this.pointToParam(r)),s=new Map(ht([e,n]));return this.splitAtParameters(e,s)}transform(t){return new Y(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.firstControlPoint),t.transform(this.lastControlPoint))}paramPoint(t){const n=1-t,e=n*n,s=t*t,r=e*n,o=3*e*t,l=3*n*s,a=s*t;return[r*this.firstPoint[0]+o*this.firstControlPoint[0]+l*this.lastControlPoint[0]+a*this.lastPoint[0],r*this.firstPoint[1]+o*this.firstControlPoint[1]+l*this.lastControlPoint[1]+a*this.lastPoint[1]]}get polynomialCoefficients(){if(this._polynomialCoefficients===null){const t=n=>{const e=this.firstPoint[n],s=this.firstControlPoint[n],r=this.lastControlPoint[n],o=this.lastPoint[n],l=-e+3*s-3*r+o,a=3*e-6*s+3*r,h=-3*e+3*s;return[e,h,a,l]};this._polynomialCoefficients=[t(0),t(1)]}return this._polynomialCoefficients}paramsAtY(t){const[n,e,s,r]=this.polynomialCoefficients[1];return hn(n-t,e,s,r).filter(o=>o>=-this.precision&&o<=1+this.precision)}pointToParam(t){const n=this.paramsAtY(t[1]);if(n.length===0)throw new Error("Point is not on the curve");const e=r=>Math.abs(this.paramPoint(r)[0]-t[0])<=this.precision,s=n.find(e);if(s===void 0)throw new Error("Point is not on the curve");return s}}function an(i,t){const n=$(t,i.firstPoint),e=ae(n,i.V)/i.squareLength;return i.paramPoint(e)}function At(i,t,n){const e=n||i.precision,s=an(i,t.center),r=G(s,t.center);if(r>t.radius+e)return[];if(Math.abs(r-t.radius)<e){const u=s;return i.isOnSegment(u)&&t.isOnSegment(u)?[u]:[]}const o=[],l=Math.sqrt(t.radius*t.radius-r*r),a=i.tangentAtFirstPoint,h=J(s,nt(a,l));i.isOnSegment(h)&&t.isOnSegment(h)&&o.push(h);const c=J(s,nt(a,-l));return i.isOnSegment(c)&&t.isOnSegment(c)&&o.push(c),o}const ps=i=>{const{firstPoint:t,lastPoint:n,center:e,clockwise:s}=i;return new R(n,t,e,s,{ignoreChecks:!0})},ws=(i,t)=>{if(i.isSame(t))return[i];const n=pt([t.isOnSegment(i.firstPoint)?i.firstPoint:null,t.isOnSegment(i.lastPoint)?i.lastPoint:null,i.isOnSegment(t.firstPoint)?t.firstPoint:null,i.isOnSegment(t.lastPoint)?t.lastPoint:null].filter(e=>e!==null)).sort((e,s)=>i.pointToParam(e)-i.pointToParam(s));if(n.length===0)return[];if(n.length===1)return[];if(n.length===2)return i.isSame(ps(t))?[]:[new R(n[0],n[1],i.center,i.clockwise)];if(n.length===3){const e=b(n[0],t.lastPoint)||b(n[0],t.firstPoint)?1:0;return[new R(n[0+e],n[1+e],i.center,i.clockwise)]}else if(n.length===4)return[new R(n[0],n[1],i.center,i.clockwise),new R(n[2],n[3],i.center,i.clockwise)];throw new Error("Bug in the arc arc overlap algorithm")};function Me(i,t,n=!1,e){const s=e||i.precision,r=G(i.center,t.center),o=i.radius+t.radius;if(r>o+s)return[];const l=Math.abs(i.radius-t.radius);if(r<l-s)return[];if(r<s)return l>s?[]:n?ws(i,t):[];const a=Q($(t.center,i.center)),h=r>o-s;if(h||Math.abs(r-l)<s){const m=h||i.radius>t.radius?1:-1,d=J(i.center,nt(a,m*i.radius));return i.isOnSegment(d)&&t.isOnSegment(d)?[d]:[]}const c=i.radius*i.radius/(2*r)-t.radius*t.radius/(2*r)+r/2,u=J(i.center,nt(a,c)),f=Math.sqrt(i.radius*i.radius-c*c),p=et(a),g=J(u,nt(p,f)),w=J(u,nt(p,-f)),P=[];return i.isOnSegment(g)&&t.isOnSegment(g)&&P.push(g),i.isOnSegment(w)&&t.isOnSegment(w)&&P.push(w),P}class z extends Ot{constructor(t,n,e,s,r,o,l=!1,{ignoreChecks:a=!1,angleUnits:h="deg"}={}){super(t,n),this.segmentType="ELLIPSE_ARC",this.precision=1e-6,this._coefficients=null,this.center=e;const c=s>=r;this.majorRadius=c?s:r,this.minorRadius=c?r:s;const u=h==="deg"?o*ee:o;if(this.tiltAngle=bt(c?u:u+Math.PI/2),this.clockwise=l,!a){if(b(t,n))throw new Error("Invalid arc, cannot be a full circle");if(!this.isPointOnEllipse(t))throw new Error(`First point ${W(t)} not on the ellipse defined by ${this.info}`);if(!this.isPointOnEllipse(n))throw new Error(`Last point ${W(n)} not on the ellipse defined by ${this.info}`);if(Math.abs(this.majorRadius-this.minorRadius)<this.precision)throw new Error("Both radii should be different, create an arc instead")}}get coefficients(){if(this._coefficients===null){const[t,n]=this.center,e=this.majorRadius*this.majorRadius,s=this.minorRadius*this.minorRadius,r=Math.cos(this.tiltAngle),o=Math.sin(this.tiltAngle),l=e*o*o+s*r*r,a=2*(s-e)*o*r,h=e*r*r+s*o*o;this._coefficients={x2:l,xy:a,y2:h,x:-2*l*t-a*n,y:-2*h*n-a*t,c:l*t*t+a*t*n+h*n*n-e*s}}return this._coefficients}get info(){return`ELLIPSE_ARC(${W(this.firstPoint)}, ${W(this.lastPoint)}, ${W(this.center)}, ${this.majorRadius}, ${this.minorRadius}, ${this.tiltAngle*tn}, ${this.clockwise?"CW":"CCW"})`}reverse(){return new z(this.lastPoint,this.firstPoint,this.center,this.majorRadius,this.minorRadius,this.tiltAngle,!this.clockwise,{ignoreChecks:!0,angleUnits:"rad"})}clone(){return new z(this.firstPoint,this.lastPoint,this.center,this.majorRadius,this.minorRadius,this.tiltAngle,this.clockwise,{ignoreChecks:!0,angleUnits:"rad"})}computeEllipseExtrema(){const t=Math.tan(this.tiltAngle),n=this.minorRadius/this.majorRadius,e=-Math.atan(n*t),s=Math.atan(n/t);return[e,Math.PI+e,s,Math.PI+s].map(o=>this.thetaToParam(o)).filter(o=>this.isValidParameter(o)).map(o=>this.paramPoint(o))}get boundingBox(){if(this._boundingBox===void 0){const t=this.computeEllipseExtrema(),n=[this.firstPoint,this.lastPoint,...t];this._boundingBox=we(n).grow(this.precision)}return this._boundingBox}get linearExentricity(){return this._linearExentricity===void 0&&(this._linearExentricity=Math.sqrt(this.majorRadius*this.majorRadius-this.minorRadius*this.minorRadius)),this._linearExentricity}get exentricity(){return this._exentricity===void 0&&(this._exentricity=this.linearExentricity/this.majorRadius),this._exentricity}get focals(){if(this._focals===void 0){const t=this.majorAxis;this._focals=[J(this.center,nt(t,this.linearExentricity)),J(this.center,nt(t,-this.linearExentricity))]}return this._focals}get majorAxis(){return Ct(1,this.tiltAngle)}paramPoint(t){const n=this.firstAngle+t*this.deltaAngle*(this.clockwise?-1:1);return this.reverseEllipseReferenceFrameTransform.transform([this.majorRadius*Math.cos(n),this.minorRadius*Math.sin(n)])}pointToParam(t){if(!this.isPointOnEllipse(t))throw new Error(`Point ${W(t)} not on the ellipse defined by ${this.repr}`);const n=this.thetaToParam(this.pointTheta(t));if(!this.isValidParameter(n))throw new Error(`Point ${W(t)} is not on segment ${this.repr}`);return n}get midPoint(){return this.paramPoint(.5)}isValidParameter(t){return 1-t>=-this.precision&&t>=-this.precision}isSame(t){return b(this.center,t.center)&&Math.abs(this.majorRadius-t.majorRadius)<this.precision&&Math.abs(this.minorRadius-t.minorRadius)<this.precision&&(Math.abs(this.tiltAngle-t.tiltAngle)<this.precision||Math.abs(Math.abs(this.tiltAngle-t.tiltAngle)-Math.PI)<this.precision)&&(b(this.firstPoint,t.firstPoint)&&this.clockwise===t.clockwise||b(this.firstPoint,t.lastPoint)&&this.clockwise!==t.clockwise)}pointTheta(t){const n=this.ellipseReferenceFrameTransform.transform(t),e=Math.atan2(n[1]/this.minorRadius,n[0]/this.majorRadius);return bt(e)}thetaToParam(t){return Zt(this.firstAngle,bt(t),this.clockwise)/this.deltaAngle}isPointOnEllipse(t){const[n,e]=this.focals,s=G(t,n),r=G(t,e);return Math.abs(2*this.majorRadius-s-r)<this.precision}isOnSegment(t){return this.isPointOnEllipse(t)?this.isValidParameter(this.thetaToParam(this.pointTheta(t))):!1}distanceFrom(t){let n;if(b(t,this.center)?n=J(this.center,nt(et(this.majorAxis),this.minorRadius)):n=this.reverseEllipseReferenceFrameTransform.transform(ys(this.majorRadius,this.minorRadius,this.ellipseReferenceFrameTransform.transform(t))),this.isValidParameter(this.thetaToParam(this.pointTheta(t))))return G(t,n);if(En(t,this.majorAxis,this.center)){const e=bt(2*Math.PI-this.pointTheta(t)),s=this.thetaToParam(e);if(this.isValidParameter(s))return G(t,this.paramPoint(s))}return Math.min(G(t,this.firstPoint),G(t,this.lastPoint))}get ellipseReferenceFrameTransform(){return this._ellipseReferenceFrameTransform===void 0&&(this._ellipseReferenceFrameTransform=new H().rotate(-this.tiltAngle).translate(-this.center[0],-this.center[1])),this._ellipseReferenceFrameTransform}get reverseEllipseReferenceFrameTransform(){return this._reverseEllipseReferenceFrameTransform===void 0&&(this._reverseEllipseReferenceFrameTransform=new H().translate(this.center[0],this.center[1]).rotate(this.tiltAngle)),this._reverseEllipseReferenceFrameTransform}get rotateFromEllipseReferenceFrame(){return this._rotateFromEllipseReferenceFrame===void 0&&(this._rotateFromEllipseReferenceFrame=new H().rotate(this.tiltAngle)),this._rotateFromEllipseReferenceFrame}get firstAngle(){return this._firstAngle===void 0&&(this._firstAngle=this.pointTheta(this.firstPoint)),this._firstAngle}get lastAngle(){return this._lastAngle===void 0&&(this._lastAngle=this.pointTheta(this.lastPoint)),this._lastAngle}get deltaAngle(){return this._deltaAngle===void 0&&(this._deltaAngle=Zt(this.firstAngle,this.lastAngle,this.clockwise)),this._deltaAngle}normalAt(t){const n=this.tangentAt(t);return this.clockwise?et(n):Tt(n)}gradientAt(t){const n=this.firstAngle+t*this.deltaAngle*(this.clockwise?-1:1),e=-this.majorRadius*this.deltaAngle*Math.sin(n),s=this.minorRadius*this.deltaAngle*Math.cos(n),r=this.clockwise?[-e,-s]:[e,s];return this.rotateFromEllipseReferenceFrame.transform(r)}tangentAt(t){const n=this.pointTheta(t),e=-this.majorRadius*Math.sin(n),s=this.minorRadius*Math.cos(n),r=this.clockwise?[-e,-s]:[e,s];return Q(this.rotateFromEllipseReferenceFrame.transform(r))}get tangentAtFirstPoint(){const t=-this.majorRadius*Math.sin(this.firstAngle),n=this.minorRadius*Math.cos(this.firstAngle),e=this.clockwise?[-t,-n]:[t,n];return Q(this.rotateFromEllipseReferenceFrame.transform(e))}get tangentAtLastPoint(){const t=-this.majorRadius*Math.sin(this.lastAngle),n=this.minorRadius*Math.cos(this.lastAngle),e=this.clockwise?[-t,-n]:[t,n];return Q(this.rotateFromEllipseReferenceFrame.transform(e))}transform(t){const n=t.transformAngle(this.tiltAngle),e=t.scaleFactor();return new z(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.center),this.majorRadius*e,this.minorRadius*e,n,t.keepsOrientation()?this.clockwise:!this.clockwise,{angleUnits:"rad"})}splitAt(t){let n;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?n=t:n=[t];const s=[0,1,...n.map(l=>this.pointToParam(l))],r=new Map(ht([s,[this.firstPoint,this.lastPoint,...n]]));s.sort((l,a)=>l-a);let o=null;return s.flatMap((l,a)=>{if(a===s.length-1)return[];const h=s[a+1];if(h-l<this.precision)return o===null&&(o=l),[];const c=o===null?l:o,u=new z(r.get(c)||this.paramPoint(c),r.get(h)||this.paramPoint(h),this.center,this.majorRadius,this.minorRadius,this.tiltAngle,this.clockwise,{angleUnits:"rad"});return o=null,u})}}function ds(i,t,n,e,s,r,o){const{center:l,rx:a,ry:h}=Ps(i,t,n,e,s*ee,!r,o);return Math.abs(a-h)<1e-9?new R(i,t,l,o):new z(i,t,l,a,h,s,o)}function Ps([i,t],[n,e],s,r,o,l,a){if(s<0&&(s=-s),r<0&&(r=-r),s==0||r==0)throw Error("rx and ry can not be 0");const h=Math.sin(o),c=Math.cos(o),u=(i-n)/2,f=(t-e)/2,p=(i+n)/2,g=(t+e)/2,w=c*u+h*f,P=c*f-h*u,m=w*w/(s*s)+P*P/(r*r);m>1&&(s=s*Math.sqrt(m),r=r*Math.sqrt(m));const d=s*r,y=s*P,M=r*w,S=y*y+M*M;if(!S)throw Error("start point can not be same as end point");let C=Math.sqrt(Math.abs((d*d-S)/S));l==a&&(C=-C);const _=C*y/r,E=-C*M/s,v=c*_-h*E+p,L=h*_+c*E+g;return{center:[v,L],rx:s,ry:r}}function ys(i,t,n){const e=Math.abs(n[0]),s=Math.abs(n[1]);let r=.707,o=.707;const l=i,a=t;for(let h=0;h<3;h++){const c=l*r,u=a*o,f=(l*l-a*a)*r**3/l,p=(a*a-l*l)*o**3/a,g=c-f,w=u-p,P=e-f,m=s-p,d=Math.hypot(g,w),y=Math.hypot(P,m);r=Math.min(1,Math.max(0,(P*d/y+f)/l)),o=Math.min(1,Math.max(0,(m*d/y+p)/a));const M=Math.hypot(r,o);r/=M,o/=M}return[l*r*Math.sign(n[0]),a*o*Math.sign(n[1])]}function ue(i,t,n=1e-9){const e=i.transform(t.ellipseReferenceFrameTransform),s=e.slope,r=e.yIntercept,o=t.majorRadius*t.majorRadius,l=t.minorRadius*t.minorRadius,a=t.majorRadius*t.minorRadius,h=e.slope*e.slope,c=e.yIntercept*e.yIntercept,u=m=>m.map(d=>t.reverseEllipseReferenceFrameTransform.transform(d)).filter(d=>i.isOnSegment(d)&&t.isOnSegment(d));if(!Number.isFinite(s)){const m=e.firstPoint[0];if(Math.abs(m)-t.majorRadius>n)return[];if(Math.abs(Math.abs(m)-t.majorRadius)<n)return u([[m,0]]);const d=t.minorRadius*Math.sqrt(1-m*m/o),y=[m,d],M=[m,-d];return u([y,M])}const f=o*h+l-c;if(f<-n)return[];const p=o*h+l;if(Math.abs(f)<n){const m=-(o*s*r)/p,d=l*r/p;return u([[m,d]])}const g=Math.sqrt(f),w=[-(o*s*r+a*g)/p,(l*r-a*s*g)/p],P=[-(o*s*r-a*g)/p,(l*r+a*s*g)/p];return u([w,P])}function un(i,t){const n=Math.max(i.precision,t.precision),e=i.coefficients,s=e.x2,r=e.xy,o=e.y2,l=e.x,a=e.y,h=e.c,c=t.coefficients,u=c.x2,f=c.xy,p=c.y2,g=c.x,w=c.y,P=c.c,m={z0:h*s*g*g+s*s*P*P-l*s*g*P+u*u*h*h-2*s*P*u*h-l*g*u*h+u*l*l*P,z1:w*l*l*u-P*g*s*r-2*s*P*u*a-h*u*f*l+2*g*f*s*h+2*w*P*s*s+g*g*s*a-w*g*s*l-2*s*w*u*h-h*u*g*r+2*h*a*u*u-P*f*s*l-a*u*g*l+2*P*r*u*l,z2:w*w*s*s+2*p*P*s*s-a*u*g*r+P*u*r*r-a*u*f*l-P*f*s*r-2*s*w*u*a+2*g*f*s*a-p*g*s*l-2*s*p*u*h+f*f*s*h+2*w*r*u*l+a*a*u*u-o*u*g*l-w*f*s*l+2*h*o*u*u-h*u*f*r+p*l*l*u+g*g*s*o-w*g*s*r-2*s*P*u*o,z3:-2*s*u*o*w+w*u*r*r+2*p*r*u*l-o*u*f*l+f*f*s*a-w*f*s*r-2*s*p*u*a-a*u*f*r-p*f*s*l+2*w*p*s*s+2*a*o*u*u-o*u*g*r+2*g*f*s*o-p*g*s*r,z4:s*s*p*p-2*s*p*u*o+u*u*o*o-r*s*f*p-r*f*u*o+r*r*u*p+o*s*f*f},y=Pe([m.z0,m.z1,m.z2,m.z3,m.z4],n).flatMap(M=>{const S=s*f*M+s*g-u*r*M-u*l;if(S)return[[-(s*P+s*p*M*M-u*o*M*M+s*w*M-u*a*M-u*h)/S,M]];const C=r*M+l,_=-C/(2*s),E=o*M*M+a*M+h,v=C*C/(4*s*s)-E/s;if(Math.abs(v)<n)return[[_,M]];if(v>0){const L=Math.sqrt(v);return[[_+L,M],[_-L,M]]}return[]});return pt(y,n)}function Le(i,t){return un(i,t).filter(e=>i.isOnSegment(e)&&t.isOnSegment(e))}const Ms=i=>{const{firstPoint:t,lastPoint:n,center:e,majorRadius:s,minorRadius:r,tiltAngle:o,clockwise:l}=i;return new z(n,t,e,s,r,o,l,{ignoreChecks:!0,angleUnits:"rad"})},bs=(i,t)=>{if(i.isSame(t))return[i];const n=(s,r)=>new z(s,r,i.center,i.majorRadius,i.minorRadius,i.tiltAngle,i.clockwise,{ignoreChecks:!0,angleUnits:"rad"}),e=pt([t.isOnSegment(i.firstPoint)?i.firstPoint:null,t.isOnSegment(i.lastPoint)?i.lastPoint:null,i.isOnSegment(t.firstPoint)?t.firstPoint:null,i.isOnSegment(t.lastPoint)?t.lastPoint:null].filter(s=>s!==null)).sort((s,r)=>i.pointToParam(s)-i.pointToParam(r));if(e.length===0)return[];if(e.length===1)return[];if(e.length===2)return i.isSame(Ms(t))?[]:[n(e[0],e[1])];if(e.length===3){const s=b(e[0],t.lastPoint)||b(e[0],t.firstPoint)?1:0;return[n(e[0+s],e[1+s])]}else if(e.length===4)return[n(e[0],e[1]),n(e[2],e[3])];throw new Error("Bug in the ellipse arc ellipse arc overlap algorithm")};function Es(i,t,n=!1){const e=Math.max(i.precision,t.precision);return b(i.center,t.center)&&Math.abs(i.majorRadius-t.majorRadius)<e&&Math.abs(i.minorRadius-t.minorRadius)<e&&(Math.abs(i.tiltAngle-t.tiltAngle)<e||Math.abs(Math.abs(i.tiltAngle-t.tiltAngle)-Math.PI)<e)?n?bs(i,t):[]:un(i,t).filter(o=>i.isOnSegment(o)&&t.isOnSegment(o))}function $e(i,t){const[n,e]=i.firstPoint,[s,r]=i.lastPoint,o=new H().rotate(-Math.atan2(r-e,s-n)).translate(-n,-e),l=o.clone().inverse(),a=t.transform(o);return a.paramsAtY(0).map(h=>a.paramPoint(h)).map(h=>l.transform(h)).filter(h=>i.isOnSegment(h))}const Ss=(i,t=1e-9)=>{let n=i;return Math.abs(i)<t&&(n=0),n.toFixed(-Math.log10(t))};function cn(i,t=1e-9){return Array.from(new Map(i.map(n=>[Ss(n,t),n])).values())}const _s=(i,t)=>{const[[n,e,s,r],[o,l,a,h]]=t.polynomialCoefficients,c=i.coefficients,u=c.x2,f=c.xy,p=c.y2,g=c.x,w=c.y,P=c.c,m=n*n,d=e*e,y=s*s,M=r*r,S=o*o,C=l*l,_=a*a,E=h*h,v=P+g*n+u*m+w*o+f*n*o+p*S,L=g*e+2*u*n*e+f*e*o+w*l+f*n*l+2*p*o*l,U=u*d+g*s+2*u*n*s+f*s*o+f*e*l+p*C+w*a+f*n*a+2*p*o*a,K=2*u*e*s+g*r+2*u*n*r+f*r*o+f*s*l+f*e*a+2*p*l*a+w*h+f*n*h+2*p*o*h,q=u*y+2*u*e*r+f*r*l+f*s*a+p*_+f*e*h+2*p*l*h,x=2*u*s*r+f*r*a+f*s*h+2*p*a*h,I=u*M+f*r*h+p*E;return[v,L,U,K,q,x,I]};function ze(i,t){const n=Math.max(i.precision,t.precision),e=_s(i,t),s=Pe(e,n).filter(r=>r>=-t.precision&&r<=1+t.precision);return cn(s,n).map(r=>t.paramPoint(r)).filter(r=>i.isOnSegment(r))}class N extends Ot{constructor(t,n,e){super(t,n),this.segmentType="QUADRATIC_BEZIER",this._boundingBox=null,this._polynomialCoefficients=null,this.controlPoint=e}get midPoint(){return this.paramPoint(.5)}get boundingBox(){if(this._boundingBox===null){const t=n=>{const[e,s,r]=[this.firstPoint[n],this.controlPoint[n],this.lastPoint[n]],o=e-2*s+r;if(!o)return[];const l=(e-s)/o;return l>=-this.precision&&l<=1+this.precision?[l]:[]};this._boundingBox=we([this.firstPoint,...t(0).map(n=>this.paramPoint(n)),...t(1).map(n=>this.paramPoint(n)),this.lastPoint]).grow(this.precision)}return this._boundingBox}clone(){return new N(this.firstPoint,this.lastPoint,this.controlPoint)}reverse(){return new N(this.lastPoint,this.firstPoint,this.controlPoint)}isSame(t){return t.segmentType!=="QUADRATIC_BEZIER"?!1:b(this.firstPoint,t.firstPoint)&&b(this.lastPoint,t.lastPoint)&&b(this.controlPoint,t.controlPoint)}distanceFrom(t){const e=rn(s=>yt(this.paramPoint(s),t),this.precision);return e.argMin<-this.precision||e.argMin>1+this.precision?Math.min(G(this.firstPoint,t),G(this.lastPoint,t)):Math.sqrt(e.fMin)}isOnSegment(t){if(!this.boundingBox.contains(t))return!1;try{const n=this.pointToParam(t);return n>=-this.precision&&n<=1+this.precision}catch{return!1}}gradientAt(t){const n=e=>{const s=this.firstPoint[e],r=this.controlPoint[e],o=this.lastPoint[e];return 2*(t*(s-2*r+o)-s+r)};return[n(0),n(1)]}tangentAt(t){const n=this.pointToParam(t);return Q(this.gradientAt(n))}get tangentAtFirstPoint(){return Q($(this.controlPoint,this.firstPoint))}get tangentAtLastPoint(){return Q($(this.controlPoint,this.lastPoint))}normalAt(t){const n=this.tangentAt(t);return et(n)}splitAtParameters(t,n=null){const e=[...t];e.sort((r,o)=>r-o);let s={originalParam:0,param:0,p0:this.firstPoint,p1:this.controlPoint};return e.flatMap((r,o)=>{if(r-s.originalParam<this.precision)return[];let l=!1;r>1-this.precision&&(l=!0);const a=(r-s.originalParam)/(1-s.originalParam),h=ye([s.p0,s.p1,this.lastPoint],l?1:a),c=l?this.lastPoint:(n==null?void 0:n.get(r))??h[0][0],u=h[1][0],f=h[1][1],p=new N(s.p0,c,u);return s={param:a,originalParam:r,p0:c,p1:f},o===e.length-1&&!l?[p,new N(s.p0,this.lastPoint,s.p1)]:p})}splitAt(t){let n;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?n=t:n=[t];const e=n.map(r=>this.pointToParam(r)),s=new Map(ht([e,n]));return this.splitAtParameters(e,s)}transform(t){return new N(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.controlPoint))}paramPoint(t){const n=1-t,e=n*n,s=2*n*t,r=t*t;return[e*this.firstPoint[0]+s*this.controlPoint[0]+r*this.lastPoint[0],e*this.firstPoint[1]+s*this.controlPoint[1]+r*this.lastPoint[1]]}get polynomialCoefficients(){if(this._polynomialCoefficients===null){const t=n=>{const e=this.firstPoint[n],s=this.controlPoint[n],r=this.lastPoint[n],o=e-2*s+r,l=2*(s-e);return[e,l,o]};this._polynomialCoefficients=[t(0),t(1)]}return this._polynomialCoefficients}paramsAtY(t){const[n,e,s]=this.polynomialCoefficients[1];return se(n-t,e,s).filter(r=>r>=-this.precision&&r<=1+this.precision)}pointToParam(t){const n=this.paramsAtY(t[1]);if(n.length===0)throw new Error("Point is not on the curve!");const e=r=>Math.abs(this.paramPoint(r)[0]-t[0])<=this.precision,s=n.find(e);if(s===void 0)throw new Error("Point is not on the curve");return s}}const ks=(i,t)=>{const[[n,e,s],[r,o,l]]=t.polynomialCoefficients,a=i.coefficients,h=a.x2,c=a.xy,u=a.y2,f=a.x,p=a.y,g=a.c,w=n*n,P=e*e,m=s*s,d=r*r,y=o*o,M=l*l,S=h*w+c*n*r+u*d+f*n+p*r+g,C=2*h*n*e+c*n*o+c*e*r+2*u*r*o+f*e+p*o,_=2*h*n*s+h*P+c*n*l+c*e*o+c*s*r+2*u*r*l+u*y+f*s+p*l,E=2*h*e*s+c*e*l+c*s*o+2*u*o*l,v=h*m+c*s*l+u*M;return[S,C,_,E,v]};function Ne(i,t){const n=Math.max(i.precision,t.precision),e=ks(i,t),s=us(...e).filter(r=>r>=-t.precision&&r<=1+t.precision);return cn(s,n).map(r=>t.paramPoint(r)).filter(r=>i.isOnSegment(r))}function tt(i,{firstPoint:t,lastPoint:n},e=1e-9){const s=$(n,t);return Math.abs(s[0])<e?s[1]>0?t[0]-i[0]:i[0]-t[0]:Math.abs(s[1])<e?s[0]>0?i[1]-t[1]:t[1]-i[1]:Pt(s,$(i,t))/bn(s)}class be{constructor(t,n,e,s){this.firstPoint=t,this.lastPoint=n,this.negativeThickness=e,this.positiveThickness=s}get width(){return this.positiveThickness-this.negativeThickness}}const Is=3/4,Cs=4/9;function xs(i){const t=tt(i.firstControlPoint,i),n=tt(i.lastControlPoint,i),e=t*n>0?Is:Cs;return new be(i.firstPoint,i.lastPoint,e*Math.min(0,t,n),e*Math.max(0,t,n))}function js(i){const t=tt(i.controlPoint,i);return new be(i.firstPoint,i.lastPoint,Math.min(0,t/2),Math.max(0,t/2))}function vs(i){if(i instanceof Y)return xs(i);if(i instanceof N)return js(i);throw new Error("Not implemented")}function Fs(i){const t=i.paramPoint(.5),n=et($(t,i.firstPoint)),e=J(t,n),s={firstPoint:t,lastPoint:e},r=[tt(i.firstPoint,s),tt(i.lastPoint,s)];return i instanceof Y?r.push(tt(i.firstControlPoint,s),tt(i.lastControlPoint,s)):i instanceof N&&r.push(tt(i.controlPoint,s)),new be(t,e,Math.min(...r),Math.max(...r))}function De(i,t){const n=[];for(let e=1;e<i.length;e++){const s=i[e];if(s[1]===t){n.push(s[0]);continue}const r=i[e-1],o=t-r[1],l=t-s[1];if(o*l<0){n.push(r[0]+(t-r[1])*(s[0]-r[0])/(s[1]-r[1]));continue}}return n}class Ft{constructor(t,n){this.from=t,this.to=n}get size(){return this.from==="start"?this.to==="end"?1:this.to:this.to==="end"?1-this.from:Math.abs(this.from-this.to)}clipCurve(t){return this.from==="start"?this.to==="end"?t:t.splitAtParameters([this.to])[0]:this.to==="end"?t.splitAtParameters([this.from])[1]:t.splitAtParameters([this.from,this.to])[1]}}function Ts(i,t){if(i instanceof Y)return new As([tt(i.firstPoint,t),tt(i.firstControlPoint,t),tt(i.lastControlPoint,t),tt(i.lastPoint,t)]);if(i instanceof N)return new Rs([tt(i.firstPoint,t),tt(i.controlPoint,t),tt(i.lastPoint,t)]);throw new Error("Not implemented")}class Rs{constructor(t){this.distances=t,this.topHull=[],this.bottomHull=[];const[n,e,s]=t,r=[0,n],o=[1/2,e],l=[1,s],a=s-n,h=n;e-(a*(1/2)+h)>0?(this.topHull=[r,o,l],this.bottomHull=[r,l]):(this.topHull=[r,l],this.bottomHull=[r,o,l])}get startDistance(){return this.distances[0]}get endDistance(){return this.distances[2]}}class As{constructor(t){this.distances=t,this.topHull=[],this.bottomHull=[];const[n,e,s,r]=t,o=[0,n],l=[1/3,e],a=[2/3,s],h=[1,r],c=r-n,u=n,f=e-(c*(1/3)+u),p=s-(c*(2/3)+u);let g=null,w=null;if(f*p<0)g=[o,l,h],w=[o,a,h];else{const m=f/p;m>=2?(g=[o,l,h],w=[o,h]):m<=.5?(g=[o,a,h],w=[o,h]):(g=[o,l,a,h],w=[o,h])}f<0&&([g,w]=[w,g]),this.topHull=g,this.bottomHull=w}get startDistance(){return this.distances[0]}get endDistance(){return this.distances[3]}}function Ve(i,t){const n=Ts(t,i),e=De(n.topHull,i.negativeThickness),s=De(n.bottomHull,i.positiveThickness),r=n.endDistance>=i.negativeThickness&&n.endDistance<=i.positiveThickness;if(!e.length&&!s.length)return r?new Ft("start","end"):null;if(e.length===1&&s.length===1)return new Ft(e[0],s[0]);if(e.length===2&&s.length===2)throw new Error("Bug in the clipping algorithm, unexpected number of crossing points");const o=e.length?e:s;return o.length===2?new Ft(o[0],o[1]):r?new Ft(o[0],"end"):new Ft("start",o[0])}function Ye(i,t){const n=vs(i),e=Ve(n,t);if(!e)return null;const s=Fs(i),r=Ve(s,t);return r?e.size>r.size?r.clipCurve(t):e.clipCurve(t):null}const Jt=i=>i instanceof N?ut($(i.controlPoint,i.firstPoint))+ut($(i.controlPoint,i.lastPoint)):ut($(i.firstControlPoint,i.firstPoint))+ut($(i.lastControlPoint,i.firstControlPoint))+ut($(i.lastControlPoint,i.lastPoint));function Et(i,t,n=1e-9,{maxIterations:e=100}={}){const s=Math.max(n*n,Number.EPSILON*10);let r=i,o=t,l=Jt(r),a=Jt(o);for(let h=0;h<e;h++){const c=l>s?Ye(o,r):r;if(!c)return[];const u=Jt(c),f=a>s?Ye(c,o):o;if(!f)return[];const p=Jt(f);if(u<=s&&p<=s)return[c.boundingBox.intersection(f.boundingBox).center];if(b(c.firstPoint,c.lastPoint)&&f.isOnSegment(c.firstPoint))return[c.firstPoint];if(b(f.firstPoint,f.lastPoint)&&c.isOnSegment(f.firstPoint))return[f.firstPoint];if(u>.8*l&&p>.8*a)if(u/l>p/a){const[g,w]=c.splitAtParameters([.5]);return pt([...Et(g,f,n,{maxIterations:e-h}),...Et(w,f,n,{maxIterations:e-h})],n)}else{const[g,w]=f.splitAtParameters([.5]);return pt([...Et(c,g,n,{maxIterations:e-h}),...Et(c,w,n,{maxIterations:e-h})],n)}r=c,o=f,l=u,a=p}throw new Error("Bézier clip: Maximum number of iterations reached")}function qs(i,t){const n=[];if([[i.firstPoint,t],[i.lastPoint,t],[t.firstPoint,i],[t.lastPoint,i]].forEach(([s,r])=>{r.isOnSegment(s)&&n.push(s)}),n.length<2)return null;if(n.length===2)return[i.splitAt(n)[1]];if(n.length===3)return b(n[0],i.firstPoint)&&b(n[1],i.lastPoint)?[i]:[t];if(n.length===4)return[i]}function Bs(i,t,n=!1){const e=Math.max(i.precision,t.precision);if(n){const s=qs(i,t);if(s)return s}return Et(i,t,e)}function Os(i,t){const n=[];if([[i.firstPoint,t],[i.lastPoint,t],[t.firstPoint,i],[t.lastPoint,i]].forEach(([s,r])=>{r.isOnSegment(s)&&n.push(s)}),n.length<2)return null;if(n.length===2)return[i.splitAt(n)[1]];if(n.length===3)return b(n[0],i.firstPoint)&&b(n[1],i.lastPoint)?[i]:[t];if(n.length===4)return[i]}function Ls(i,t,n=!1){const e=Math.max(i.precision,t.precision);if(n){const s=Os(i,t);if(s)return s}return Et(i,t,e)}function $s(i,t,n){if(i instanceof B&&t instanceof B){const e=sn(i,t,!1,n);return e===null?[]:[e]}if(i instanceof B&&t instanceof R)return At(i,t,n);if(i instanceof R&&t instanceof B)return At(t,i,n);if(i instanceof R&&t instanceof R)return Me(i,t,!1,n);throw new Error("Not implemented")}function qt(i,t,n){if(i instanceof B&&t instanceof B){const e=sn(i,t,!0,n);return e===null?{intersections:[],overlaps:[],count:0}:e instanceof B?{intersections:[],overlaps:[e],count:1}:{intersections:[e],overlaps:[],count:1}}if(!i.boundingBox.overlaps(t.boundingBox))return{intersections:[],overlaps:[],count:0};if(i instanceof B&&t instanceof R){const e=At(i,t,n);return{intersections:e,overlaps:[],count:e.length}}if(i instanceof R&&t instanceof B){const e=At(t,i,n);return{intersections:e,overlaps:[],count:e.length}}if(i instanceof R&&t instanceof R){const e=Me(i,t,!0,n);return e.length?e[0]instanceof R?{intersections:[],overlaps:e,count:e.length}:{intersections:e,overlaps:[],count:e.length}:{intersections:[],overlaps:[],count:0}}if(i instanceof B&&t instanceof z){const e=ue(i,t,n);return{intersections:e,overlaps:[],count:e.length}}if(t instanceof B&&i instanceof z){const e=ue(t,i,n);return{intersections:e,overlaps:[],count:e.length}}if(i instanceof R&&t instanceof z){const e=Le(i,t);return{intersections:e,overlaps:[],count:e.length}}if(t instanceof R&&i instanceof z){const e=Le(t,i);return{intersections:e,overlaps:[],count:e.length}}if(i instanceof z&&t instanceof z){const e=Es(i,t,!0);return e.length?e[0]instanceof z?{intersections:[],overlaps:e,count:e.length}:{intersections:e,overlaps:[],count:e.length}:{intersections:[],overlaps:[],count:0}}if(i instanceof B&&(t instanceof Y||t instanceof N)){const e=$e(i,t);return{intersections:e,overlaps:[],count:e.length}}if(t instanceof B&&(i instanceof Y||i instanceof N)){const e=$e(t,i);return{intersections:e,overlaps:[],count:e.length}}if((i instanceof R||i instanceof z)&&t instanceof N){const e=Ne(i,t);return{intersections:e,overlaps:[],count:e.length}}if((t instanceof R||t instanceof z)&&i instanceof N){const e=Ne(t,i);return{intersections:e,overlaps:[],count:e.length}}if((i instanceof R||i instanceof z)&&t instanceof Y){const e=ze(i,t);return{intersections:e,overlaps:[],count:e.length}}if((t instanceof R||t instanceof z)&&i instanceof Y){const e=ze(t,i);return{intersections:e,overlaps:[],count:e.length}}if(i instanceof N&&t instanceof N){const e=Ls(i,t);return e.length?e[0]instanceof N?{intersections:[],overlaps:e,count:e.length}:{intersections:e,overlaps:[],count:e.length}:{intersections:[],overlaps:[],count:0}}if(i instanceof N&&t instanceof Y||t instanceof N&&i instanceof Y){const e=Et(i,t);return{intersections:e,overlaps:[],count:e.length}}if(i instanceof Y&&t instanceof Y){const e=Bs(i,t);return e.length?e[0]instanceof Y?{intersections:[],overlaps:e,count:e.length}:{intersections:e,overlaps:[],count:e.length}:{intersections:[],overlaps:[],count:0}}throw new Error("Not implemented")}function fn(i){const t=[];for(let n=0;n<i;n++)for(let e=0;e<=n;e++)t.push([n,e]);return t}function*ce(i){for(const[t,n]of fn(i.length))t!==n&&(yield[i[t],i[n]])}class mn extends Bt{constructor(t,{ignoreChecks:n=!1}={}){super(),this._boundingBox=null,n||gn(t),this.segments=t}get repr(){return this.segments.map(t=>t.repr).join(`
|
|
9
|
-
`)+`
|
|
10
|
-
`}get info(){return this.repr}get firstPoint(){return this.segments[0].firstPoint}get lastPoint(){return this.segments[this.segments.length-1].lastPoint}get segmentsCount(){return this.segments.length}onStroke(t){return this.segments.some(n=>n.isOnSegment(t))}intersects(t){return this.boundingBox.overlaps(t.boundingBox)?this.segments.some(n=>t.segments.some(e=>qt(n,e).count>0)):!1}overlappingSegments(t){return this.segments.flatMap(n=>t.segments.flatMap(e=>n.boundingBox.overlaps(e.boundingBox)?qt(n,e).overlaps:[]))}get boundingBox(){if(this._boundingBox===null){let t=this.segments[0].boundingBox;this.segments.slice(1).forEach(n=>{t=t.merge(n.boundingBox)}),this._boundingBox=t}return this._boundingBox}[Symbol.for("nodejs.util.inspect.custom")](){return this.repr}}function zs(i,t="Stroke"){fn(i.length).forEach(([n,e])=>{if(n===e)return;const s=i[n],r=i[e],o=qt(s,r),l=Math.max(s.precision,r.precision);if(o.count!==0){if(o.count===1&&!o.overlaps.length){const a=n-e,h=o.intersections[0];if(a===1&&b(s.firstPoint,h,l)||a===-1&&b(s.lastPoint,h,l)||a===i.length-1&&b(s.lastPoint,h,l)&&b(r.firstPoint,h,l)||-a===i.length-1&&b(s.firstPoint,h,l)&&b(r.lastPoint,h,l))return}if(!(o.count===2&&i.length===2&&(b(s.firstPoint,o.intersections[0],l)&&b(s.lastPoint,o.intersections[1],l)||b(s.firstPoint,o.intersections[1],l)&&b(s.lastPoint,o.intersections[0],l))))throw new Error(`${t} segments must not intersect, but segments ${s.info} and ${r.info} do at ${JSON.stringify(o.intersections)}`)}})}function gn(i,t="Stroke"){if(i.length===0)throw new Error(`${t} must have at least one segment`);ht([i.slice(0,-1),i.slice(1)]).forEach(([n,e])=>{if(!b(n.lastPoint,e.firstPoint))throw new Error(`${t} segments must be connected, but ${n.info} and ${e.info} are not`)}),zs(i,t)}function Ue(i,t){return!!(i instanceof B&&t instanceof B&&de(i.V,t.V)||i instanceof R&&t instanceof R&&b(i.center,t.center)&&i.radius-t.radius<i.precision)}function Xe(i,t){if(i instanceof B&&t instanceof B)return new B(i.firstPoint,t.lastPoint);if(i instanceof R&&t instanceof R)return new R(i.firstPoint,t.lastPoint,i.center,i.clockwise);throw new Error("Not implemented")}function pn(i){let t=!1;const n=[];for(const e of i.segments){if(n.length===0){n.push(e);continue}const s=n[n.length-1];Ue(s,e)?(t=!0,n.pop(),n.push(Xe(s,e))):n.push(e)}if(b(i.firstPoint,i.lastPoint)&&Ue(n[0],n[n.length-1])){t=!0;const e=n.pop();n[0]=Xe(e,n[0])}return t?n:null}class rt extends mn{constructor(){super(...arguments),this.strokeType="STRAND"}reverse(){const t=this.segments.map(n=>n.reverse());return t.reverse(),new rt(t,{ignoreChecks:!0})}clone(){return new rt(this.segments.map(t=>t.clone()),{ignoreChecks:!0})}extend(t){if(!b(this.lastPoint,t.firstPoint))throw console.error(this.repr,t.repr),new Error("Cannot extend strand: connection point is not the same");return new rt([...this.segments,...t.segments])}simplify(){const t=pn(this);return t?new rt(t,{ignoreChecks:!0}):this}transform(t){return new rt(this.segments.map(n=>n.transform(t)),{ignoreChecks:!0})}}const Ns=(i,t)=>{const n=Lt(t,{V:[1,0],firstPoint:i,precision:t.precision});if(n==="parallel")return 0;const{intersectionParam1:e,intersectionParam2:s}=n;if(!t.isValidParameter(e)||s<=-t.precision)return 0;if(Math.abs(e)<t.precision||Math.abs(e-1)<t.precision){const[,r]=t.midPoint;return i[1]-r<0?1:0}return 1};class Ee{constructor(t){this._count=0,this.segment=t}update(t,n=!1){!n&&!this.segment.isOnSegment(t)||(b(t,this.segment.firstPoint)?this._count+=this.segment.tangentAtFirstPoint[1]>0?1:0:b(t,this.segment.lastPoint)?this._count+=this.segment.tangentAtLastPoint[1]>0?0:1:this._count+=1)}get count(){return this._count}}const Ds=(i,t)=>{const n=t.precision,e=Math.abs(i[1]-t.center[1]);if(e>t.radius+n)return 0;const s=yt(i,t.center),r=t.radius*t.radius,o=n*n;if(Math.abs(s-r)<o&&t.isOnSegment(i))return 0;const l=s-r>o;if(l&&t.center[0]<i[0])return 0;const a=Math.sqrt(t.radius*t.radius-e*e),h=new Ee(t);return h.update([t.center[0]+a,i[1]]),l&&h.update([t.center[0]-a,i[1]]),h.count},Vs=(i,t)=>{const n=t.boundingBox.xMax+t.boundingBox.width/2,e=new B(i,[n,i[1]]),s=new Ee(t);return ue(e,t).forEach(r=>{s.update(r,!0)}),s.count},Ys=(i,t)=>{const n=new Ee(t);return t.paramsAtY(i[1]).map(e=>{try{return t.paramPoint(e)}catch{return null}}).filter(e=>e!==null).filter(e=>{const[s]=e;return s>=i[0]}).forEach(e=>{n.update(e,!0)}),n.count};function Us(i,t){if(t instanceof B)return Ns(i,t);if(t instanceof R)return Ds(i,t);if(t instanceof z)return Vs(i,t);if(t instanceof Y||t instanceof N)return Ys(i,t);throw new Error("Not implemented")}class gt extends mn{constructor(t,{ignoreChecks:n=!1}={}){super(t,{ignoreChecks:!0}),this.strokeType="LOOP",this._clockwise=null,n||Xs(t)}get clockwise(){if(this._clockwise===null){const t=this.segments.flatMap(e=>e instanceof B?[e.firstPoint]:[e.firstPoint,e.paramPoint(.5)]),n=t.map((e,s)=>{const r=t[(s+1)%t.length];return(r[0]-e[0])*(r[1]+e[1])}).reduce((e,s)=>e+s,0);this._clockwise=n>0}return this._clockwise}clone(){return new gt(this.segments.map(t=>t.clone()),{ignoreChecks:!0})}reverse(){const t=this.segments.map(n=>n.reverse());return t.reverse(),new gt(t,{ignoreChecks:!0})}transform(t){return new gt(this.segments.map(n=>n.transform(t)),{ignoreChecks:!0})}contains(t){return this.onStroke(t)||!this.boundingBox.contains(t)?!1:this.segments.reduce((e,s)=>e+Us(t,s),0)%2===1}simplify(){const t=pn(this);return t?new gt(t,{ignoreChecks:!0}):this}}function Xs(i){if(gn(i,"Loop"),!b(i[0].firstPoint,i[i.length-1].lastPoint))throw new Error("Loop segment must be closed")}const Gs=[B,R,z,N,Y];function wn(i){return Gs.some(t=>i instanceof t)}function dn(i){if(i instanceof B)return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint};if(i instanceof R)return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint,center:i.center,clockwise:i.clockwise};if(i instanceof z)return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint,center:i.center,clockwise:i.clockwise,majorRadius:i.majorRadius,minorRadius:i.minorRadius,tiltAngle:i.tiltAngle};if(i instanceof N)return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint,controlPoint:i.controlPoint};if(i instanceof Y)return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint,firstControlPoint:i.firstControlPoint,lastControlPoint:i.lastControlPoint};throw new Error("Unknown segment type")}function fe(i){return{type:"LOOP",segments:i.segments.map(dn)}}function Pn(i){return{type:"FIGURE",contour:fe(i.contour),holes:i.holes.map(fe)}}function Ws(i){return{type:"DIAGRAM",figures:i.figures.map(Pn)}}function me(i){if(i instanceof mt)return Ws(i);if(i instanceof it)return Pn(i);if(i instanceof gt)return fe(i);if(wn(i))return dn(i);throw new Error("Unknown shape type")}class Qs{constructor(){this.ids=[],this.values=[],this.length=0}clear(){this.length=0}push(t,n){let e=this.length++;for(;e>0;){const s=e-1>>1,r=this.values[s];if(n>=r)break;this.ids[e]=this.ids[s],this.values[e]=r,e=s}this.ids[e]=t,this.values[e]=n}pop(){if(this.length===0)return;const t=this.ids[0];if(this.length--,this.length>0){const n=this.ids[0]=this.ids[this.length],e=this.values[0]=this.values[this.length],s=this.length>>1;let r=0;for(;r<s;){let o=(r<<1)+1;const l=o+1;let a=this.ids[o],h=this.values[o];const c=this.values[l];if(l<this.length&&c<h&&(o=l,a=this.ids[l],h=c),h>=e)break;this.ids[r]=a,this.values[r]=h,r=o}this.ids[r]=n,this.values[r]=e}return t}peek(){if(this.length!==0)return this.ids[0]}peekValue(){if(this.length!==0)return this.values[0]}shrink(){this.ids.length=this.values.length=this.length}}const Ge=[Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array],he=3;class Se{static from(t){if(!t||t.byteLength===void 0||t.buffer)throw new Error("Data must be an instance of ArrayBuffer or SharedArrayBuffer.");const[n,e]=new Uint8Array(t,0,2);if(n!==251)throw new Error("Data does not appear to be in a Flatbush format.");if(e>>4!==he)throw new Error(`Got v${e>>4} data when expected v${he}.`);const[s]=new Uint16Array(t,2,1),[r]=new Uint32Array(t,4,1);return new Se(r,s,Ge[e&15],void 0,t)}constructor(t,n=16,e=Float64Array,s=ArrayBuffer,r){if(t===void 0)throw new Error("Missing required argument: numItems.");if(isNaN(t)||t<=0)throw new Error(`Unexpected numItems value: ${t}.`);this.numItems=+t,this.nodeSize=Math.min(Math.max(+n,2),65535);let o=t,l=o;this._levelBounds=[o*4];do o=Math.ceil(o/this.nodeSize),l+=o,this._levelBounds.push(l*4);while(o!==1);this.ArrayType=e||Float64Array,this.IndexArrayType=l<16384?Uint16Array:Uint32Array;const a=Ge.indexOf(this.ArrayType),h=l*4*this.ArrayType.BYTES_PER_ELEMENT;if(a<0)throw new Error(`Unexpected typed array class: ${e}.`);r&&r.byteLength!==void 0&&!r.buffer?(this.data=r,this._boxes=new this.ArrayType(this.data,8,l*4),this._indices=new this.IndexArrayType(this.data,8+h,l),this._pos=l*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 s(8+h+l*this.IndexArrayType.BYTES_PER_ELEMENT),this._boxes=new this.ArrayType(this.data,8,l*4),this._indices=new this.IndexArrayType(this.data,8+h,l),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,(he<<4)+a]),new Uint16Array(this.data,2,1)[0]=n,new Uint32Array(this.data,4,1)[0]=t),this._queue=new Qs}add(t,n,e,s){const r=this._pos>>2,o=this._boxes;return this._indices[r]=r,o[this._pos++]=t,o[this._pos++]=n,o[this._pos++]=e,o[this._pos++]=s,t<this.minX&&(this.minX=t),n<this.minY&&(this.minY=n),e>this.maxX&&(this.maxX=e),s>this.maxY&&(this.maxY=s),r}finish(){if(this._pos>>2!==this.numItems)throw new Error(`Added ${this._pos>>2} items when expected ${this.numItems}.`);const t=this._boxes;if(this.numItems<=this.nodeSize){t[this._pos++]=this.minX,t[this._pos++]=this.minY,t[this._pos++]=this.maxX,t[this._pos++]=this.maxY;return}const n=this.maxX-this.minX||1,e=this.maxY-this.minY||1,s=new Uint32Array(this.numItems),r=(1<<16)-1;for(let o=0,l=0;o<this.numItems;o++){const a=t[l++],h=t[l++],c=t[l++],u=t[l++],f=Math.floor(r*((a+c)/2-this.minX)/n),p=Math.floor(r*((h+u)/2-this.minY)/e);s[o]=Zs(f,p)}ge(s,t,this._indices,0,this.numItems-1,this.nodeSize);for(let o=0,l=0;o<this._levelBounds.length-1;o++){const a=this._levelBounds[o];for(;l<a;){const h=l;let c=t[l++],u=t[l++],f=t[l++],p=t[l++];for(let g=1;g<this.nodeSize&&l<a;g++)c=Math.min(c,t[l++]),u=Math.min(u,t[l++]),f=Math.max(f,t[l++]),p=Math.max(p,t[l++]);this._indices[this._pos>>2]=h,t[this._pos++]=c,t[this._pos++]=u,t[this._pos++]=f,t[this._pos++]=p}}}search(t,n,e,s,r){if(this._pos!==this._boxes.length)throw new Error("Data not yet indexed - call index.finish().");let o=this._boxes.length-4;const l=[],a=[];for(;o!==void 0;){const h=Math.min(o+this.nodeSize*4,Qe(o,this._levelBounds));for(let c=o;c<h;c+=4){if(e<this._boxes[c]||s<this._boxes[c+1]||t>this._boxes[c+2]||n>this._boxes[c+3])continue;const u=this._indices[c>>2]|0;o>=this.numItems*4?l.push(u):(r===void 0||r(u))&&a.push(u)}o=l.pop()}return a}neighbors(t,n,e=1/0,s=1/0,r){if(this._pos!==this._boxes.length)throw new Error("Data not yet indexed - call index.finish().");let o=this._boxes.length-4;const l=this._queue,a=[],h=s*s;for(;o!==void 0;){const c=Math.min(o+this.nodeSize*4,Qe(o,this._levelBounds));for(let u=o;u<c;u+=4){const f=this._indices[u>>2]|0,p=We(t,this._boxes[u],this._boxes[u+2]),g=We(n,this._boxes[u+1],this._boxes[u+3]),w=p*p+g*g;o>=this.numItems*4?l.push(f<<1,w):(r===void 0||r(f))&&l.push((f<<1)+1,w)}for(;l.length&&l.peek()&1;)if(l.peekValue()>h||(a.push(l.pop()>>1),a.length===e))return l.clear(),a;o=l.pop()>>1}return l.clear(),a}}function We(i,t,n){return i<t?t-i:i<=n?0:i-n}function Qe(i,t){let n=0,e=t.length-1;for(;n<e;){const s=n+e>>1;t[s]>i?e=s:n=s+1}return t[n]}function ge(i,t,n,e,s,r){if(Math.floor(e/r)>=Math.floor(s/r))return;const o=i[e+s>>1];let l=e-1,a=s+1;for(;;){do l++;while(i[l]<o);do a--;while(i[a]>o);if(l>=a)break;Js(i,t,n,l,a)}ge(i,t,n,e,a,r),ge(i,t,n,a+1,s,r)}function Js(i,t,n,e,s){const r=i[e];i[e]=i[s],i[s]=r;const o=4*e,l=4*s,a=t[o],h=t[o+1],c=t[o+2],u=t[o+3];t[o]=t[l],t[o+1]=t[l+1],t[o+2]=t[l+2],t[o+3]=t[l+3],t[l]=a,t[l+1]=h,t[l+2]=c,t[l+3]=u;const f=n[e];n[e]=n[s],n[s]=f}function Zs(i,t){let n=i^t,e=65535^n,s=65535^(i|t),r=i&(t^65535),o=n|e>>1,l=n>>1^n,a=s>>1^e&r>>1^s,h=n&s>>1^r>>1^r;n=o,e=l,s=a,r=h,o=n&n>>2^e&e>>2,l=n&e>>2^e&(n^e)>>2,a^=n&s>>2^e&r>>2,h^=e&s>>2^(n^e)&r>>2,n=o,e=l,s=a,r=h,o=n&n>>4^e&e>>4,l=n&e>>4^e&(n^e)>>4,a^=n&s>>4^e&r>>4,h^=e&s>>4^(n^e)&r>>4,n=o,e=l,s=a,r=h,a^=n&s>>8^e&r>>8,h^=e&s>>8^(n^e)&r>>8,n=a^a>>1,e=h^h>>1;let c=i^t,u=e|65535^(c|n);return c=(c|c<<8)&16711935,c=(c|c<<4)&252645135,c=(c|c<<2)&858993459,c=(c|c<<1)&1431655765,u=(u|u<<8)&16711935,u=(u|u<<4)&252645135,u=(u|u<<2)&858993459,u=(u|u<<1)&1431655765,(u<<1|c)>>>0}function _e(i,t=1e-7){if(i.length===0)return[];if(i.length===1)return[i];const n=new Se(i.length);i.forEach(r=>{const[o,l]=r.firstPoint;n.add(o-t,l-t,o+t,l+t)}),n.finish();const e=[],s=new Set;return i.forEach((r,o)=>{if(s.has(o))return;const l=[r];let a=o;s.add(o);let h=i.length;for(;;){if(h--<0)throw new Error("Infinite loop detected");const c=l[l.length-1].lastPoint,[u,f]=c,p=n.search(u-t,f-t,u+t,f+t),g=d=>Math.abs((a-d)%i.length),w=p.filter(d=>!s.has(d)).map(d=>[i[d],d,g(d)]).sort(([,,d],[,,y])=>g(d)-g(y));if(w.length===0){e.push(l);break}const[P,m]=w[0];l.push(P),s.add(m),a=m}}),e}class it extends Bt{constructor(t,n=[],{ignoreChecks:e=!1}={}){super(),e||Ks(t,n),this.contour=t,this.holes=n}get boundingBox(){return this.contour.boundingBox}get isFull(){return this.holes.length===0}get allLoops(){return[this.contour,...this.holes]}clone(){return new it(this.contour.clone(),this.holes.map(t=>t.clone()))}transform(t){return new it(this.contour.transform(t),this.holes.map(n=>n.transform(t)))}contains(t){return this.contour.contains(t)&&!this.holes.some(n=>n.contains(t))}intersects(t){return this.allLoops.some(n=>t.allLoops.some(e=>n.intersects(e)))}overlappingStrands(t){const n=t instanceof it?t.allLoops:[t],e=this.allLoops.flatMap(s=>n.flatMap(r=>s.overlappingSegments(r)));return _e(e).map(s=>new rt(s))}}function Ks(i,t=[]){if(!i)throw new Error("Figure must have a contour");for(const[n,e]of ce([i,...t]))if(n.intersects(e))throw new Error("Loops in a figure must not intersect");if(t.some(n=>!i.contains(n.firstPoint)&&!i.onStroke(n.firstPoint)))throw new Error("Holes must be inside the contour");for(const[n,e]of ce(t))if(n.contains(e.firstPoint))throw console.error(me(n),me(e)),new Error("Holes must not be inside other holes")}const Hs=i=>{const t=i.map((s,r)=>i.slice(r+1).map((o,l)=>[l+r+1,o]).filter(([,o])=>s.boundingBox.overlaps(o.boundingBox)).map(([o])=>o)),n=[],e=Array(t.length);return t.forEach((s,r)=>{let o=e[r];o||(o=[],n.push(o)),o.push(i[r]),s.length&&s.forEach(l=>{e[l]=o})}),n},yn=i=>i.map((t,n)=>{const s=t.segments[0].midPoint,r=i.filter((o,l)=>n===l?!1:o.contains(s));return{loop:t,isIn:r}}),ti=(i,t)=>i.flatMap(({loop:n})=>ke(t.filter(({loop:e,isIn:s})=>e===n||s.indexOf(n)!==-1))),ei=(i,t)=>{const n=t.filter(({isIn:s})=>s.length<=1),e=ke(yn(i.map(({loop:s})=>s)));return[n,...e]},ke=i=>{if(!i.length)return[];const t=i.filter(({isIn:e})=>!e.length),n=i.filter(({isIn:e})=>e.length>1);return t.length===1&&n.length===0?[i]:t.length>1?ti(t,i):ei(n,i)};function Rt(i){return Hs(i).map(yn).flatMap(ke).map(n=>{if(n.length===1)return new it(n[0].loop);n.sort((r,o)=>r.isIn.length-o.isIn.length);const[e,...s]=n.map(({loop:r})=>r);return new it(e,s)})}function ni(i,t){const n=[];for(const e of i)for(const s of t)n.push([e,s]);return n}function*pe(i,t,n){const e=o=>t.some(l=>b(l,o.lastPoint)),s=o=>n.some(l=>o.isSame(l));let r=[];for(const o of i)e(o)?(r.push(o),yield new rt(r,{ignoreChecks:!0}),r=[]):s(o)?(r.length&&(yield new rt(r,{ignoreChecks:!0}),r=[]),yield new rt([o],{ignoreChecks:!0})):r.push(o);r.length&&(yield new rt(r,{ignoreChecks:!0}))}const Je=(i,t)=>{const n=i.findIndex(r=>b(t,r.firstPoint)),e=i.slice(0,n);return i.slice(n).concat(e)},Ze=(i,t)=>{let n=i;const e=l=>b(l.firstPoint,t.firstPoint)&&b(l.lastPoint,t.lastPoint);let s=i.findIndex(e);if(s===-1){const l=i.map(a=>a.reverse());if(l.reverse(),s=l.findIndex(e),s===-1)throw console.error(l.map(a=>a.repr),t.repr),new Error("Failed to rotate to segment start");n=l}const r=n.slice(0,s);return n.slice(s).concat(r)};function si(i,t,n){return i.filter(e=>{const s=t.filter(l=>b(l.firstPoint,e)||b(l.lastPoint,e));if(s.length%2)throw new Error("Bug in the intersection algo on non crossing point");const r=s.map(l=>n.contains(l.midPoint));return!(r.every(l=>l)||!r.some(l=>l))})}function ii(i,t,n){let e=[];const s=[],r=new Array(i.segments.length).fill(0).map(()=>[]),o=new Array(t.segments.length).fill(0).map(()=>[]);if(i.segments.forEach((f,p)=>{t.segments.forEach((g,w)=>{const{intersections:P,overlaps:m}=qt(f,g,n);e.push(...P),r[p].push(...P),o[w].push(...P),s.push(...m);const d=m.flatMap(y=>[y.firstPoint,y.lastPoint]);e.push(...d),r[p].push(...d),o[w].push(...d)})}),e=pt(e,n),!e.length||e.length===1)return null;const l=([f,p])=>p.length?f.splitAt(p):[f];let a=ht([i.segments,r]).flatMap(l),h=ht([t.segments,o]).flatMap(l);if(e=si(e,a,t),!e.length&&!s.length)return null;if(s.length){const f=s[0];a=Ze(a,f),h=Ze(h,f)}else{const f=e[0];a=Je(a,f),h=Je(h,f)}let c=Array.from(pe(a,e,s)),u=Array.from(pe(h,e,s));return(!b(u[0].lastPoint,c[0].lastPoint)||s.length>0&&u[0].segmentsCount!==1)&&(u=u.map(f=>f.reverse()).reverse(),b(u[0].lastPoint,c[0].lastPoint)||(c=c.map(f=>f.reverse()).reverse())),ht([c,u]).map(([f,p])=>f.segmentsCount===1&&s.some(g=>f.segments[0].isSame(g))?[f,"same"]:[f,p])}function Ke(i){let t=i[0];for(const n of i.slice(1))t=t.extend(n);if(!b(t.firstPoint,t.lastPoint))throw console.error(W(t.firstPoint),W(t.lastPoint)),new Error("Bug in the intersection algo on non closing strand");return new gt(t.segments)}function ri(i,t){const n=ht([t.slice(0,-1),t.slice(1)]).map(([s,r])=>Ke(i.slice(s,r)));let e=i.slice(t[t.length-1]);return t[0]!==0&&(e=e.concat(i.slice(0,t[0]))),n.push(Ke(e)),n}function oi(i){if(!i.length)return[];const t=i.map(s=>s.firstPoint);let n=i.map(s=>s.lastPoint);n=n.slice(-1).concat(n.slice(0,-1));const e=ht([t,n]).flatMap(([s,r],o)=>b(s,r)?[]:o);try{return ri(i,e)}catch{return _e(i.flatMap(r=>r.segments)).filter(r=>r.length>1).filter(r=>b(r[0].firstPoint,r.at(-1).lastPoint)).map(r=>new gt(r))}}const He=(i,t)=>{if(i.length===0)return[t];const n=i.at(-1);return b(n.lastPoint,t.firstPoint)?i.slice(0,-1).concat([n.extend(t)]):b(n.lastPoint,t.lastPoint)?i.slice(0,-1).concat([n.extend(t.reverse())]):i.concat([t])},li=(i,t)=>i.length===0?[t]:b(i[0].firstPoint,t.lastPoint)?[t.extend(i[0])].concat(i.slice(1)):[t].concat(i);function Ie(i,t,{firstInside:n,secondInside:e}){const s=ii(i,t);if(!s){const a=i.segments[0].midPoint,h=t.contains(a),c=t.segments[0].midPoint,u=i.contains(c);return{identical:!1,firstCurveInSecond:h,secondCurveInFirst:u}}if(s.every(([,a])=>a==="same"))return{identical:!0};let r=null,o=null;const l=s.flatMap(([a,h])=>{let c=[],u=0;if(h==="same")return o===1?(o=1,a):o===2||o===0?(o=null,[]):o===null?(r?r=r.extend(a):r=a,[]):(console.error("weird situation"),[]);const f=a.segments[0].midPoint,p=t.contains(f);(n==="keep"&&p||n==="remove"&&!p)&&(u+=1,c=He(c,a));const g=h.segments[0].midPoint,w=i.contains(g);if(e==="keep"&&w||e==="remove"&&!w){const P=h;u+=1,u===2&&c.length?(c=He(c,P),r=null):c=[P]}return o===null&&u===1&&r&&(c=li(c,r)),u===1&&(o=u,r=null),c.length?c:(r=null,[])});return oi(l)}const hi=(i,t)=>{const n=Ie(i,t,{firstInside:"remove",secondInside:"remove"});return Array.isArray(n)?n:n.identical?[i]:n.firstCurveInSecond?[t]:n.secondCurveInFirst?[i]:[i,t]},Kt=(i,t)=>{const n=Ie(i,t,{firstInside:"remove",secondInside:"keep"});return Array.isArray(n)?n:n.identical?[]:n.firstCurveInSecond?[]:n.secondCurveInFirst?[i,t]:[i]},Ce=(i,t)=>{const n=Ie(i,t,{firstInside:"keep",secondInside:"keep"});return Array.isArray(n)?n:n.identical?[i]:n.firstCurveInSecond?[i]:n.secondCurveInFirst?[t]:[]};function ai(i){const t=new Map,n=[];return i.forEach((e,s)=>{let r;t.has(s)?r=t.get(s):(r={current:[e],fusedWith:new Set([s])},n.push(r)),i.slice(s+1).forEach((o,l)=>{const a=r.current,h=s+l+1;if(r.fusedWith.has(h))return;let c=[o],u=!1;if(t.has(h)&&(c=t.get(h).current,u=!0),!a.some(g=>c.some(w=>g.intersects(w))))return;let p;a.length>1||c.length>1?p=jt(a,c):p=Mn(a[0],c[0]),r.fusedWith.add(h),r.current=p,u||t.set(h,r)})}),n.flatMap(({current:e})=>e)}function Mn(i,t){const n=hi(i.contour,t.contour),e=t.holes.flatMap(o=>Kt(o,i.contour)),s=i.holes.flatMap(o=>Kt(o,t.contour)),r=ni(i.holes,t.holes).flatMap(([o,l])=>Ce(o,l));return Rt([...n,...e,...s,...r])}function Ht(i,t){if(i.isFull&&t.isFull)return Rt(Kt(i.contour,t.contour));if(i.isFull){const e=Kt(i.contour,t.contour),s=t.holes.flatMap(r=>Ce(r,i.contour));return Rt([...e,...s])}else if(t.isFull&&!i.contour.intersects(t.contour))if(i.contour.contains(t.contour.firstPoint)){const e=jt(i.holes.map(s=>new it(s)),[t]);return Rt([i.contour,...e.flatMap(s=>s.allLoops)])}else return[i];let n=Ht(new it(i.contour),t);return i.holes.forEach(e=>{n=n.flatMap(s=>Ht(s,new it(e)))}),n}function ui(i,t){const n=Ce(i.contour,t.contour);if(!n.length)return[];let e=Rt(n);return e=vt(e,i.holes.map(s=>new it(s))),vt(e,t.holes.map(s=>new it(s)))}function jt(i,t){if(!i.length)return t;if(!t.length)return i;if(i.length===1&&t.length>1||t.length===1&&i.length>1)return ai([...i,...t]);if(i.length>1&&t.length>1){let n=jt([i[0]],t);return i.slice(1).forEach(e=>{n=jt([e],n)}),n}return i.length===1&&t.length===1?Mn(i[0],t[0]):[]}function vt(i,t){if(!i.length)return[];if(!t.length)return i;if(i.length===1&&t.length===1)return Ht(i[0],t[0]);if(i.length>1)return i.flatMap(e=>vt([e],t));let n=Ht(i[0],t[0]);return t.slice(1).forEach(e=>{n=vt(n,[e])}),n}function te(i,t){return!i.length||!t.length?[]:i.length===1&&t.length===1?ui(i[0],t[0]):i.length>1?i.flatMap(n=>te([n],t)):t.flatMap(n=>te(i,[n]))}class mt extends Bt{constructor(t=[],{ignoreChecks:n=!1}={}){super(),this._boundingBox=null,n||ci(t),this.figures=t}get isEmpty(){return this.figures.length===0}get boundingBox(){if(this.isEmpty)return new ct;if(this._boundingBox===null){let t=this.figures[0].boundingBox;for(const n of this.figures.slice(1))t=t.merge(n.boundingBox);this._boundingBox=t}return this._boundingBox}clone(){return new mt(this.figures.map(t=>t.clone()))}transform(t){return new mt(this.figures.map(n=>n.transform(t)))}contains(t){return this.figures.some(n=>n.contains(t))}intersects(t){return this.figures.some(n=>t.figures.some(e=>n.intersects(e)))}overlappingStrands(t){return this.figures.flatMap(n=>t instanceof mt?t.figures.flatMap(e=>n.overlappingStrands(e)):n.overlappingStrands(t))}fuse(t){return new mt(jt(this.figures,t.figures))}cut(t){return new mt(vt(this.figures,t.figures))}intersect(t){return new mt(te(this.figures,t.figures))}}function ci(i){for(const[t,n]of ce(i))if(t.intersects(n))throw new Error("Diagram figures must not intersect")}exports.Arc=R;exports.BoundingBox=ct;exports.CubicBezier=Y;exports.DEG2RAD=ee;exports.Diagram=mt;exports.EllipseArc=z;exports.Figure=it;exports.Line=B;exports.Loop=gt;exports.QuadraticBezier=N;exports.RAD2DEG=tn;exports.Strand=rt;exports.Transformable=Bt;exports.TransformationMatrix=H;exports.add=J;exports.arcArcIntersection=Me;exports.cartesianToPolar=nn;exports.crossProduct=Pt;exports.cutFiguresLists=vt;exports.distance=G;exports.exportJSON=me;exports.findIntersections=$s;exports.findIntersectionsAndOverlaps=qt;exports.fuseFiguresLists=jt;exports.intersectFiguresLists=te;exports.isSegment=wn;exports.lineArcIntersection=At;exports.lineLineParams=Lt;exports.normalize=Q;exports.perpendicular=et;exports.perpendicularClockwise=Tt;exports.polarAngle=en;exports.polarToCartesian=Ct;exports.projectPointOnLine=an;exports.removeDuplicatePoints=pt;exports.sameVector=b;exports.scalarMultiply=nt;exports.squareDistance=yt;exports.stitchSegments=_e;exports.strandsBetweenIntersections=pe;exports.subtract=$;exports.svgEllipse=ds;exports.tangentArc=Cn;exports.threePointsArc=In;exports.zip=ht;
|
|
11
|
-
//# sourceMappingURL=Diagram-d8288579.cjs.map
|