dielines 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dielines.js +3032 -3020
- package/dist/dielines.js.map +1 -1
- package/dist/dielines.umd.cjs +15 -15
- package/dist/dielines.umd.cjs.map +1 -1
- package/dist/examples/framePieces.d.ts +3 -0
- package/dist/examples/noGlueTray.d.ts +4 -0
- package/dist/{types → src}/Dieline.d.ts +2 -2
- package/dist/{types → src}/FoldLockBump.d.ts +2 -2
- package/dist/{types → src}/drawFlaps.d.ts +4 -4
- package/dist/{types → src}/linearDistribution.d.ts +2 -2
- package/dist/src/main.d.ts +4 -0
- package/package.json +10 -10
- package/dist/types/main.d.ts +0 -4
package/dist/dielines.umd.cjs
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
(function(rt,wt){typeof exports=="object"&&typeof module<"u"?wt(exports):typeof define=="function"&&define.amd?define(["exports"],wt):(rt=typeof globalThis<"u"?globalThis:rt||self,wt(rt.Dielines={}))})(this,function(rt){"use strict";function wt(s,t,e,n){return s<=n&&t>=e}let qt=class Dt{constructor(t=1/0,e=1/0,n=-1/0,r=-1/0){this.xMin=t,this.yMin=e,this.xMax=n,this.yMax=r}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 Dt(this.xMin-t,this.yMin-t,this.xMax+t,this.yMax+t)}contains(t){const[e,n]=t;return wt(this.xMin,this.xMax,e,e)&&wt(this.yMin,this.yMax,n,n)}overlaps(t){return wt(this.xMin,this.xMax,t.xMin,t.xMax)&&wt(this.yMin,this.yMax,t.yMin,t.yMax)}addPoint(t){const[e,n]=t;return new Dt(Math.min(this.xMin,e),Math.min(this.yMin,n),Math.max(this.xMax,e),Math.max(this.yMax,n))}merge(t){return new Dt(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 Dt(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 me(s){return s.reduce((t,e)=>t.addPoint(e),new qt)}const Ve=(s,t=1e-9)=>{let e=s;return Math.abs(s)<t&&(e=0),e.toFixed(-Math.log10(t))};function mt(s,t=1e-9){return Array.from(new Map(s.map(([e,n])=>[`[${Ve(e,t)},${Ve(n,t)}]`,[e,n]])).values())}const xt=Math.PI/180,ge=180/Math.PI,H=s=>`[${s[0]}, ${s[1]}]`,y=([s,t],[e,n],r=1e-9)=>Math.abs(s-e)<=r&&Math.abs(t-n)<=r,V=([s,t],[e,n])=>[s+e,t+n],q=([s,t],[e,n])=>[s-e,t-n],ct=([s,t])=>s*s+t*t,ss=([s,t])=>Math.sqrt(ct([s,t])),W=([s,t],e)=>[s*e,t*e],vt=([s,t],[e,n]=[0,0])=>(s-e)**2+(t-n)**2,z=(s,t=[0,0])=>Math.sqrt(vt(s,t));function dt([s,t],[e,n]){return s*n-t*e}function pe([s,t],[e,n]){return s*e+t*n}function J([s,t]){const e=z([s,t]);return[s/e,t/e]}function gt(s,t){const e=Math.cos(t)*s,n=Math.sin(t)*s;return[e,n]}function rs([s,t]){return Math.atan2(t,s)}function ze(s){const t=z(s),e=rs(s);return[t,e]}function we(s,t,e=1e-9){const n=dt(s,t),r=ct(s),o=ct(t);return n*n<r*o*e*e}function G(s){return[-s[1],s[0]]}function Ot(s){return[s[1],-s[0]]}function os(s,t,e){return y(e,s)||we(q(e,s),t)}const Ct=(s,t)=>{const[e,n,r,o,i,h,l,a,c]=s,[u,f,p,g,w,P,m,d,M]=t;return[e*u+n*g+r*m,e*f+n*w+r*d,e*p+n*P+r*M,o*u+i*g+h*m,o*f+i*w+h*d,o*p+i*P+h*M,l*u+a*g+c*m,l*f+a*w+c*d,l*p+a*P+c*M]},is=s=>{const[t,e,n,r,o,i,h,l,a]=s,c=t*(o*a-i*l)-e*(r*a-i*h)+n*(r*l-o*h);return[(o*a-i*l)/c,(n*l-e*a)/c,(e*i-n*o)/c,(i*h-r*a)/c,(t*a-n*h)/c,(n*r-t*i)/c,(r*l-o*h)/c,(e*h-t*l)/c,(t*o-e*r)/c]},hs=s=>{const[t,e,n,r,o,i,h,l,a]=s;return[t,r,h,e,o,l,n,i,a]};class tt{constructor(t){this._matrix=[1,0,0,0,1,0,0,0,1],t&&(this._matrix=[...t])}clone(){return new tt(this._matrix)}transpose(){return this._matrix=hs(this._matrix),this}inverse(){return this._matrix=is(this._matrix),this}translate(t,e){return this._matrix=Ct(this._matrix,[1,0,t,0,1,e,0,0,1]),this}rotate(t,e){const n=Math.cos(t),r=Math.sin(t),o=[n,-r,0,r,n,0,0,0,1];return e&&this.translate(e[0],e[1]),this._matrix=Ct(this._matrix,o),e&&this.translate(-e[0],-e[1]),this}mirrorX(){return this._matrix=Ct(this._matrix,[1,0,0,0,-1,0,0,0,1]),this}mirrorY(){return this._matrix=Ct(this._matrix,[-1,0,0,0,1,0,0,0,1]),this}mirrorLine(t,e){const[n,r]=t,o=Math.atan2(r,n);return e&&this.translate(e[0],e[1]),this.rotate(o),this.mirrorX(),this.rotate(-o),e&&this.translate(-e[0],-e[1]),this}mirrorCenter(t){return t&&this.translate(t[0],t[1]),this._matrix=Ct(this._matrix,[-1,0,0,0,-1,0,0,0,1]),t&&this.translate(-t[0],-t[1]),this}scale(t,e){return e&&this.translate(e[0],e[1]),this._matrix=Ct(this._matrix,[t,0,0,0,t,0,0,0,1]),e&&this.translate(-e[0],-e[1]),this}transform(t){const[e,n]=t,[r,o,i,h,l,a]=this._matrix;return[r*e+o*n+i,h*e+l*n+a]}transformAngle(t){const[e,n]=this.transform([Math.cos(t),Math.sin(t)]),[r,o]=this.transform([0,0]);return Math.atan2(n-o,e-r)}keepsOrientation(){const[t,,,,e]=this._matrix;return t*e>0}scaleFactor(){const[t,,,e]=this._matrix;return Math.sqrt(t*t+e*e)}}let At=class{translateX(t){const e=new tt().translate(t,0);return this.transform(e)}translateY(t){const e=new tt().translate(0,t);return this.transform(e)}translate(t,e){const n=new tt().translate(t,e);return this.transform(n)}translateTo([t,e]){const n=new tt().translate(t,e);return this.transform(n)}rotate(t,e){const n=new tt().rotate(t*xt,e);return this.transform(n)}scale(t,e){const n=new tt().scale(t,e);return this.transform(n)}mirrorCenter(t){const e=new tt().mirrorCenter(t);return this.transform(e)}mirror(t="x",e){const n=new tt;return t==="x"?n.mirrorX():t==="y"?n.mirrorY():n.mirrorLine(t,e),this.transform(n)}};class Nt extends At{constructor(t,e){super(),this.firstPoint=t,this.lastPoint=e,this.precision=1e-9,this.firstPoint=t,this.lastPoint=e}get repr(){return`${this.segmentType} ${H(this.firstPoint)} - ${H(this.lastPoint)}`}get info(){return this.repr}[Symbol.for("nodejs.util.inspect.custom")](){return this.repr}}let B=class Lt extends Nt{constructor(){super(...arguments),this.segmentType="LINE",this._V=null,this._slope=null,this._yIntercept=null,this._boundingBox=null}isValidParameter(t){const e=this.length*this.precision;return t>=-e&&1-t>=-e}paramPoint(t){return V(this.firstPoint,W(this.V,t))}get length(){return z(this.firstPoint,this.lastPoint)}get squareLength(){return vt(this.firstPoint,this.lastPoint)}get V(){return this._V===null&&(this._V=q(this.lastPoint,this.firstPoint)),this._V}get slope(){if(this._slope===null){const[t,e]=this.V;this._slope=e/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 V(this.firstPoint,W(this.V,.5))}isSame(t){return t instanceof Lt?y(this.firstPoint,t.firstPoint)&&y(this.lastPoint,t.lastPoint)||y(this.lastPoint,t.firstPoint)&&y(this.firstPoint,t.lastPoint):!1}clone(){return new Lt(this.firstPoint,this.lastPoint)}reverse(){return new Lt(this.lastPoint,this.firstPoint)}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=new qt(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 e=q(t,this.firstPoint),n=pe(e,this.V)/this.squareLength;if(n<0)return z(t,this.firstPoint);if(n>1)return z(t,this.lastPoint);const r=this.paramPoint(n);return z(t,r)}isOnSegment(t){if(y(t,this.firstPoint,this.precision))return!0;const e=q(t,this.firstPoint);if(!we(this.V,e))return!1;const n=pe(e,this.V)/this.squareLength;return this.isValidParameter(n)}gradientAt(t){return this.V}tangentAt(t){if(!this.isOnSegment(t))throw new Error("Point is not on segment");return J(this.V)}get normalVector(){return G(J(this.V))}get tangentAtFirstPoint(){return J(this.V)}get tangentAtLastPoint(){return J(this.V)}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t],e.forEach(l=>{if(!this.isOnSegment(l))throw new Error(`Point ${H(l)} is not on segment ${this.repr}`)});const n=[this.firstPoint,...e,this.lastPoint],r=mt(n),o=this.lastPoint[0]-this.firstPoint[0];let i=Math.sign(o),h=0;return Math.abs(o)<this.precision&&(i=Math.sign(this.lastPoint[1]-this.firstPoint[1]),h=1),r.sort((l,a)=>i*(l[h]-a[h])),r.flatMap((l,a)=>a===r.length-1?[]:new Lt(l,r[a+1]))}transform(t){return new Lt(t.transform(this.firstPoint),t.transform(this.lastPoint))}};function as(s){return Array.from(Array(s).keys())}function at(s){const t=Math.min(...s.map(e=>e.length));return as(t).map(e=>s.map(n=>n[e]))}function Et(s,t=1e-9){return s<0?s+2*Math.PI:s>=2*Math.PI?s%(2*Math.PI):s>2*Math.PI-t?0:s}function Ht(s,t,e,n=1e-9){let r=t-s;return e&&(r=-r),r<0&&(r+=2*Math.PI),r>2*Math.PI-n?0:r}const Wt=(s,t,e)=>{const n=dt(s.V,t.V),r=ct(s.V),o=ct(t.V),i=e?e*e:s.precision*t.precision;if(n*n<r*o*i)return"parallel";const h=q(t.firstPoint,s.firstPoint),l=dt(h,t.V)/n,a=dt(h,s.V)/n;return{intersectionParam1:l,intersectionParam2:a}};function Ue(s,t,e=!1,n){const r=Wt(s,t,n);if(r==="parallel"){if(!e)return null;if(s.isSame(t))return s;const h=mt([t.isOnSegment(s.firstPoint)?s.firstPoint:null,t.isOnSegment(s.lastPoint)?s.lastPoint:null,s.isOnSegment(t.firstPoint)?t.firstPoint:null,s.isOnSegment(t.lastPoint)?t.lastPoint:null].filter(l=>l!==null)).sort((l,a)=>l[0]-a[0]);if(h.length===0||h.length===1)return null;if(h.length===2)return new B(h[0],h[1]);throw console.error(h),new Error("Unexpected number of points while intersecting parallel lines")}const{intersectionParam1:o,intersectionParam2:i}=r;return!s.isValidParameter(o)||!t.isValidParameter(i)?null:s.paramPoint(o)}const Jt=(s,t)=>{const e=q(s,t);return ze(e)};class T extends Nt{constructor(t,e,n,r=!1,{ignoreChecks:o=!1}={}){if(super(t,e),this.segmentType="ARC",this._coefficients=null,this._angularLength=null,this._radius=null,this._firstAngle=null,this._lastAngle=null,this._boundingBox=null,this.center=n,this.clockwise=r,!o){if(y(t,e))throw new Error("Invalid arc, cannot be a full circle");if(Math.abs(this.radius-z(this.lastPoint,this.center))>this.precision)throw new Error(`Invalid arc, radius does not match between ${H(t)} and ${H(e)}} (center ${H(n)})`)}}get info(){return`ARC(${H(this.firstPoint)}, ${H(this.lastPoint)}, ${H(this.center)}, ${this.clockwise?"CW":"CCW"})`}get coefficients(){if(this._coefficients===null){const[t,e]=this.center,n=this.radius*this.radius;this._coefficients={x2:1/n,xy:0,y2:1/n,x:-(2*t)/n,y:-(2*e)/n,c:(t*t+e*e-n)/n}}return this._coefficients}isValidParameter(t){return 1-t>=-this.precision&&t>=-this.precision}angleToParam(t){return Ht(this.firstAngle,Et(t),this.clockwise)/this.angularLength}get angularLength(){return this._angularLength||(this._angularLength=Ht(this.firstAngle,this.lastAngle,this.clockwise)),this._angularLength}paramPoint(t){return V(this.center,gt(this.radius,this.firstAngle+t*this.angularLength*(this.clockwise?-1:1)))}pointToParam(t){const[e,n]=Jt(t,this.center);if(Math.abs(e-this.radius)>this.precision)throw new Error(`Point ${H(t)} is not on segment ${this.repr}`);const r=this.angleToParam(n);if(!this.isValidParameter(r))throw new Error(`Point ${H(t)} is not on segment ${this.repr}`);return r}get radius(){return this._radius===null&&(this._radius=z(this.firstPoint,this.center)),this._radius}get firstAngle(){if(this._firstAngle===null){const[t,e]=q(this.firstPoint,this.center);this._firstAngle=Et(Math.atan2(e,t))}return this._firstAngle}get lastAngle(){if(this._lastAngle===null){const[t,e]=q(this.lastPoint,this.center);this._lastAngle=Et(Math.atan2(e,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 T)||!y(this.center,t.center)?!1:y(this.firstPoint,t.firstPoint)&&y(this.lastPoint,t.lastPoint)&&this.clockwise===t.clockwise||y(this.lastPoint,t.firstPoint)&&y(this.firstPoint,t.lastPoint)&&this.clockwise===!t.clockwise}clone(){return new T(this.firstPoint,this.lastPoint,this.center,this.clockwise)}reverse(){return new T(this.lastPoint,this.firstPoint,this.center,!this.clockwise)}get boundingBox(){if(this._boundingBox===null){const t=this.radius+this.precision,e=n=>this.isValidParameter(this.angleToParam(n));this._boundingBox=new qt(e(Math.PI)?this.center[0]-t:Math.min(this.firstPoint[0],this.lastPoint[0])-this.precision,e(Math.PI*1.5)?this.center[1]-t:Math.min(this.firstPoint[1],this.lastPoint[1])-this.precision,e(0)?this.center[0]+t:Math.max(this.firstPoint[0],this.lastPoint[0])+this.precision,e(Math.PI/2)?this.center[1]+t:Math.max(this.firstPoint[1],this.lastPoint[1])+this.precision)}return this._boundingBox}distanceFrom(t){const[e,n]=Jt(t,this.center);return this.isValidParameter(this.angleToParam(n))?Math.abs(e-this.radius):Math.sqrt(Math.min(vt(t,this.firstPoint),vt(t,this.lastPoint)))}isOnSegment(t){if(y(t,this.firstPoint)||y(t,this.lastPoint))return!0;const[e,n]=Jt(t,this.center);if(Math.abs(e-this.radius)>this.precision)return!1;const r=this.angleToParam(n);return this.isValidParameter(r)}gradientAt(t){const e=this.firstAngle+t*this.angularLength*(this.clockwise?-1:1),n=this.radius*this.angularLength,r=-n*Math.sin(e),o=n*Math.cos(e);return this.clockwise?[-r,-o]:[r,o]}tangentAt(t){const[e,n]=Jt(t,this.center);if(Math.abs(e-this.radius)>this.precision)throw new Error("Point is not on the arc");const r=this.angleToParam(n);if(!this.isValidParameter(r))throw new Error("Point is not on the arc");const o=gt(1,n);return(this.clockwise?Ot:G)(J(o))}get tangentAtFirstPoint(){const t=gt(1,this.firstAngle);return(this.clockwise?Ot:G)(J(t))}get tangentAtLastPoint(){const t=gt(1,this.lastAngle);return(this.clockwise?Ot:G)(J(t))}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const n=[0,1,...e.map(i=>this.pointToParam(i))],r=new Map(at([n,[this.firstPoint,this.lastPoint,...e]]));n.sort((i,h)=>i-h);let o=null;return n.flatMap((i,h)=>{if(h===n.length-1)return[];const l=n[h+1];if(l-i<this.precision)return o===null&&(o=i),[];const a=o===null?i:o,c=new T(r.get(a)||this.paramPoint(a),r.get(l)||this.paramPoint(l),this.center,this.clockwise);return o=null,c})}transform(t){return new T(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.center),t.keepsOrientation()?this.clockwise:!this.clockwise)}}function ls(s,t,e){const n=new B(t,s),r=new B(t,e),o=G(n.tangentAtFirstPoint),i=G(r.tangentAtLastPoint),h=Wt({firstPoint:n.midPoint,V:o,precision:1e-9},{firstPoint:r.midPoint,V:i,precision:1e-9});if(h==="parallel")throw new Error("Cannot create an arc from three colinear points");const l=dt(q(s,t),q(e,t))>0;return new T(s,e,V(n.midPoint,W(o,h.intersectionParam1)),l,{ignoreChecks:!0})}function Ye(s,t,e){const n=new B(t,s),r=G(n.tangentAtFirstPoint),o=Wt({firstPoint:n.midPoint,V:r,precision:1e-9},{firstPoint:s,V:G(e),precision:1e-9});if(o==="parallel")throw new Error("Cannot create an arc from three colinear points");const i=V(n.midPoint,W(r,o.intersectionParam1)),h=dt(q(i,s),q(i,V(s,e)))<0;return new T(s,t,i,h,{ignoreChecks:!0})}const De=1e-21,Zt=1.618034;function Xe(s,t=0,e=1,n=110,r=1e3){let o,i,h,l,a,c,u,f,p,g,w,P,m;for(i=s(t),h=s(e),i<h&&([t,e]=[e,t],[i,h]=[h,i]),m=e+Zt*(e-t),l=s(m),a=3,u=0;l<h;){if(f=(e-t)*(h-l),p=(e-m)*(h-i),g=p-f,Math.abs(g)<De?o=2*De:o=2*g,w=e-((e-m)*p-(e-t)*f)/o,P=e+n*(m-e),u>r)throw new Error("Too many iterations.");if(u+=1,(w-m)*(e-w)>0){if(c=s(w),a+=1,c<l)return t=e,e=w,i=h,h=c,[t,e,m,i,h,l,a];if(c>h)return m=w,l=c,[t,e,m,i,h,l,a];w=m+Zt*(m-e),c=s(w),a+=1}else(w-P)*(P-m)>=0?(w=P,c=s(w),a+=1):(w-P)*(m-w)>0?(c=s(w),a+=1,c<l&&(e=m,m=w,w=m+Zt*(m-e),h=l,l=c,c=s(w),a+=1)):(w=m+Zt*(m-e),c=s(w),a+=1);t=e,e=m,m=w,i=h,h=l,l=c}return[t,e,m,i,h,l,a]}const He=.381966,us=1e-11;class cs{constructor(t,e=148e-10,n=500){this.func=t,this.tol=e,this.maxiter=n,this.func=t,this.tol=e,this.maxiter=n,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,e,n,r,o,i,h;const l=this.func,a=this.brack;if(a===null)[o,i,h,t,e,n,r]=Xe(l);else if(a.length===2)[o,i,h,t,e,n,r]=Xe(l,a[0],a[1]);else if(a.length===3){if([o,i,h]=a,o>h&&([h,o]=[o,h]),!(o<i&&i<h))throw new Error("Not a bracketing interval.");if(t=l(o),e=l(i),n=l(h),!(e<t&&e<n))throw new Error("Not a bracketing interval.");r=3}else throw new Error("Bracketing interval must be length 2 or 3 sequence.");return[o,i,h,t,e,n,r]}optimize(){const t=this.func;let[e,n,r,,,,o]=this.getBracketInfo(),i=n,h=n,l=n,a=t(i),c=a,u=a,f,p;e<r?(f=e,p=r):(f=r,p=e);let g=0;o=1;let w=0,P=0,m;for(;w<this.maxiter;){const d=this.tol*Math.abs(i)+us,M=2*d,x=.5*(f+p);if(Math.abs(i-x)<M-.5*(p-f))break;if(Math.abs(g)<=d)i>=x?g=f-i:g=p-i,P=He*g;else{const k=(i-h)*(u-c);let A=(i-l)*(u-a),b=(i-l)*A-(i-h)*k;A=2*(A-k),A>0&&(b=-b),A=Math.abs(A);const I=g;g=P,b>A*(f-i)&&b<A*(p-i)&&Math.abs(b)<Math.abs(.5*A*I)?(P=b*1/A,m=i+P,(m-f<M||p-m<M)&&(x-i>=0?P=d:P=-d)):(i>=x?g=f-i:g=p-i,P=He*g)}Math.abs(P)<d?P>=0?m=i+d:m=i-d:m=i+P;const v=t(m);o+=1,v>u?(m<i?f=m:p=m,v<=a||h===i?(l=h,h=m,c=a,a=v):(v<=c||l===i||l===h)&&(l=m,c=v)):(m>=i?f=i:p=i,l=h,h=i,i=m,c=a,a=u,u=v),w+=1}this.xmin=i,this.fval=u,this.iter=w,this.funcalls=o}results(){return{argMin:this.xmin,fMin:this.fval,iterations:this.iter,funcCalls:this.funcalls}}run(){return this.optimize(),this.results()}}function We(s,t=1e-8,e=1e3){const n=new cs(s,t,e);return n.setBracket([0,1]),n.run()}const fs=Object.prototype.toString;function ot(s){const t=fs.call(s);return t.endsWith("Array]")&&!t.includes("Big")}function ms(s){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!ot(s))throw new TypeError("input must be an array");if(s.length===0)throw new TypeError("input must not be empty");var e=t.fromIndex,n=e===void 0?0:e,r=t.toIndex,o=r===void 0?s.length:r;if(n<0||n>=s.length||!Number.isInteger(n))throw new Error("fromIndex must be a positive integer smaller than length");if(o<=n||o>s.length||!Number.isInteger(o))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var i=s[n],h=n+1;h<o;h++)s[h]>i&&(i=s[h]);return i}function gs(s){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!ot(s))throw new TypeError("input must be an array");if(s.length===0)throw new TypeError("input must not be empty");var e=t.fromIndex,n=e===void 0?0:e,r=t.toIndex,o=r===void 0?s.length:r;if(n<0||n>=s.length||!Number.isInteger(n))throw new Error("fromIndex must be a positive integer smaller than length");if(o<=n||o>s.length||!Number.isInteger(o))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var i=s[n],h=n+1;h<o;h++)s[h]<i&&(i=s[h]);return i}function Je(s){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(ot(s)){if(s.length===0)throw new TypeError("input must not be empty")}else throw new TypeError("input must be an array");var e;if(t.output!==void 0){if(!ot(t.output))throw new TypeError("output option must be an array if specified");e=t.output}else e=new Array(s.length);var n=gs(s),r=ms(s);if(n===r)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var o=t.min,i=o===void 0?t.autoMinMax?n:0:o,h=t.max,l=h===void 0?t.autoMinMax?r:1:h;if(i>=l)throw new RangeError("min option must be smaller than max option");for(var a=(l-i)/(r-n),c=0;c<s.length;c++)e[c]=(s[c]-n)*a+i;return e}const Gt=" ".repeat(2),Ze=" ".repeat(4);function ps(){return Ge(this)}function Ge(s,t={}){const{maxRows:e=15,maxColumns:n=10,maxNumSize:r=8,padMinus:o="auto"}=t;return`${s.constructor.name} {
|
|
2
|
-
${
|
|
3
|
-
${
|
|
4
|
-
${
|
|
5
|
-
${
|
|
6
|
-
${
|
|
7
|
-
}`}function
|
|
8
|
-
${Ze}`)}function ds(s,t,e){return(s>=0&&e?` ${Ke(s,t-1)}`:Ke(s,t)).padEnd(t)}function Ke(s,t){let e=s.toString();if(e.length<=t)return e;let n=s.toFixed(t);if(n.length>t&&(n=s.toFixed(Math.max(0,t-(n.length-t)))),n.length<=t&&!n.startsWith("0.000")&&!n.startsWith("-0.000"))return n;let r=s.toExponential(t);return r.length>t&&(r=s.toExponential(Math.max(0,t-(r.length-t)))),r.slice(0)}function Ps(s,t){s.prototype.add=function(e){return typeof e=="number"?this.addS(e):this.addM(e)},s.prototype.addS=function(e){for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)+e);return this},s.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 n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)+e.get(n,r));return this},s.add=function(e,n){return new t(e).add(n)},s.prototype.sub=function(e){return typeof e=="number"?this.subS(e):this.subM(e)},s.prototype.subS=function(e){for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)-e);return this},s.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 n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)-e.get(n,r));return this},s.sub=function(e,n){return new t(e).sub(n)},s.prototype.subtract=s.prototype.sub,s.prototype.subtractS=s.prototype.subS,s.prototype.subtractM=s.prototype.subM,s.subtract=s.sub,s.prototype.mul=function(e){return typeof e=="number"?this.mulS(e):this.mulM(e)},s.prototype.mulS=function(e){for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)*e);return this},s.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 n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)*e.get(n,r));return this},s.mul=function(e,n){return new t(e).mul(n)},s.prototype.multiply=s.prototype.mul,s.prototype.multiplyS=s.prototype.mulS,s.prototype.multiplyM=s.prototype.mulM,s.multiply=s.mul,s.prototype.div=function(e){return typeof e=="number"?this.divS(e):this.divM(e)},s.prototype.divS=function(e){for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)/e);return this},s.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 n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)/e.get(n,r));return this},s.div=function(e,n){return new t(e).div(n)},s.prototype.divide=s.prototype.div,s.prototype.divideS=s.prototype.divS,s.prototype.divideM=s.prototype.divM,s.divide=s.div,s.prototype.mod=function(e){return typeof e=="number"?this.modS(e):this.modM(e)},s.prototype.modS=function(e){for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)%e);return this},s.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 n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)%e.get(n,r));return this},s.mod=function(e,n){return new t(e).mod(n)},s.prototype.modulus=s.prototype.mod,s.prototype.modulusS=s.prototype.modS,s.prototype.modulusM=s.prototype.modM,s.modulus=s.mod,s.prototype.and=function(e){return typeof e=="number"?this.andS(e):this.andM(e)},s.prototype.andS=function(e){for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)&e);return this},s.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 n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)&e.get(n,r));return this},s.and=function(e,n){return new t(e).and(n)},s.prototype.or=function(e){return typeof e=="number"?this.orS(e):this.orM(e)},s.prototype.orS=function(e){for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)|e);return this},s.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 n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)|e.get(n,r));return this},s.or=function(e,n){return new t(e).or(n)},s.prototype.xor=function(e){return typeof e=="number"?this.xorS(e):this.xorM(e)},s.prototype.xorS=function(e){for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)^e);return this},s.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 n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)^e.get(n,r));return this},s.xor=function(e,n){return new t(e).xor(n)},s.prototype.leftShift=function(e){return typeof e=="number"?this.leftShiftS(e):this.leftShiftM(e)},s.prototype.leftShiftS=function(e){for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)<<e);return this},s.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 n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)<<e.get(n,r));return this},s.leftShift=function(e,n){return new t(e).leftShift(n)},s.prototype.signPropagatingRightShift=function(e){return typeof e=="number"?this.signPropagatingRightShiftS(e):this.signPropagatingRightShiftM(e)},s.prototype.signPropagatingRightShiftS=function(e){for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)>>e);return this},s.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 n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)>>e.get(n,r));return this},s.signPropagatingRightShift=function(e,n){return new t(e).signPropagatingRightShift(n)},s.prototype.rightShift=function(e){return typeof e=="number"?this.rightShiftS(e):this.rightShiftM(e)},s.prototype.rightShiftS=function(e){for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)>>>e);return this},s.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 n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,this.get(n,r)>>>e.get(n,r));return this},s.rightShift=function(e,n){return new t(e).rightShift(n)},s.prototype.zeroFillRightShift=s.prototype.rightShift,s.prototype.zeroFillRightShiftS=s.prototype.rightShiftS,s.prototype.zeroFillRightShiftM=s.prototype.rightShiftM,s.zeroFillRightShift=s.rightShift,s.prototype.not=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,~this.get(e,n));return this},s.not=function(e){return new t(e).not()},s.prototype.abs=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.abs(this.get(e,n)));return this},s.abs=function(e){return new t(e).abs()},s.prototype.acos=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.acos(this.get(e,n)));return this},s.acos=function(e){return new t(e).acos()},s.prototype.acosh=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.acosh(this.get(e,n)));return this},s.acosh=function(e){return new t(e).acosh()},s.prototype.asin=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.asin(this.get(e,n)));return this},s.asin=function(e){return new t(e).asin()},s.prototype.asinh=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.asinh(this.get(e,n)));return this},s.asinh=function(e){return new t(e).asinh()},s.prototype.atan=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.atan(this.get(e,n)));return this},s.atan=function(e){return new t(e).atan()},s.prototype.atanh=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.atanh(this.get(e,n)));return this},s.atanh=function(e){return new t(e).atanh()},s.prototype.cbrt=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.cbrt(this.get(e,n)));return this},s.cbrt=function(e){return new t(e).cbrt()},s.prototype.ceil=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.ceil(this.get(e,n)));return this},s.ceil=function(e){return new t(e).ceil()},s.prototype.clz32=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.clz32(this.get(e,n)));return this},s.clz32=function(e){return new t(e).clz32()},s.prototype.cos=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.cos(this.get(e,n)));return this},s.cos=function(e){return new t(e).cos()},s.prototype.cosh=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.cosh(this.get(e,n)));return this},s.cosh=function(e){return new t(e).cosh()},s.prototype.exp=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.exp(this.get(e,n)));return this},s.exp=function(e){return new t(e).exp()},s.prototype.expm1=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.expm1(this.get(e,n)));return this},s.expm1=function(e){return new t(e).expm1()},s.prototype.floor=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.floor(this.get(e,n)));return this},s.floor=function(e){return new t(e).floor()},s.prototype.fround=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.fround(this.get(e,n)));return this},s.fround=function(e){return new t(e).fround()},s.prototype.log=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.log(this.get(e,n)));return this},s.log=function(e){return new t(e).log()},s.prototype.log1p=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.log1p(this.get(e,n)));return this},s.log1p=function(e){return new t(e).log1p()},s.prototype.log10=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.log10(this.get(e,n)));return this},s.log10=function(e){return new t(e).log10()},s.prototype.log2=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.log2(this.get(e,n)));return this},s.log2=function(e){return new t(e).log2()},s.prototype.round=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.round(this.get(e,n)));return this},s.round=function(e){return new t(e).round()},s.prototype.sign=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.sign(this.get(e,n)));return this},s.sign=function(e){return new t(e).sign()},s.prototype.sin=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.sin(this.get(e,n)));return this},s.sin=function(e){return new t(e).sin()},s.prototype.sinh=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.sinh(this.get(e,n)));return this},s.sinh=function(e){return new t(e).sinh()},s.prototype.sqrt=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.sqrt(this.get(e,n)));return this},s.sqrt=function(e){return new t(e).sqrt()},s.prototype.tan=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.tan(this.get(e,n)));return this},s.tan=function(e){return new t(e).tan()},s.prototype.tanh=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.tanh(this.get(e,n)));return this},s.tanh=function(e){return new t(e).tanh()},s.prototype.trunc=function(){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,Math.trunc(this.get(e,n)));return this},s.trunc=function(e){return new t(e).trunc()},s.pow=function(e,n){return new t(e).pow(n)},s.prototype.pow=function(e){return typeof e=="number"?this.powS(e):this.powM(e)},s.prototype.powS=function(e){for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,Math.pow(this.get(n,r),e));return this},s.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 n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.set(n,r,Math.pow(this.get(n,r),e.get(n,r)));return this}}function lt(s,t,e){let n=e?s.rows:s.rows-1;if(t<0||t>n)throw new RangeError("Row index out of range")}function ut(s,t,e){let n=e?s.columns:s.columns-1;if(t<0||t>n)throw new RangeError("Column index out of range")}function Tt(s,t){if(t.to1DArray&&(t=t.to1DArray()),t.length!==s.columns)throw new RangeError("vector size must be the same as the number of columns");return t}function Rt(s,t){if(t.to1DArray&&(t=t.to1DArray()),t.length!==s.rows)throw new RangeError("vector size must be the same as the number of rows");return t}function Ms(s,t){if(!ot(t))throw new TypeError("row indices must be an array");for(let e=0;e<t.length;e++)if(t[e]<0||t[e]>=s.rows)throw new RangeError("row indices are out of range")}function ys(s,t){if(!ot(t))throw new TypeError("column indices must be an array");for(let e=0;e<t.length;e++)if(t[e]<0||t[e]>=s.columns)throw new RangeError("column indices are out of range")}function Qe(s,t,e,n,r){if(arguments.length!==5)throw new RangeError("expected 4 arguments");if(Qt("startRow",t),Qt("endRow",e),Qt("startColumn",n),Qt("endColumn",r),t>e||n>r||t<0||t>=s.rows||e<0||e>=s.rows||n<0||n>=s.columns||r<0||r>=s.columns)throw new RangeError("Submatrix indices are out of range")}function Kt(s,t=0){let e=[];for(let n=0;n<s;n++)e.push(t);return e}function Qt(s,t){if(typeof t!="number")throw new TypeError(`${s} must be a number`)}function Ft(s){if(s.isEmpty())throw new Error("Empty matrix has no elements to index")}function bs(s){let t=Kt(s.rows);for(let e=0;e<s.rows;++e)for(let n=0;n<s.columns;++n)t[e]+=s.get(e,n);return t}function xs(s){let t=Kt(s.columns);for(let e=0;e<s.rows;++e)for(let n=0;n<s.columns;++n)t[n]+=s.get(e,n);return t}function vs(s){let t=0;for(let e=0;e<s.rows;e++)for(let n=0;n<s.columns;n++)t+=s.get(e,n);return t}function As(s){let t=Kt(s.rows,1);for(let e=0;e<s.rows;++e)for(let n=0;n<s.columns;++n)t[e]*=s.get(e,n);return t}function Es(s){let t=Kt(s.columns,1);for(let e=0;e<s.rows;++e)for(let n=0;n<s.columns;++n)t[n]*=s.get(e,n);return t}function Ss(s){let t=1;for(let e=0;e<s.rows;e++)for(let n=0;n<s.columns;n++)t*=s.get(e,n);return t}function ks(s,t,e){const n=s.rows,r=s.columns,o=[];for(let i=0;i<n;i++){let h=0,l=0,a=0;for(let c=0;c<r;c++)a=s.get(i,c)-e[i],h+=a,l+=a*a;t?o.push((l-h*h/r)/(r-1)):o.push((l-h*h/r)/r)}return o}function Cs(s,t,e){const n=s.rows,r=s.columns,o=[];for(let i=0;i<r;i++){let h=0,l=0,a=0;for(let c=0;c<n;c++)a=s.get(c,i)-e[i],h+=a,l+=a*a;t?o.push((l-h*h/n)/(n-1)):o.push((l-h*h/n)/n)}return o}function Ts(s,t,e){const n=s.rows,r=s.columns,o=n*r;let i=0,h=0,l=0;for(let a=0;a<n;a++)for(let c=0;c<r;c++)l=s.get(a,c)-e,i+=l,h+=l*l;return t?(h-i*i/o)/(o-1):(h-i*i/o)/o}function Rs(s,t){for(let e=0;e<s.rows;e++)for(let n=0;n<s.columns;n++)s.set(e,n,s.get(e,n)-t[e])}function Fs(s,t){for(let e=0;e<s.rows;e++)for(let n=0;n<s.columns;n++)s.set(e,n,s.get(e,n)-t[n])}function Is(s,t){for(let e=0;e<s.rows;e++)for(let n=0;n<s.columns;n++)s.set(e,n,s.get(e,n)-t)}function $s(s){const t=[];for(let e=0;e<s.rows;e++){let n=0;for(let r=0;r<s.columns;r++)n+=Math.pow(s.get(e,r),2)/(s.columns-1);t.push(Math.sqrt(n))}return t}function _s(s,t){for(let e=0;e<s.rows;e++)for(let n=0;n<s.columns;n++)s.set(e,n,s.get(e,n)/t[e])}function Ls(s){const t=[];for(let e=0;e<s.columns;e++){let n=0;for(let r=0;r<s.rows;r++)n+=Math.pow(s.get(r,e),2)/(s.rows-1);t.push(Math.sqrt(n))}return t}function Bs(s,t){for(let e=0;e<s.rows;e++)for(let n=0;n<s.columns;n++)s.set(e,n,s.get(e,n)/t[n])}function js(s){const t=s.size-1;let e=0;for(let n=0;n<s.columns;n++)for(let r=0;r<s.rows;r++)e+=Math.pow(s.get(r,n),2)/t;return Math.sqrt(e)}function qs(s,t){for(let e=0;e<s.rows;e++)for(let n=0;n<s.columns;n++)s.set(e,n,s.get(e,n)/t)}class R{static from1DArray(t,e,n){if(t*e!==n.length)throw new RangeError("data length does not match given dimensions");let r=new F(t,e);for(let o=0;o<t;o++)for(let i=0;i<e;i++)r.set(o,i,n[o*e+i]);return r}static rowVector(t){let e=new F(1,t.length);for(let n=0;n<t.length;n++)e.set(0,n,t[n]);return e}static columnVector(t){let e=new F(t.length,1);for(let n=0;n<t.length;n++)e.set(n,0,t[n]);return e}static zeros(t,e){return new F(t,e)}static ones(t,e){return new F(t,e).fill(1)}static rand(t,e,n={}){if(typeof n!="object")throw new TypeError("options must be an object");const{random:r=Math.random}=n;let o=new F(t,e);for(let i=0;i<t;i++)for(let h=0;h<e;h++)o.set(i,h,r());return o}static randInt(t,e,n={}){if(typeof n!="object")throw new TypeError("options must be an object");const{min:r=0,max:o=1e3,random:i=Math.random}=n;if(!Number.isInteger(r))throw new TypeError("min must be an integer");if(!Number.isInteger(o))throw new TypeError("max must be an integer");if(r>=o)throw new RangeError("min must be smaller than max");let h=o-r,l=new F(t,e);for(let a=0;a<t;a++)for(let c=0;c<e;c++){let u=r+Math.round(i()*h);l.set(a,c,u)}return l}static eye(t,e,n){e===void 0&&(e=t),n===void 0&&(n=1);let r=Math.min(t,e),o=this.zeros(t,e);for(let i=0;i<r;i++)o.set(i,i,n);return o}static diag(t,e,n){let r=t.length;e===void 0&&(e=r),n===void 0&&(n=e);let o=Math.min(r,e,n),i=this.zeros(e,n);for(let h=0;h<o;h++)i.set(h,h,t[h]);return i}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let n=t.rows,r=t.columns,o=new F(n,r);for(let i=0;i<n;i++)for(let h=0;h<r;h++)o.set(i,h,Math.min(t.get(i,h),e.get(i,h)));return o}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let n=t.rows,r=t.columns,o=new this(n,r);for(let i=0;i<n;i++)for(let h=0;h<r;h++)o.set(i,h,Math.max(t.get(i,h),e.get(i,h)));return o}static checkMatrix(t){return R.isMatrix(t)?t:new F(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 e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t.call(this,e,n);return this}to1DArray(){let t=[];for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t.push(this.get(e,n));return t}to2DArray(){let t=[];for(let e=0;e<this.rows;e++){t.push([]);for(let n=0;n<this.columns;n++)t[e].push(this.get(e,n))}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 e=0;e<=t;e++)if(this.get(t,e)!==this.get(e,t))return!1;return!0}return!1}isEchelonForm(){let t=0,e=0,n=-1,r=!0,o=!1;for(;t<this.rows&&r;){for(e=0,o=!1;e<this.columns&&o===!1;)this.get(t,e)===0?e++:this.get(t,e)===1&&e>n?(o=!0,n=e):(r=!1,o=!0);t++}return r}isReducedEchelonForm(){let t=0,e=0,n=-1,r=!0,o=!1;for(;t<this.rows&&r;){for(e=0,o=!1;e<this.columns&&o===!1;)this.get(t,e)===0?e++:this.get(t,e)===1&&e>n?(o=!0,n=e):(r=!1,o=!0);for(let i=e+1;i<this.rows;i++)this.get(t,i)!==0&&(r=!1);t++}return r}echelonForm(){let t=this.clone(),e=0,n=0;for(;e<t.rows&&n<t.columns;){let r=e;for(let o=e;o<t.rows;o++)t.get(o,n)>t.get(r,n)&&(r=o);if(t.get(r,n)===0)n++;else{t.swapRows(e,r);let o=t.get(e,n);for(let i=n;i<t.columns;i++)t.set(e,i,t.get(e,i)/o);for(let i=e+1;i<t.rows;i++){let h=t.get(i,n)/t.get(e,n);t.set(i,n,0);for(let l=n+1;l<t.columns;l++)t.set(i,l,t.get(i,l)-t.get(e,l)*h)}e++,n++}}return t}reducedEchelonForm(){let t=this.echelonForm(),e=t.columns,n=t.rows,r=n-1;for(;r>=0;)if(t.maxRow(r)===0)r--;else{let o=0,i=!1;for(;o<n&&i===!1;)t.get(r,o)===1?i=!0:o++;for(let h=0;h<r;h++){let l=t.get(h,o);for(let a=o;a<e;a++){let c=t.get(h,a)-l*t.get(r,a);t.set(h,a,c)}}r--}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:e=1,columns:n=1}=t;if(!Number.isInteger(e)||e<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(n)||n<=0)throw new TypeError("columns must be a positive integer");let r=new F(this.rows*e,this.columns*n);for(let o=0;o<e;o++)for(let i=0;i<n;i++)r.setSubMatrix(this,this.rows*o,this.columns*i);return r}fill(t){for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,t);return this}neg(){return this.mulS(-1)}getRow(t){lt(this,t);let e=[];for(let n=0;n<this.columns;n++)e.push(this.get(t,n));return e}getRowVector(t){return F.rowVector(this.getRow(t))}setRow(t,e){lt(this,t),e=Tt(this,e);for(let n=0;n<this.columns;n++)this.set(t,n,e[n]);return this}swapRows(t,e){lt(this,t),lt(this,e);for(let n=0;n<this.columns;n++){let r=this.get(t,n);this.set(t,n,this.get(e,n)),this.set(e,n,r)}return this}getColumn(t){ut(this,t);let e=[];for(let n=0;n<this.rows;n++)e.push(this.get(n,t));return e}getColumnVector(t){return F.columnVector(this.getColumn(t))}setColumn(t,e){ut(this,t),e=Rt(this,e);for(let n=0;n<this.rows;n++)this.set(n,t,e[n]);return this}swapColumns(t,e){ut(this,t),ut(this,e);for(let n=0;n<this.rows;n++){let r=this.get(n,t);this.set(n,t,this.get(n,e)),this.set(n,e,r)}return this}addRowVector(t){t=Tt(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)+t[n]);return this}subRowVector(t){t=Tt(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)-t[n]);return this}mulRowVector(t){t=Tt(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)*t[n]);return this}divRowVector(t){t=Tt(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)/t[n]);return this}addColumnVector(t){t=Rt(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)+t[e]);return this}subColumnVector(t){t=Rt(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)-t[e]);return this}mulColumnVector(t){t=Rt(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)*t[e]);return this}divColumnVector(t){t=Rt(this,t);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)this.set(e,n,this.get(e,n)/t[e]);return this}mulRow(t,e){lt(this,t);for(let n=0;n<this.columns;n++)this.set(t,n,this.get(t,n)*e);return this}mulColumn(t,e){ut(this,t);for(let n=0;n<this.rows;n++)this.set(n,t,this.get(n,t)*e);return this}max(t){if(this.isEmpty())return NaN;switch(t){case"row":{const e=new Array(this.rows).fill(Number.NEGATIVE_INFINITY);for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.get(n,r)>e[n]&&(e[n]=this.get(n,r));return e}case"column":{const e=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.get(n,r)>e[r]&&(e[r]=this.get(n,r));return e}case void 0:{let e=this.get(0,0);for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.get(n,r)>e&&(e=this.get(n,r));return e}default:throw new Error(`invalid option: ${t}`)}}maxIndex(){Ft(this);let t=this.get(0,0),e=[0,0];for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.get(n,r)>t&&(t=this.get(n,r),e[0]=n,e[1]=r);return e}min(t){if(this.isEmpty())return NaN;switch(t){case"row":{const e=new Array(this.rows).fill(Number.POSITIVE_INFINITY);for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.get(n,r)<e[n]&&(e[n]=this.get(n,r));return e}case"column":{const e=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.get(n,r)<e[r]&&(e[r]=this.get(n,r));return e}case void 0:{let e=this.get(0,0);for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.get(n,r)<e&&(e=this.get(n,r));return e}default:throw new Error(`invalid option: ${t}`)}}minIndex(){Ft(this);let t=this.get(0,0),e=[0,0];for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)this.get(n,r)<t&&(t=this.get(n,r),e[0]=n,e[1]=r);return e}maxRow(t){if(lt(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let n=1;n<this.columns;n++)this.get(t,n)>e&&(e=this.get(t,n));return e}maxRowIndex(t){lt(this,t),Ft(this);let e=this.get(t,0),n=[t,0];for(let r=1;r<this.columns;r++)this.get(t,r)>e&&(e=this.get(t,r),n[1]=r);return n}minRow(t){if(lt(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let n=1;n<this.columns;n++)this.get(t,n)<e&&(e=this.get(t,n));return e}minRowIndex(t){lt(this,t),Ft(this);let e=this.get(t,0),n=[t,0];for(let r=1;r<this.columns;r++)this.get(t,r)<e&&(e=this.get(t,r),n[1]=r);return n}maxColumn(t){if(ut(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let n=1;n<this.rows;n++)this.get(n,t)>e&&(e=this.get(n,t));return e}maxColumnIndex(t){ut(this,t),Ft(this);let e=this.get(0,t),n=[0,t];for(let r=1;r<this.rows;r++)this.get(r,t)>e&&(e=this.get(r,t),n[0]=r);return n}minColumn(t){if(ut(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let n=1;n<this.rows;n++)this.get(n,t)<e&&(e=this.get(n,t));return e}minColumnIndex(t){ut(this,t),Ft(this);let e=this.get(0,t),n=[0,t];for(let r=1;r<this.rows;r++)this.get(r,t)<e&&(e=this.get(r,t),n[0]=r);return n}diag(){let t=Math.min(this.rows,this.columns),e=[];for(let n=0;n<t;n++)e.push(this.get(n,n));return e}norm(t="frobenius"){let e=0;if(t==="max")return this.max();if(t==="frobenius"){for(let n=0;n<this.rows;n++)for(let r=0;r<this.columns;r++)e=e+this.get(n,r)*this.get(n,r);return Math.sqrt(e)}else throw new RangeError(`unknown norm type: ${t}`)}cumulativeSum(){let t=0;for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t+=this.get(e,n),this.set(e,n,t);return this}dot(t){R.isMatrix(t)&&(t=t.to1DArray());let e=this.to1DArray();if(e.length!==t.length)throw new RangeError("vectors do not have the same size");let n=0;for(let r=0;r<e.length;r++)n+=e[r]*t[r];return n}mmul(t){t=F.checkMatrix(t);let e=this.rows,n=this.columns,r=t.columns,o=new F(e,r),i=new Float64Array(n);for(let h=0;h<r;h++){for(let l=0;l<n;l++)i[l]=t.get(l,h);for(let l=0;l<e;l++){let a=0;for(let c=0;c<n;c++)a+=this.get(l,c)*i[c];o.set(l,h,a)}}return o}strassen2x2(t){t=F.checkMatrix(t);let e=new F(2,2);const n=this.get(0,0),r=t.get(0,0),o=this.get(0,1),i=t.get(0,1),h=this.get(1,0),l=t.get(1,0),a=this.get(1,1),c=t.get(1,1),u=(n+a)*(r+c),f=(h+a)*r,p=n*(i-c),g=a*(l-r),w=(n+o)*c,P=(h-n)*(r+i),m=(o-a)*(l+c),d=u+g-w+m,M=p+w,x=f+g,v=u-f+p+P;return e.set(0,0,d),e.set(0,1,M),e.set(1,0,x),e.set(1,1,v),e}strassen3x3(t){t=F.checkMatrix(t);let e=new F(3,3);const n=this.get(0,0),r=this.get(0,1),o=this.get(0,2),i=this.get(1,0),h=this.get(1,1),l=this.get(1,2),a=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),M=t.get(2,1),x=t.get(2,2),v=(n+r+o-i-h-c-u)*P,k=(n-i)*(-p+P),A=h*(-f+p+w-P-m-d+x),b=(-n+i+h)*(f-p+P),I=(i+h)*(-f+p),O=n*f,D=(-n+a+c)*(f-g+m),nt=(-n+a)*(g-m),L=(a+c)*(-f+g),C=(n+r+o-h-l-a-c)*m,S=c*(-f+g+w-P-m-d+M),E=(-o+c+u)*(P+d-M),yt=(o-u)*(P-M),_=o*d,j=(c+u)*(-d+M),$=(-o+h+l)*(m+d-x),U=(o-l)*(m-x),X=(h+l)*(-d+x),Y=r*w,st=l*M,pt=i*g,ft=a*p,bt=u*x,je=O+_+Y,qe=v+b+I+O+E+_+j,Oe=O+D+L+C+_+$+X,Ne=k+A+b+O+_+$+U,ae=k+b+I+O+st,le=_+$+U+X+pt,ue=O+D+nt+S+E+yt+_,ce=E+yt+_+j+ft,fe=O+D+nt+L+bt;return e.set(0,0,je),e.set(0,1,qe),e.set(0,2,Oe),e.set(1,0,Ne),e.set(1,1,ae),e.set(1,2,le),e.set(2,0,ue),e.set(2,1,ce),e.set(2,2,fe),e}mmulStrassen(t){t=F.checkMatrix(t);let e=this.clone(),n=e.rows,r=e.columns,o=t.rows,i=t.columns;r!==o&&console.warn(`Multiplying ${n} x ${r} and ${o} x ${i} matrix: dimensions do not match.`);function h(u,f,p){let g=u.rows,w=u.columns;if(g===f&&w===p)return u;{let P=R.zeros(f,p);return P=P.setSubMatrix(u,0,0),P}}let l=Math.max(n,o),a=Math.max(r,i);e=h(e,l,a),t=h(t,l,a);function c(u,f,p,g){if(p<=512||g<=512)return u.mmul(f);p%2===1&&g%2===1?(u=h(u,p+1,g+1),f=h(f,p+1,g+1)):p%2===1?(u=h(u,p+1,g),f=h(f,p+1,g)):g%2===1&&(u=h(u,p,g+1),f=h(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),M=u.subMatrix(0,w-1,P,u.columns-1),x=f.subMatrix(0,w-1,P,f.columns-1),v=u.subMatrix(w,u.rows-1,0,P-1),k=f.subMatrix(w,f.rows-1,0,P-1),A=u.subMatrix(w,u.rows-1,P,u.columns-1),b=f.subMatrix(w,f.rows-1,P,f.columns-1),I=c(R.add(m,A),R.add(d,b),w,P),O=c(R.add(v,A),d,w,P),D=c(m,R.sub(x,b),w,P),nt=c(A,R.sub(k,d),w,P),L=c(R.add(m,M),b,w,P),C=c(R.sub(v,m),R.add(d,x),w,P),S=c(R.sub(M,A),R.add(k,b),w,P),E=R.add(I,nt);E.sub(L),E.add(S);let yt=R.add(D,L),_=R.add(O,nt),j=R.sub(I,O);j.add(D),j.add(C);let $=R.zeros(2*E.rows,2*E.columns);return $=$.setSubMatrix(E,0,0),$=$.setSubMatrix(yt,E.rows,0),$=$.setSubMatrix(_,0,E.columns),$=$.setSubMatrix(j,E.rows,E.columns),$.subMatrix(0,p-1,0,g-1)}return c(e,t,l,a)}scaleRows(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{min:e=0,max:n=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(n))throw new TypeError("max must be a number");if(e>=n)throw new RangeError("min must be smaller than max");let r=new F(this.rows,this.columns);for(let o=0;o<this.rows;o++){const i=this.getRow(o);i.length>0&&Je(i,{min:e,max:n,output:i}),r.setRow(o,i)}return r}scaleColumns(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{min:e=0,max:n=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(n))throw new TypeError("max must be a number");if(e>=n)throw new RangeError("min must be smaller than max");let r=new F(this.rows,this.columns);for(let o=0;o<this.columns;o++){const i=this.getColumn(o);i.length&&Je(i,{min:e,max:n,output:i}),r.setColumn(o,i)}return r}flipRows(){const t=Math.ceil(this.columns/2);for(let e=0;e<this.rows;e++)for(let n=0;n<t;n++){let r=this.get(e,n),o=this.get(e,this.columns-1-n);this.set(e,n,o),this.set(e,this.columns-1-n,r)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let e=0;e<this.columns;e++)for(let n=0;n<t;n++){let r=this.get(n,e),o=this.get(this.rows-1-n,e);this.set(n,e,o),this.set(this.rows-1-n,e,r)}return this}kroneckerProduct(t){t=F.checkMatrix(t);let e=this.rows,n=this.columns,r=t.rows,o=t.columns,i=new F(e*r,n*o);for(let h=0;h<e;h++)for(let l=0;l<n;l++)for(let a=0;a<r;a++)for(let c=0;c<o;c++)i.set(r*h+a,o*l+c,this.get(h,l)*t.get(a,c));return i}kroneckerSum(t){if(t=F.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let e=this.rows,n=t.rows,r=this.kroneckerProduct(F.eye(n,n)),o=F.eye(e,e).kroneckerProduct(t);return r.add(o)}transpose(){let t=new F(this.columns,this.rows);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t.set(n,e,this.get(e,n));return t}sortRows(t=tn){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=tn){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,n,r){Qe(this,t,e,n,r);let o=new F(e-t+1,r-n+1);for(let i=t;i<=e;i++)for(let h=n;h<=r;h++)o.set(i-t,h-n,this.get(i,h));return o}subMatrixRow(t,e,n){if(e===void 0&&(e=0),n===void 0&&(n=this.columns-1),e>n||e<0||e>=this.columns||n<0||n>=this.columns)throw new RangeError("Argument out of range");let r=new F(t.length,n-e+1);for(let o=0;o<t.length;o++)for(let i=e;i<=n;i++){if(t[o]<0||t[o]>=this.rows)throw new RangeError(`Row index out of range: ${t[o]}`);r.set(o,i-e,this.get(t[o],i))}return r}subMatrixColumn(t,e,n){if(e===void 0&&(e=0),n===void 0&&(n=this.rows-1),e>n||e<0||e>=this.rows||n<0||n>=this.rows)throw new RangeError("Argument out of range");let r=new F(n-e+1,t.length);for(let o=0;o<t.length;o++)for(let i=e;i<=n;i++){if(t[o]<0||t[o]>=this.columns)throw new RangeError(`Column index out of range: ${t[o]}`);r.set(i-e,o,this.get(i,t[o]))}return r}setSubMatrix(t,e,n){if(t=F.checkMatrix(t),t.isEmpty())return this;let r=e+t.rows-1,o=n+t.columns-1;Qe(this,e,r,n,o);for(let i=0;i<t.rows;i++)for(let h=0;h<t.columns;h++)this.set(e+i,n+h,t.get(i,h));return this}selection(t,e){Ms(this,t),ys(this,e);let n=new F(t.length,e.length);for(let r=0;r<t.length;r++){let o=t[r];for(let i=0;i<e.length;i++){let h=e[i];n.set(r,i,this.get(o,h))}}return n}trace(){let t=Math.min(this.rows,this.columns),e=0;for(let n=0;n<t;n++)e+=this.get(n,n);return e}clone(){let t=new F(this.rows,this.columns);for(let e=0;e<this.rows;e++)for(let n=0;n<this.columns;n++)t.set(e,n,this.get(e,n));return t}sum(t){switch(t){case"row":return bs(this);case"column":return xs(this);case void 0:return vs(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return As(this);case"column":return Es(this);case void 0:return Ss(this);default:throw new Error(`invalid option: ${t}`)}}mean(t){const e=this.sum(t);switch(t){case"row":{for(let n=0;n<this.rows;n++)e[n]/=this.columns;return e}case"column":{for(let n=0;n<this.columns;n++)e[n]/=this.rows;return e}case void 0:return e/this.size;default:throw new Error(`invalid option: ${t}`)}}variance(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");const{unbiased:n=!0,mean:r=this.mean(t)}=e;if(typeof n!="boolean")throw new TypeError("unbiased must be a boolean");switch(t){case"row":{if(!ot(r))throw new TypeError("mean must be an array");return ks(this,n,r)}case"column":{if(!ot(r))throw new TypeError("mean must be an array");return Cs(this,n,r)}case void 0:{if(typeof r!="number")throw new TypeError("mean must be a number");return Ts(this,n,r)}default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,e){typeof t=="object"&&(e=t,t=void 0);const n=this.variance(t,e);if(t===void 0)return Math.sqrt(n);for(let r=0;r<n.length;r++)n[r]=Math.sqrt(n[r]);return n}center(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");const{center:n=this.mean(t)}=e;switch(t){case"row":{if(!ot(n))throw new TypeError("center must be an array");return Rs(this,n),this}case"column":{if(!ot(n))throw new TypeError("center must be an array");return Fs(this,n),this}case void 0:{if(typeof n!="number")throw new TypeError("center must be a number");return Is(this,n),this}default:throw new Error(`invalid option: ${t}`)}}scale(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");let n=e.scale;switch(t){case"row":{if(n===void 0)n=$s(this);else if(!ot(n))throw new TypeError("scale must be an array");return _s(this,n),this}case"column":{if(n===void 0)n=Ls(this);else if(!ot(n))throw new TypeError("scale must be an array");return Bs(this,n),this}case void 0:{if(n===void 0)n=js(this);else if(typeof n!="number")throw new TypeError("scale must be a number");return qs(this,n),this}default:throw new Error(`invalid option: ${t}`)}}toString(t){return Ge(this,t)}}R.prototype.klass="Matrix",typeof Symbol<"u"&&(R.prototype[Symbol.for("nodejs.util.inspect.custom")]=ps);function tn(s,t){return s-t}function Os(s){return s.every(t=>typeof t=="number")}R.random=R.rand,R.randomInt=R.randInt,R.diagonal=R.diag,R.prototype.diagonal=R.prototype.diag,R.identity=R.eye,R.prototype.negate=R.prototype.neg,R.prototype.tensorProduct=R.prototype.kroneckerProduct;let F=class ns extends R{constructor(t,e){if(super(),ns.isMatrix(t))return t.clone();if(Number.isInteger(t)&&t>=0)if(this.data=[],Number.isInteger(e)&&e>=0)for(let n=0;n<t;n++)this.data.push(new Float64Array(e));else throw new TypeError("nColumns must be a positive integer");else if(ot(t)){const n=t;if(t=n.length,e=t?n[0].length:0,typeof e!="number")throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let r=0;r<t;r++){if(n[r].length!==e)throw new RangeError("Inconsistent array dimensions");if(!Os(n[r]))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(n[r]))}}else throw new TypeError("First argument must be a positive number or an array");this.rows=t,this.columns=e}set(t,e,n){return this.data[t][e]=n,this}get(t,e){return this.data[t][e]}removeRow(t){return lt(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return e===void 0&&(e=t,t=this.rows),lt(this,t,!0),e=Float64Array.from(Tt(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){ut(this,t);for(let e=0;e<this.rows;e++){const n=new Float64Array(this.columns-1);for(let r=0;r<t;r++)n[r]=this.data[e][r];for(let r=t+1;r<this.columns;r++)n[r-1]=this.data[e][r];this.data[e]=n}return this.columns-=1,this}addColumn(t,e){typeof e>"u"&&(e=t,t=this.columns),ut(this,t,!0),e=Rt(this,e);for(let n=0;n<this.rows;n++){const r=new Float64Array(this.columns+1);let o=0;for(;o<t;o++)r[o]=this.data[n][o];for(r[o++]=e[n];o<this.columns+1;o++)r[o]=this.data[n][o-1];this.data[n]=r}return this.columns+=1,this}};Ps(R,F);class Ns extends R{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,e,n){return this.data[t][e]=n,this}get(t,e){return this.data[t][e]}}function en(s,t){let e=0;return Math.abs(s)>Math.abs(t)?(e=t/s,Math.abs(s)*Math.sqrt(1+e*e)):t!==0?(e=s/t,Math.abs(t)*Math.sqrt(1+e*e)):0}class Vs{constructor(t,e={}){const{assumeSymmetric:n=!1}=e;if(t=Ns.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 r=t.columns,o=new F(r,r),i=new Float64Array(r),h=new Float64Array(r),l=t,a,c,u=!1;if(n?u=!0:u=t.isSymmetric(),u){for(a=0;a<r;a++)for(c=0;c<r;c++)o.set(a,c,l.get(a,c));zs(r,h,i,o),Us(r,h,i,o)}else{let f=new F(r,r),p=new Float64Array(r);for(c=0;c<r;c++)for(a=0;a<r;a++)f.set(a,c,l.get(a,c));Ys(r,f,p,o),Ds(r,h,i,o,f)}this.n=r,this.e=h,this.d=i,this.V=o}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,e=this.e,n=this.d,r=new F(t,t),o,i;for(o=0;o<t;o++){for(i=0;i<t;i++)r.set(o,i,0);r.set(o,o,n[o]),e[o]>0?r.set(o,o+1,e[o]):e[o]<0&&r.set(o,o-1,e[o])}return r}}function zs(s,t,e,n){let r,o,i,h,l,a,c,u;for(l=0;l<s;l++)e[l]=n.get(s-1,l);for(h=s-1;h>0;h--){for(u=0,i=0,a=0;a<h;a++)u=u+Math.abs(e[a]);if(u===0)for(t[h]=e[h-1],l=0;l<h;l++)e[l]=n.get(h-1,l),n.set(h,l,0),n.set(l,h,0);else{for(a=0;a<h;a++)e[a]/=u,i+=e[a]*e[a];for(r=e[h-1],o=Math.sqrt(i),r>0&&(o=-o),t[h]=u*o,i=i-r*o,e[h-1]=r-o,l=0;l<h;l++)t[l]=0;for(l=0;l<h;l++){for(r=e[l],n.set(l,h,r),o=t[l]+n.get(l,l)*r,a=l+1;a<=h-1;a++)o+=n.get(a,l)*e[a],t[a]+=n.get(a,l)*r;t[l]=o}for(r=0,l=0;l<h;l++)t[l]/=i,r+=t[l]*e[l];for(c=r/(i+i),l=0;l<h;l++)t[l]-=c*e[l];for(l=0;l<h;l++){for(r=e[l],o=t[l],a=l;a<=h-1;a++)n.set(a,l,n.get(a,l)-(r*t[a]+o*e[a]));e[l]=n.get(h-1,l),n.set(h,l,0)}}e[h]=i}for(h=0;h<s-1;h++){if(n.set(s-1,h,n.get(h,h)),n.set(h,h,1),i=e[h+1],i!==0){for(a=0;a<=h;a++)e[a]=n.get(a,h+1)/i;for(l=0;l<=h;l++){for(o=0,a=0;a<=h;a++)o+=n.get(a,h+1)*n.get(a,l);for(a=0;a<=h;a++)n.set(a,l,n.get(a,l)-o*e[a])}}for(a=0;a<=h;a++)n.set(a,h+1,0)}for(l=0;l<s;l++)e[l]=n.get(s-1,l),n.set(s-1,l,0);n.set(s-1,s-1,1),t[0]=0}function Us(s,t,e,n){let r,o,i,h,l,a,c,u,f,p,g,w,P,m,d,M;for(i=1;i<s;i++)t[i-1]=t[i];t[s-1]=0;let x=0,v=0,k=Number.EPSILON;for(a=0;a<s;a++){for(v=Math.max(v,Math.abs(e[a])+Math.abs(t[a])),c=a;c<s&&!(Math.abs(t[c])<=k*v);)c++;if(c>a)do{for(r=e[a],u=(e[a+1]-r)/(2*t[a]),f=en(u,1),u<0&&(f=-f),e[a]=t[a]/(u+f),e[a+1]=t[a]*(u+f),p=e[a+1],o=r-e[a],i=a+2;i<s;i++)e[i]-=o;for(x=x+o,u=e[c],g=1,w=g,P=g,m=t[a+1],d=0,M=0,i=c-1;i>=a;i--)for(P=w,w=g,M=d,r=g*t[i],o=g*u,f=en(u,t[i]),t[i+1]=d*f,d=t[i]/f,g=u/f,u=g*e[i]-d*r,e[i+1]=o+d*(g*r+d*e[i]),l=0;l<s;l++)o=n.get(l,i+1),n.set(l,i+1,d*n.get(l,i)+g*o),n.set(l,i,g*n.get(l,i)-d*o);u=-d*M*P*m*t[a]/p,t[a]=d*u,e[a]=g*u}while(Math.abs(t[a])>k*v);e[a]=e[a]+x,t[a]=0}for(i=0;i<s-1;i++){for(l=i,u=e[i],h=i+1;h<s;h++)e[h]<u&&(l=h,u=e[h]);if(l!==i)for(e[l]=e[i],e[i]=u,h=0;h<s;h++)u=n.get(h,i),n.set(h,i,n.get(h,l)),n.set(h,l,u)}}function Ys(s,t,e,n){let r=0,o=s-1,i,h,l,a,c,u,f;for(u=r+1;u<=o-1;u++){for(f=0,a=u;a<=o;a++)f=f+Math.abs(t.get(a,u-1));if(f!==0){for(l=0,a=o;a>=u;a--)e[a]=t.get(a,u-1)/f,l+=e[a]*e[a];for(h=Math.sqrt(l),e[u]>0&&(h=-h),l=l-e[u]*h,e[u]=e[u]-h,c=u;c<s;c++){for(i=0,a=o;a>=u;a--)i+=e[a]*t.get(a,c);for(i=i/l,a=u;a<=o;a++)t.set(a,c,t.get(a,c)-i*e[a])}for(a=0;a<=o;a++){for(i=0,c=o;c>=u;c--)i+=e[c]*t.get(a,c);for(i=i/l,c=u;c<=o;c++)t.set(a,c,t.get(a,c)-i*e[c])}e[u]=f*e[u],t.set(u,u-1,f*h)}}for(a=0;a<s;a++)for(c=0;c<s;c++)n.set(a,c,a===c?1:0);for(u=o-1;u>=r+1;u--)if(t.get(u,u-1)!==0){for(a=u+1;a<=o;a++)e[a]=t.get(a,u-1);for(c=u;c<=o;c++){for(h=0,a=u;a<=o;a++)h+=e[a]*n.get(a,c);for(h=h/e[u]/t.get(u,u-1),a=u;a<=o;a++)n.set(a,c,n.get(a,c)+h*e[a])}}}function Ds(s,t,e,n,r){let o=s-1,i=0,h=s-1,l=Number.EPSILON,a=0,c=0,u=0,f=0,p=0,g=0,w=0,P=0,m,d,M,x,v,k,A,b,I,O,D,nt,L,C,S;for(m=0;m<s;m++)for((m<i||m>h)&&(e[m]=r.get(m,m),t[m]=0),d=Math.max(m-1,0);d<s;d++)c=c+Math.abs(r.get(m,d));for(;o>=i;){for(x=o;x>i&&(g=Math.abs(r.get(x-1,x-1))+Math.abs(r.get(x,x)),g===0&&(g=c),!(Math.abs(r.get(x,x-1))<l*g));)x--;if(x===o)r.set(o,o,r.get(o,o)+a),e[o]=r.get(o,o),t[o]=0,o--,P=0;else if(x===o-1){if(A=r.get(o,o-1)*r.get(o-1,o),u=(r.get(o-1,o-1)-r.get(o,o))/2,f=u*u+A,w=Math.sqrt(Math.abs(f)),r.set(o,o,r.get(o,o)+a),r.set(o-1,o-1,r.get(o-1,o-1)+a),b=r.get(o,o),f>=0){for(w=u>=0?u+w:u-w,e[o-1]=b+w,e[o]=e[o-1],w!==0&&(e[o]=b-A/w),t[o-1]=0,t[o]=0,b=r.get(o,o-1),g=Math.abs(b)+Math.abs(w),u=b/g,f=w/g,p=Math.sqrt(u*u+f*f),u=u/p,f=f/p,d=o-1;d<s;d++)w=r.get(o-1,d),r.set(o-1,d,f*w+u*r.get(o,d)),r.set(o,d,f*r.get(o,d)-u*w);for(m=0;m<=o;m++)w=r.get(m,o-1),r.set(m,o-1,f*w+u*r.get(m,o)),r.set(m,o,f*r.get(m,o)-u*w);for(m=i;m<=h;m++)w=n.get(m,o-1),n.set(m,o-1,f*w+u*n.get(m,o)),n.set(m,o,f*n.get(m,o)-u*w)}else e[o-1]=b+u,e[o]=b+u,t[o-1]=w,t[o]=-w;o=o-2,P=0}else{if(b=r.get(o,o),I=0,A=0,x<o&&(I=r.get(o-1,o-1),A=r.get(o,o-1)*r.get(o-1,o)),P===10){for(a+=b,m=i;m<=o;m++)r.set(m,m,r.get(m,m)-b);g=Math.abs(r.get(o,o-1))+Math.abs(r.get(o-1,o-2)),b=I=.75*g,A=-.4375*g*g}if(P===30&&(g=(I-b)/2,g=g*g+A,g>0)){for(g=Math.sqrt(g),I<b&&(g=-g),g=b-A/((I-b)/2+g),m=i;m<=o;m++)r.set(m,m,r.get(m,m)-g);a+=g,b=I=A=.964}for(P=P+1,v=o-2;v>=x&&(w=r.get(v,v),p=b-w,g=I-w,u=(p*g-A)/r.get(v+1,v)+r.get(v,v+1),f=r.get(v+1,v+1)-w-p-g,p=r.get(v+2,v+1),g=Math.abs(u)+Math.abs(f)+Math.abs(p),u=u/g,f=f/g,p=p/g,!(v===x||Math.abs(r.get(v,v-1))*(Math.abs(f)+Math.abs(p))<l*(Math.abs(u)*(Math.abs(r.get(v-1,v-1))+Math.abs(w)+Math.abs(r.get(v+1,v+1))))));)v--;for(m=v+2;m<=o;m++)r.set(m,m-2,0),m>v+2&&r.set(m,m-3,0);for(M=v;M<=o-1&&(C=M!==o-1,M!==v&&(u=r.get(M,M-1),f=r.get(M+1,M-1),p=C?r.get(M+2,M-1):0,b=Math.abs(u)+Math.abs(f)+Math.abs(p),b!==0&&(u=u/b,f=f/b,p=p/b)),b!==0);M++)if(g=Math.sqrt(u*u+f*f+p*p),u<0&&(g=-g),g!==0){for(M!==v?r.set(M,M-1,-g*b):x!==v&&r.set(M,M-1,-r.get(M,M-1)),u=u+g,b=u/g,I=f/g,w=p/g,f=f/u,p=p/u,d=M;d<s;d++)u=r.get(M,d)+f*r.get(M+1,d),C&&(u=u+p*r.get(M+2,d),r.set(M+2,d,r.get(M+2,d)-u*w)),r.set(M,d,r.get(M,d)-u*b),r.set(M+1,d,r.get(M+1,d)-u*I);for(m=0;m<=Math.min(o,M+3);m++)u=b*r.get(m,M)+I*r.get(m,M+1),C&&(u=u+w*r.get(m,M+2),r.set(m,M+2,r.get(m,M+2)-u*p)),r.set(m,M,r.get(m,M)-u),r.set(m,M+1,r.get(m,M+1)-u*f);for(m=i;m<=h;m++)u=b*n.get(m,M)+I*n.get(m,M+1),C&&(u=u+w*n.get(m,M+2),n.set(m,M+2,n.get(m,M+2)-u*p)),n.set(m,M,n.get(m,M)-u),n.set(m,M+1,n.get(m,M+1)-u*f)}}}if(c!==0){for(o=s-1;o>=0;o--)if(u=e[o],f=t[o],f===0)for(x=o,r.set(o,o,1),m=o-1;m>=0;m--){for(A=r.get(m,m)-u,p=0,d=x;d<=o;d++)p=p+r.get(m,d)*r.get(d,o);if(t[m]<0)w=A,g=p;else if(x=m,t[m]===0?r.set(m,o,A!==0?-p/A:-p/(l*c)):(b=r.get(m,m+1),I=r.get(m+1,m),f=(e[m]-u)*(e[m]-u)+t[m]*t[m],k=(b*g-w*p)/f,r.set(m,o,k),r.set(m+1,o,Math.abs(b)>Math.abs(w)?(-p-A*k)/b:(-g-I*k)/w)),k=Math.abs(r.get(m,o)),l*k*k>1)for(d=m;d<=o;d++)r.set(d,o,r.get(d,o)/k)}else if(f<0)for(x=o-1,Math.abs(r.get(o,o-1))>Math.abs(r.get(o-1,o))?(r.set(o-1,o-1,f/r.get(o,o-1)),r.set(o-1,o,-(r.get(o,o)-u)/r.get(o,o-1))):(S=te(0,-r.get(o-1,o),r.get(o-1,o-1)-u,f),r.set(o-1,o-1,S[0]),r.set(o-1,o,S[1])),r.set(o,o-1,0),r.set(o,o,1),m=o-2;m>=0;m--){for(O=0,D=0,d=x;d<=o;d++)O=O+r.get(m,d)*r.get(d,o-1),D=D+r.get(m,d)*r.get(d,o);if(A=r.get(m,m)-u,t[m]<0)w=A,p=O,g=D;else if(x=m,t[m]===0?(S=te(-O,-D,A,f),r.set(m,o-1,S[0]),r.set(m,o,S[1])):(b=r.get(m,m+1),I=r.get(m+1,m),nt=(e[m]-u)*(e[m]-u)+t[m]*t[m]-f*f,L=(e[m]-u)*2*f,nt===0&&L===0&&(nt=l*c*(Math.abs(A)+Math.abs(f)+Math.abs(b)+Math.abs(I)+Math.abs(w))),S=te(b*p-w*O+f*D,b*g-w*D-f*O,nt,L),r.set(m,o-1,S[0]),r.set(m,o,S[1]),Math.abs(b)>Math.abs(w)+Math.abs(f)?(r.set(m+1,o-1,(-O-A*r.get(m,o-1)+f*r.get(m,o))/b),r.set(m+1,o,(-D-A*r.get(m,o)-f*r.get(m,o-1))/b)):(S=te(-p-I*r.get(m,o-1),-g-I*r.get(m,o),w,f),r.set(m+1,o-1,S[0]),r.set(m+1,o,S[1]))),k=Math.max(Math.abs(r.get(m,o-1)),Math.abs(r.get(m,o))),l*k*k>1)for(d=m;d<=o;d++)r.set(d,o-1,r.get(d,o-1)/k),r.set(d,o,r.get(d,o)/k)}for(m=0;m<s;m++)if(m<i||m>h)for(d=m;d<s;d++)n.set(m,d,r.get(m,d));for(d=s-1;d>=i;d--)for(m=i;m<=h;m++){for(w=0,M=i;M<=Math.min(d,h);M++)w=w+n.get(m,M)*r.get(M,d);n.set(m,d,w)}}}function te(s,t,e,n){let r,o;return Math.abs(e)>Math.abs(n)?(r=n/e,o=e+r*n,[(s+r*t)/o,(t-r*s)/o]):(r=e/n,o=n+r*e,[(r*s+t)/o,(r*t-s)/o])}function ee(s,t,e){const n=s/e,r=t/e;if(!(isFinite(n)&&isFinite(r))){const l=-s/t;return isFinite(l)?[l]:s==0&&t==0?[0]:[]}const o=r*r-4*n;let i=0;if(isFinite(o)){if(o<0)return[];if(o==0)return[-.5*r];i=-.5*(r+It(Math.sqrt(o),r))}else i=-r;const h=n/i;return isFinite(h)?h>i?[i,h]:[h,i]:[i]}function nn(s,t,e,n){const r=e/(3*n),o=t/(3*n),i=s/n;if(!(isFinite(i)&&isFinite(o)&&isFinite(r)))return ee(s,t,e);const h=-r*r+o,l=-o*r+i,a=r*i-o*o,c=4*h*a-l*l,u=-2*r*h+l;if(c<0){const f=Math.sqrt(-.25*c),p=-.5*u;return[Math.cbrt(p+f)+Math.cbrt(p-f)-r]}else if(c==0){const f=It(Math.sqrt(-h),u);return[f-r,-2*f-r]}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(-h);return[m*p-r,m*w-r,m*P-r]}}function Xs(s,t,e,n,r){if(r==0)return nn(s,t,e,n);const o=n/r,i=e/r,h=t/r,l=s/r;let a=sn(o,i,h,l,!1);if(a!==null)return a;const c=716e74;for(let u=0;u<2;u++)if(a=sn(o/c,i/(c*c),h/(c*c*c),l/(c*c*c*c),u!=0),a!==null){for(let f=0;f<a.length;f++)a[f]*=c;return a}return[]}function St(s,t){return t==0?Math.abs(s):Math.abs((s-t)/t)}function sn(s,t,e,n,r){const o=Hs(s,t,e,n,r);if(o!==null&&o.length==4){let i=[];for(let h=0;h<2;h++){const l=o[h*2],a=o[h*2+1];i=i.concat(ee(a,l,1))}return i}return null}function Hs(s,t,e,n,r){function o(_,j,$,U){const X=St(_+$,s),Y=St(j+_*$+U,t),st=St(j*$+_*U,e);return X+Y+st}function i(_,j,$,U){return o(_,j,$,U)+St(j*U,n)}const h=9*s*s-24*t,l=h>=0?-2*t/(3*s+It(Math.sqrt(h),s)):-.25*s,a=s+4*l,c=t+3*l*(s+2*l),u=e+l*(2*t+l*(3*s+4*l)),f=n+l*(e+l*(t+l*(s+l)));let p=0,g=0;const w=349e100;if(r){const _=a/w,j=c/w,$=u/w,U=f/w;p=_*$-4/w*U-1/3*j*j,g=(_*$-8/w*U-2/9*j*j)*(1/3)*j-$*($/w)-_*_*U}else p=a*u-4*f-1/3*c*c,g=(a*u+8*f-2/9*c*c)*(1/3)*c-u*u-a*a*f;if(!isFinite(p)&&isFinite(g))return null;let P=Ws(p,g);r&&(P*=w);const m=s*.5,d=1/6*t+.5*P,M=e-s*d,x=2/3*t-P-m*m,v=.5*M/x,k=2*(n-d*d)/M,A=.5*M/k;let b=0,I=0,O=0;for(let _=0;_<3;_++){const j=_==1?A:x,$=_==0?v:k,U=St(j+m*m+2*d,t),X=St(2*(j*$+m*d),e),Y=St(j*$*$+d*d,n),st=U+X+Y;(_==0||st<O)&&(b=j,I=$,O=st)}const D=b,nt=I;let L=0,C=0,S=0,E=0;if(D<0){const _=Math.sqrt(-D);if(L=m+_,C=d+_*nt,S=m-_,E=d-_*nt,Math.abs(E)<Math.abs(C)?E=n/C:Math.abs(E)>Math.abs(C)&&(C=n/E),Math.abs(L)!=Math.abs(S)){let j=null,$=null;if(Math.abs(L)<Math.abs(S)){const X=(e-C*S)/E,Y=(t-E-C)/S;j=[s-S,X,Y],$=[S,S,S]}else{const X=(e-L*E)/C,Y=(t-E-C)/L,st=s-L;j=[L,L,L],$=[st,X,Y]}let U=0;for(let X=0;X<3;X++){const Y=j[X],st=$[X];if(isFinite(Y)&&isFinite(st)){const pt=o(Y,C,st,E);(X==0||pt<U)&&(L=Y,S=st,U=pt)}}}}else if(D==0){const _=n-d*d;L=m,C=d+Math.sqrt(-_),S=m,E=d-Math.sqrt(-_),Math.abs(C)>Math.abs(E)?E=n/C:Math.abs(E)>Math.abs(C)&&(C=n/E)}else return[];let yt=i(L,C,S,E);for(let _=0;_<8&&yt!=0;_++){const j=C*E-n,$=C*S+L*E-e,U=C+L*S+E-t,X=L+S-s,Y=L-S,st=C*C-C*(S*Y+2*E)+E*(L*Y+E);if(st==0)break;const pt=1/st,ft=E-C,bt=C*S-L*E,je=Y*j+ft*$+bt*U-(C*ft+L*bt)*X,qe=(L*Y+ft)*j-C*(Y*$+ft*U+bt*X),Oe=-Y*j-ft*$-bt*U+(S*bt+E*ft)*X,Ne=-(S*Y+ft)*j+E*(Y*$+ft*U+bt*X),ae=L-pt*je,le=C-pt*qe,ue=S-pt*Oe,ce=E-pt*Ne,fe=i(ae,le,ue,ce);if(fe<yt)L=ae,C=le,S=ue,E=ce,yt=fe;else break}return[L,C,S,E]}function Ws(s,t){const e=-.3333333333333333*s,n=.5*t;let r,o=null;if((Math.abs(e)>=1e102||Math.abs(n)>=1e164)&&(Math.abs(e)<Math.abs(n)?o=1-e*(e/n)*(e/n):o=Math.sign(e)*(n/e*(n/e)/e-1)),o!==null&&n==0)s>0?r=0:r=Math.sqrt(-s);else if(o!==null?o<0:n*n<e*e*e){const a=o!==null?n/e/Math.sqrt(e):n/Math.sqrt(e*e*e);r=-2*Math.sqrt(e)*It(Math.cos(Math.acos(Math.abs(a))*(1/3)),a)}else{let a;o!==null?Math.abs(e)<Math.abs(n)?a=-n*(1+Math.sqrt(o)):a=-n-It(Math.sqrt(Math.abs(e))*e*Math.sqrt(o),n):a=Math.cbrt(-n-It(Math.sqrt(n*n-e*e*e),n));const c=a==0?0:e/a;r=a+c}let i=r,h=(i*i+s)*i+t;const l=222045e-21;if(Math.abs(h)<l*Math.max(i*i*i,s*i,t))return i;for(let a=0;a<8;a++){const c=3*i*i+s;if(c==0)break;const u=i-h/c,f=(u*u+s)*u+t;if(f==0)return u;if(Math.abs(f)>=Math.abs(h))break;i=u,h=f}return i}function It(s,t){const e=Math.abs(s);return t<0?-e:e}function de(s,t=1e-9){if(s[s.length-1]==0)return de(s.slice(0,-1),t);const e=new F([s.slice(0,-1).reverse().map(r=>-r/s[s.length-1]),...Array.from({length:s.length-2},(r,o)=>{const i=new Array(s.length-1).fill(0);return i[o]=1,i})]),n=new Vs(e);return n.realEigenvalues.filter((r,o)=>Math.abs(n.imaginaryEigenvalues[o])<t)}const Js=(s,t,e)=>[s[0]*(1-e)+t[0]*e,s[1]*(1-e)+t[1]*e];function Zs(s){const t=[];for(let e=0;e<s.length-1;e++)t.push([s[e],s[e+1]]);return t}function Pe(s,t){const e=Zs(s).map(([n,r])=>Js(n,r,t));return s.length===2?[e]:[...Pe(e,t),e]}let K=class Bt extends Nt{constructor(t,e,n,r){super(t,e),this.segmentType="CUBIC_BEZIER",this._boundingBox=null,this._polynomialCoefficients=null,this.firstControlPoint=n,this.lastControlPoint=r}get midPoint(){return this.paramPoint(.5)}get boundingBox(){if(this._boundingBox===null){const t=e=>{const[n,r,o,i]=[this.firstPoint[e],this.firstControlPoint[e],this.lastControlPoint[e],this.lastPoint[e]],h=-n+3*r-3*o+i,l=2*n-4*r+2*o,a=-n+r;return ee(a,l,h).filter(c=>c>=-this.precision&&c<=1+this.precision)};this._boundingBox=me([this.firstPoint,...t(0).map(e=>this.paramPoint(e)),...t(1).map(e=>this.paramPoint(e)),this.lastPoint]).grow(this.precision)}return this._boundingBox}clone(){return new Bt(this.firstPoint,this.lastPoint,this.firstControlPoint,this.lastControlPoint)}reverse(){return new Bt(this.lastPoint,this.firstPoint,this.lastControlPoint,this.firstControlPoint)}isSame(t){return t.segmentType!=="CUBIC_BEZIER"?!1:y(this.firstPoint,t.firstPoint)&&y(this.lastPoint,t.lastPoint)&&y(this.firstControlPoint,t.firstControlPoint)&&y(this.lastControlPoint,t.lastControlPoint)}distanceFrom(t){const e=We(n=>vt(this.paramPoint(n),t),this.precision);return e.argMin<-this.precision||e.argMin>1+this.precision?Math.min(z(this.firstPoint,t),z(this.lastPoint,t)):Math.sqrt(e.fMin)}isOnSegment(t){if(!this.boundingBox.contains(t))return!1;try{const e=this.pointToParam(t);return e>=-this.precision&&e<=1+this.precision}catch{return!1}}gradientAt(t){const e=1-t,n=e*e,r=t*t,o=3*n,i=6*e*t,h=3*r,l=q(this.firstControlPoint,this.firstPoint),a=q(this.lastControlPoint,this.firstControlPoint),c=q(this.lastPoint,this.lastControlPoint);return[o*l[0]+i*a[0]+h*c[0],o*l[1]+i*a[1]+h*c[1]]}tangentAt(t){const e=this.pointToParam(t);return J(this.gradientAt(e))}get tangentAtFirstPoint(){return J(q(this.firstControlPoint,this.firstPoint))}get tangentAtLastPoint(){return J(q(this.lastControlPoint,this.lastPoint))}normalAt(t){const e=this.tangentAt(t);return G(e)}splitAtParameters(t,e=null){const n=[...t];n.sort((o,i)=>o-i);let r={originalParam:0,param:0,p0:this.firstPoint,p1:this.firstControlPoint,p2:this.lastControlPoint};return n.flatMap((o,i)=>{if(o-r.originalParam<this.precision)return[];let h=!1;o>1-this.precision&&(h=!0);const l=(o-r.originalParam)/(1-r.originalParam),a=Pe([r.p0,r.p1,r.p2,this.lastPoint],h?1:l),c=h?this.lastPoint:(e==null?void 0:e.get(o))??a[0][0],u=a[1][0],f=a[2][0],p=a[1][1],g=a[2][2],w=new Bt(r.p0,c,f,u);return r={param:l,originalParam:o,p0:c,p1:p,p2:g},i===n.length-1&&!h?[w,new Bt(r.p0,this.lastPoint,r.p1,r.p2)]:w})}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const n=e.map(o=>this.pointToParam(o)),r=new Map(at([n,e]));return this.splitAtParameters(n,r)}transform(t){return new Bt(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.firstControlPoint),t.transform(this.lastControlPoint))}paramPoint(t){const e=1-t,n=e*e,r=t*t,o=n*e,i=3*n*t,h=3*e*r,l=r*t;return[o*this.firstPoint[0]+i*this.firstControlPoint[0]+h*this.lastControlPoint[0]+l*this.lastPoint[0],o*this.firstPoint[1]+i*this.firstControlPoint[1]+h*this.lastControlPoint[1]+l*this.lastPoint[1]]}get polynomialCoefficients(){if(this._polynomialCoefficients===null){const t=e=>{const n=this.firstPoint[e],r=this.firstControlPoint[e],o=this.lastControlPoint[e],i=this.lastPoint[e],h=-n+3*r-3*o+i,l=3*n-6*r+3*o,a=-3*n+3*r;return[n,a,l,h]};this._polynomialCoefficients=[t(0),t(1)]}return this._polynomialCoefficients}paramsAtY(t){const[e,n,r,o]=this.polynomialCoefficients[1];return nn(e-t,n,r,o).filter(i=>i>=-this.precision&&i<=1+this.precision)}pointToParam(t){const e=this.paramsAtY(t[1]);if(e.length===0)throw new Error("Point is not on the curve");const n=o=>Math.abs(this.paramPoint(o)[0]-t[0])<=this.precision,r=e.find(n);if(r===void 0)throw new Error("Point is not on the curve");return r}};function Gs(s,t){const e=q(t,s.firstPoint),n=pe(e,s.V)/s.squareLength;return s.paramPoint(n)}function ne(s,t,e){const n=e||s.precision,r=Gs(s,t.center),o=z(r,t.center);if(o>t.radius+n)return[];if(Math.abs(o-t.radius)<n){const u=r;return s.isOnSegment(u)&&t.isOnSegment(u)?[u]:[]}const i=[],h=Math.sqrt(t.radius*t.radius-o*o),l=s.tangentAtFirstPoint,a=V(r,W(l,h));s.isOnSegment(a)&&t.isOnSegment(a)&&i.push(a);const c=V(r,W(l,-h));return s.isOnSegment(c)&&t.isOnSegment(c)&&i.push(c),i}const Ks=s=>{const{firstPoint:t,lastPoint:e,center:n,clockwise:r}=s;return new T(e,t,n,r,{ignoreChecks:!0})},Qs=(s,t)=>{if(s.isSame(t))return[s];const e=mt([t.isOnSegment(s.firstPoint)?s.firstPoint:null,t.isOnSegment(s.lastPoint)?s.lastPoint:null,s.isOnSegment(t.firstPoint)?t.firstPoint:null,s.isOnSegment(t.lastPoint)?t.lastPoint:null].filter(n=>n!==null)).sort((n,r)=>s.pointToParam(n)-s.pointToParam(r));if(e.length===0)return[];if(e.length===1)return[];if(e.length===2)return s.isSame(Ks(t))?[]:[new T(e[0],e[1],s.center,s.clockwise)];if(e.length===3){const n=y(e[0],t.lastPoint)||y(e[0],t.firstPoint)?1:0;return[new T(e[0+n],e[1+n],s.center,s.clockwise)]}else if(e.length===4)return[new T(e[0],e[1],s.center,s.clockwise),new T(e[2],e[3],s.center,s.clockwise)];throw new Error("Bug in the arc arc overlap algorithm")};function rn(s,t,e=!1,n){const r=n||s.precision,o=z(s.center,t.center),i=s.radius+t.radius;if(o>i+r)return[];const h=Math.abs(s.radius-t.radius);if(o<h-r)return[];if(o<r)return h>r?[]:e?Qs(s,t):[];const l=J(q(t.center,s.center)),a=o>i-r;if(a||Math.abs(o-h)<r){const m=a||s.radius>t.radius?1:-1,d=V(s.center,W(l,m*s.radius));return s.isOnSegment(d)&&t.isOnSegment(d)?[d]:[]}const c=s.radius*s.radius/(2*o)-t.radius*t.radius/(2*o)+o/2,u=V(s.center,W(l,c)),f=Math.sqrt(s.radius*s.radius-c*c),p=G(l),g=V(u,W(p,f)),w=V(u,W(p,-f)),P=[];return s.isOnSegment(g)&&t.isOnSegment(g)&&P.push(g),s.isOnSegment(w)&&t.isOnSegment(w)&&P.push(w),P}let Z=class Xt extends Nt{constructor(t,e,n,r,o,i,h=!1,{ignoreChecks:l=!1,angleUnits:a="deg"}={}){super(t,e),this.segmentType="ELLIPSE_ARC",this.precision=1e-6,this._coefficients=null,this.center=n;const c=r>=o;this.majorRadius=c?r:o,this.minorRadius=c?o:r;const u=a==="deg"?i*xt:i;if(this.tiltAngle=Et(c?u:u+Math.PI/2),this.clockwise=h,!l){if(y(t,e))throw new Error("Invalid arc, cannot be a full circle");if(!this.isPointOnEllipse(t))throw new Error(`First point ${H(t)} not on the ellipse defined by ${this.info}`);if(!this.isPointOnEllipse(e))throw new Error(`Last point ${H(e)} 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,e]=this.center,n=this.majorRadius*this.majorRadius,r=this.minorRadius*this.minorRadius,o=Math.cos(this.tiltAngle),i=Math.sin(this.tiltAngle),h=n*i*i+r*o*o,l=2*(r-n)*i*o,a=n*o*o+r*i*i;this._coefficients={x2:h,xy:l,y2:a,x:-2*h*t-l*e,y:-2*a*e-l*t,c:h*t*t+l*t*e+a*e*e-n*r}}return this._coefficients}get info(){return`ELLIPSE_ARC(${H(this.firstPoint)}, ${H(this.lastPoint)}, ${H(this.center)}, ${this.majorRadius}, ${this.minorRadius}, ${this.tiltAngle*ge}, ${this.clockwise?"CW":"CCW"})`}reverse(){return new Xt(this.lastPoint,this.firstPoint,this.center,this.majorRadius,this.minorRadius,this.tiltAngle,!this.clockwise,{ignoreChecks:!0,angleUnits:"rad"})}clone(){return new Xt(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),e=this.minorRadius/this.majorRadius,n=-Math.atan(e*t),r=Math.atan(e/t);return[n,Math.PI+n,r,Math.PI+r].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(),e=[this.firstPoint,this.lastPoint,...t];this._boundingBox=me(e).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=[V(this.center,W(t,this.linearExentricity)),V(this.center,W(t,-this.linearExentricity))]}return this._focals}get majorAxis(){return gt(1,this.tiltAngle)}paramPoint(t){const e=this.firstAngle+t*this.deltaAngle*(this.clockwise?-1:1);return this.reverseEllipseReferenceFrameTransform.transform([this.majorRadius*Math.cos(e),this.minorRadius*Math.sin(e)])}pointToParam(t){if(!this.isPointOnEllipse(t))throw new Error(`Point ${H(t)} not on the ellipse defined by ${this.repr}`);const e=this.thetaToParam(this.pointTheta(t));if(!this.isValidParameter(e))throw new Error(`Point ${H(t)} is not on segment ${this.repr}`);return e}get midPoint(){return this.paramPoint(.5)}isValidParameter(t){return 1-t>=-this.precision&&t>=-this.precision}isSame(t){return y(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)&&(y(this.firstPoint,t.firstPoint)&&this.clockwise===t.clockwise||y(this.firstPoint,t.lastPoint)&&this.clockwise!==t.clockwise)}pointTheta(t){const e=this.ellipseReferenceFrameTransform.transform(t),n=Math.atan2(e[1]/this.minorRadius,e[0]/this.majorRadius);return Et(n)}thetaToParam(t){return Ht(this.firstAngle,Et(t),this.clockwise)/this.deltaAngle}isPointOnEllipse(t){const[e,n]=this.focals,r=z(t,e),o=z(t,n);return Math.abs(2*this.majorRadius-r-o)<this.precision}isOnSegment(t){return this.isPointOnEllipse(t)?this.isValidParameter(this.thetaToParam(this.pointTheta(t))):!1}distanceFrom(t){let e;if(y(t,this.center)?e=V(this.center,W(G(this.majorAxis),this.minorRadius)):e=this.reverseEllipseReferenceFrameTransform.transform(nr(this.majorRadius,this.minorRadius,this.ellipseReferenceFrameTransform.transform(t))),this.isValidParameter(this.thetaToParam(this.pointTheta(t))))return z(t,e);if(os(t,this.majorAxis,this.center)){const n=Et(2*Math.PI-this.pointTheta(t)),r=this.thetaToParam(n);if(this.isValidParameter(r))return z(t,this.paramPoint(r))}return Math.min(z(t,this.firstPoint),z(t,this.lastPoint))}get ellipseReferenceFrameTransform(){return this._ellipseReferenceFrameTransform===void 0&&(this._ellipseReferenceFrameTransform=new tt().rotate(-this.tiltAngle).translate(-this.center[0],-this.center[1])),this._ellipseReferenceFrameTransform}get reverseEllipseReferenceFrameTransform(){return this._reverseEllipseReferenceFrameTransform===void 0&&(this._reverseEllipseReferenceFrameTransform=new tt().translate(this.center[0],this.center[1]).rotate(this.tiltAngle)),this._reverseEllipseReferenceFrameTransform}get rotateFromEllipseReferenceFrame(){return this._rotateFromEllipseReferenceFrame===void 0&&(this._rotateFromEllipseReferenceFrame=new tt().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=Ht(this.firstAngle,this.lastAngle,this.clockwise)),this._deltaAngle}normalAt(t){const e=this.tangentAt(t);return this.clockwise?G(e):Ot(e)}gradientAt(t){const e=this.firstAngle+t*this.deltaAngle*(this.clockwise?-1:1),n=-this.majorRadius*this.deltaAngle*Math.sin(e),r=this.minorRadius*this.deltaAngle*Math.cos(e),o=this.clockwise?[-n,-r]:[n,r];return this.rotateFromEllipseReferenceFrame.transform(o)}tangentAt(t){const e=this.pointTheta(t),n=-this.majorRadius*Math.sin(e),r=this.minorRadius*Math.cos(e),o=this.clockwise?[-n,-r]:[n,r];return J(this.rotateFromEllipseReferenceFrame.transform(o))}get tangentAtFirstPoint(){const t=-this.majorRadius*Math.sin(this.firstAngle),e=this.minorRadius*Math.cos(this.firstAngle),n=this.clockwise?[-t,-e]:[t,e];return J(this.rotateFromEllipseReferenceFrame.transform(n))}get tangentAtLastPoint(){const t=-this.majorRadius*Math.sin(this.lastAngle),e=this.minorRadius*Math.cos(this.lastAngle),n=this.clockwise?[-t,-e]:[t,e];return J(this.rotateFromEllipseReferenceFrame.transform(n))}transform(t){const e=t.transformAngle(this.tiltAngle),n=t.scaleFactor();return new Xt(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.center),this.majorRadius*n,this.minorRadius*n,e,t.keepsOrientation()?this.clockwise:!this.clockwise,{angleUnits:"rad"})}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const n=[0,1,...e.map(i=>this.pointToParam(i))],r=new Map(at([n,[this.firstPoint,this.lastPoint,...e]]));n.sort((i,h)=>i-h);let o=null;return n.flatMap((i,h)=>{if(h===n.length-1)return[];const l=n[h+1];if(l-i<this.precision)return o===null&&(o=i),[];const a=o===null?i:o,c=new Xt(r.get(a)||this.paramPoint(a),r.get(l)||this.paramPoint(l),this.center,this.majorRadius,this.minorRadius,this.tiltAngle,this.clockwise,{angleUnits:"rad"});return o=null,c})}};function tr(s,t,e,n,r,o,i){const{center:h,rx:l,ry:a}=er(s,t,e,n,r*xt,!o,i);return Math.abs(l-a)<1e-9?new T(s,t,h,i):new Z(s,t,h,l,a,r,i)}function er([s,t],[e,n],r,o,i,h,l){if(r<0&&(r=-r),o<0&&(o=-o),r==0||o==0)throw Error("rx and ry can not be 0");const a=Math.sin(i),c=Math.cos(i),u=(s-e)/2,f=(t-n)/2,p=(s+e)/2,g=(t+n)/2,w=c*u+a*f,P=c*f-a*u,m=w*w/(r*r)+P*P/(o*o);m>1&&(r=r*Math.sqrt(m),o=o*Math.sqrt(m));const d=r*o,M=r*P,x=o*w,v=M*M+x*x;if(!v)throw Error("start point can not be same as end point");let k=Math.sqrt(Math.abs((d*d-v)/v));h==l&&(k=-k);const A=k*M/o,b=-k*x/r,I=c*A-a*b+p,O=a*A+c*b+g;return{center:[I,O],rx:r,ry:o}}function nr(s,t,e){const n=Math.abs(e[0]),r=Math.abs(e[1]);let o=.707,i=.707;const h=s,l=t;for(let a=0;a<3;a++){const c=h*o,u=l*i,f=(h*h-l*l)*o**3/h,p=(l*l-h*h)*i**3/l,g=c-f,w=u-p,P=n-f,m=r-p,d=Math.hypot(g,w),M=Math.hypot(P,m);o=Math.min(1,Math.max(0,(P*d/M+f)/h)),i=Math.min(1,Math.max(0,(m*d/M+p)/l));const x=Math.hypot(o,i);o/=x,i/=x}return[h*o*Math.sign(e[0]),l*i*Math.sign(e[1])]}function Me(s,t,e=1e-9){const n=s.transform(t.ellipseReferenceFrameTransform),r=n.slope,o=n.yIntercept,i=t.majorRadius*t.majorRadius,h=t.minorRadius*t.minorRadius,l=t.majorRadius*t.minorRadius,a=n.slope*n.slope,c=n.yIntercept*n.yIntercept,u=m=>m.map(d=>t.reverseEllipseReferenceFrameTransform.transform(d)).filter(d=>s.isOnSegment(d)&&t.isOnSegment(d));if(!Number.isFinite(r)){const m=n.firstPoint[0];if(Math.abs(m)-t.majorRadius>e)return[];if(Math.abs(Math.abs(m)-t.majorRadius)<e)return u([[m,0]]);const d=t.minorRadius*Math.sqrt(1-m*m/i),M=[m,d],x=[m,-d];return u([M,x])}const f=i*a+h-c;if(f<-e)return[];const p=i*a+h;if(Math.abs(f)<e){const m=-(i*r*o)/p,d=h*o/p;return u([[m,d]])}const g=Math.sqrt(f),w=[-(i*r*o+l*g)/p,(h*o-l*r*g)/p],P=[-(i*r*o-l*g)/p,(h*o+l*r*g)/p];return u([w,P])}function on(s,t){const e=Math.max(s.precision,t.precision),n=s.coefficients,r=n.x2,o=n.xy,i=n.y2,h=n.x,l=n.y,a=n.c,c=t.coefficients,u=c.x2,f=c.xy,p=c.y2,g=c.x,w=c.y,P=c.c,m={z0:a*r*g*g+r*r*P*P-h*r*g*P+u*u*a*a-2*r*P*u*a-h*g*u*a+u*h*h*P,z1:w*h*h*u-P*g*r*o-2*r*P*u*l-a*u*f*h+2*g*f*r*a+2*w*P*r*r+g*g*r*l-w*g*r*h-2*r*w*u*a-a*u*g*o+2*a*l*u*u-P*f*r*h-l*u*g*h+2*P*o*u*h,z2:w*w*r*r+2*p*P*r*r-l*u*g*o+P*u*o*o-l*u*f*h-P*f*r*o-2*r*w*u*l+2*g*f*r*l-p*g*r*h-2*r*p*u*a+f*f*r*a+2*w*o*u*h+l*l*u*u-i*u*g*h-w*f*r*h+2*a*i*u*u-a*u*f*o+p*h*h*u+g*g*r*i-w*g*r*o-2*r*P*u*i,z3:-2*r*u*i*w+w*u*o*o+2*p*o*u*h-i*u*f*h+f*f*r*l-w*f*r*o-2*r*p*u*l-l*u*f*o-p*f*r*h+2*w*p*r*r+2*l*i*u*u-i*u*g*o+2*g*f*r*i-p*g*r*o,z4:r*r*p*p-2*r*p*u*i+u*u*i*i-o*r*f*p-o*f*u*i+o*o*u*p+i*r*f*f},d=de([m.z0,m.z1,m.z2,m.z3,m.z4],e).flatMap(M=>{const x=r*f*M+r*g-u*o*M-u*h;if(x)return[[-(r*P+r*p*M*M-u*i*M*M+r*w*M-u*l*M-u*a)/x,M]];const v=o*M+h,k=-v/(2*r),A=i*M*M+l*M+a,b=v*v/(4*r*r)-A/r;if(Math.abs(b)<e)return[[k,M]];if(b>0){const I=Math.sqrt(b);return[[k+I,M],[k-I,M]]}return[]});return mt(d,e)}function hn(s,t){return on(s,t).filter(e=>s.isOnSegment(e)&&t.isOnSegment(e))}const sr=s=>{const{firstPoint:t,lastPoint:e,center:n,majorRadius:r,minorRadius:o,tiltAngle:i,clockwise:h}=s;return new Z(e,t,n,r,o,i,h,{ignoreChecks:!0,angleUnits:"rad"})},rr=(s,t)=>{if(s.isSame(t))return[s];const e=(r,o)=>new Z(r,o,s.center,s.majorRadius,s.minorRadius,s.tiltAngle,s.clockwise,{ignoreChecks:!0,angleUnits:"rad"}),n=mt([t.isOnSegment(s.firstPoint)?s.firstPoint:null,t.isOnSegment(s.lastPoint)?s.lastPoint:null,s.isOnSegment(t.firstPoint)?t.firstPoint:null,s.isOnSegment(t.lastPoint)?t.lastPoint:null].filter(r=>r!==null)).sort((r,o)=>s.pointToParam(r)-s.pointToParam(o));if(n.length===0)return[];if(n.length===1)return[];if(n.length===2)return s.isSame(sr(t))?[]:[e(n[0],n[1])];if(n.length===3){const r=y(n[0],t.lastPoint)||y(n[0],t.firstPoint)?1:0;return[e(n[0+r],n[1+r])]}else if(n.length===4)return[e(n[0],n[1]),e(n[2],n[3])];throw new Error("Bug in the ellipse arc ellipse arc overlap algorithm")};function or(s,t,e=!1){const n=Math.max(s.precision,t.precision);return y(s.center,t.center)&&Math.abs(s.majorRadius-t.majorRadius)<n&&Math.abs(s.minorRadius-t.minorRadius)<n&&(Math.abs(s.tiltAngle-t.tiltAngle)<n||Math.abs(Math.abs(s.tiltAngle-t.tiltAngle)-Math.PI)<n)?e?rr(s,t):[]:on(s,t).filter(r=>s.isOnSegment(r)&&t.isOnSegment(r))}function an(s,t){const[e,n]=s.firstPoint,[r,o]=s.lastPoint,i=new tt().rotate(-Math.atan2(o-n,r-e)).translate(-e,-n),h=i.clone().inverse(),l=t.transform(i);return l.paramsAtY(0).map(a=>l.paramPoint(a)).map(a=>h.transform(a)).filter(a=>s.isOnSegment(a))}const ir=(s,t=1e-9)=>{let e=s;return Math.abs(s)<t&&(e=0),e.toFixed(-Math.log10(t))};function ln(s,t=1e-9){return Array.from(new Map(s.map(e=>[ir(e,t),e])).values())}const hr=(s,t)=>{const[[e,n,r,o],[i,h,l,a]]=t.polynomialCoefficients,c=s.coefficients,u=c.x2,f=c.xy,p=c.y2,g=c.x,w=c.y,P=c.c,m=e*e,d=n*n,M=r*r,x=o*o,v=i*i,k=h*h,A=l*l,b=a*a,I=P+g*e+u*m+w*i+f*e*i+p*v,O=g*n+2*u*e*n+f*n*i+w*h+f*e*h+2*p*i*h,D=u*d+g*r+2*u*e*r+f*r*i+f*n*h+p*k+w*l+f*e*l+2*p*i*l,nt=2*u*n*r+g*o+2*u*e*o+f*o*i+f*r*h+f*n*l+2*p*h*l+w*a+f*e*a+2*p*i*a,L=u*M+2*u*n*o+f*o*h+f*r*l+p*A+f*n*a+2*p*h*a,C=2*u*r*o+f*o*l+f*r*a+2*p*l*a,S=u*x+f*o*a+p*b;return[I,O,D,nt,L,C,S]};function un(s,t){const e=Math.max(s.precision,t.precision),n=hr(s,t),r=de(n,e).filter(o=>o>=-t.precision&&o<=1+t.precision);return ln(r,e).map(o=>t.paramPoint(o)).filter(o=>s.isOnSegment(o))}class N extends Nt{constructor(t,e,n){super(t,e),this.segmentType="QUADRATIC_BEZIER",this._boundingBox=null,this._polynomialCoefficients=null,this.controlPoint=n}get midPoint(){return this.paramPoint(.5)}get boundingBox(){if(this._boundingBox===null){const t=e=>{const[n,r,o]=[this.firstPoint[e],this.controlPoint[e],this.lastPoint[e]],i=n-2*r+o;if(!i)return[];const h=(n-r)/i;return h>=-this.precision&&h<=1+this.precision?[h]:[]};this._boundingBox=me([this.firstPoint,...t(0).map(e=>this.paramPoint(e)),...t(1).map(e=>this.paramPoint(e)),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:y(this.firstPoint,t.firstPoint)&&y(this.lastPoint,t.lastPoint)&&y(this.controlPoint,t.controlPoint)}distanceFrom(t){const e=We(n=>vt(this.paramPoint(n),t),this.precision);return e.argMin<-this.precision||e.argMin>1+this.precision?Math.min(z(this.firstPoint,t),z(this.lastPoint,t)):Math.sqrt(e.fMin)}isOnSegment(t){if(!this.boundingBox.contains(t))return!1;try{const e=this.pointToParam(t);return e>=-this.precision&&e<=1+this.precision}catch{return!1}}gradientAt(t){const e=n=>{const r=this.firstPoint[n],o=this.controlPoint[n],i=this.lastPoint[n];return 2*(t*(r-2*o+i)-r+o)};return[e(0),e(1)]}tangentAt(t){const e=this.pointToParam(t);return J(this.gradientAt(e))}get tangentAtFirstPoint(){return J(q(this.controlPoint,this.firstPoint))}get tangentAtLastPoint(){return J(q(this.controlPoint,this.lastPoint))}normalAt(t){const e=this.tangentAt(t);return G(e)}splitAtParameters(t,e=null){const n=[...t];n.sort((o,i)=>o-i);let r={originalParam:0,param:0,p0:this.firstPoint,p1:this.controlPoint};return n.flatMap((o,i)=>{if(o-r.originalParam<this.precision)return[];let h=!1;o>1-this.precision&&(h=!0);const l=(o-r.originalParam)/(1-r.originalParam),a=Pe([r.p0,r.p1,this.lastPoint],h?1:l),c=h?this.lastPoint:(e==null?void 0:e.get(o))??a[0][0],u=a[1][0],f=a[1][1],p=new N(r.p0,c,u);return r={param:l,originalParam:o,p0:c,p1:f},i===n.length-1&&!h?[p,new N(r.p0,this.lastPoint,r.p1)]:p})}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const n=e.map(o=>this.pointToParam(o)),r=new Map(at([n,e]));return this.splitAtParameters(n,r)}transform(t){return new N(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.controlPoint))}paramPoint(t){const e=1-t,n=e*e,r=2*e*t,o=t*t;return[n*this.firstPoint[0]+r*this.controlPoint[0]+o*this.lastPoint[0],n*this.firstPoint[1]+r*this.controlPoint[1]+o*this.lastPoint[1]]}get polynomialCoefficients(){if(this._polynomialCoefficients===null){const t=e=>{const n=this.firstPoint[e],r=this.controlPoint[e],o=this.lastPoint[e],i=n-2*r+o,h=2*(r-n);return[n,h,i]};this._polynomialCoefficients=[t(0),t(1)]}return this._polynomialCoefficients}paramsAtY(t){const[e,n,r]=this.polynomialCoefficients[1];return ee(e-t,n,r).filter(o=>o>=-this.precision&&o<=1+this.precision)}pointToParam(t){const e=this.paramsAtY(t[1]);if(e.length===0)throw new Error("Point is not on the curve!");const n=o=>Math.abs(this.paramPoint(o)[0]-t[0])<=this.precision,r=e.find(n);if(r===void 0)throw new Error("Point is not on the curve");return r}}const ar=(s,t)=>{const[[e,n,r],[o,i,h]]=t.polynomialCoefficients,l=s.coefficients,a=l.x2,c=l.xy,u=l.y2,f=l.x,p=l.y,g=l.c,w=e*e,P=n*n,m=r*r,d=o*o,M=i*i,x=h*h,v=a*w+c*e*o+u*d+f*e+p*o+g,k=2*a*e*n+c*e*i+c*n*o+2*u*o*i+f*n+p*i,A=2*a*e*r+a*P+c*e*h+c*n*i+c*r*o+2*u*o*h+u*M+f*r+p*h,b=2*a*n*r+c*n*h+c*r*i+2*u*i*h,I=a*m+c*r*h+u*x;return[v,k,A,b,I]};function cn(s,t){const e=Math.max(s.precision,t.precision),n=ar(s,t),r=Xs(...n).filter(o=>o>=-t.precision&&o<=1+t.precision);return ln(r,e).map(o=>t.paramPoint(o)).filter(o=>s.isOnSegment(o))}function et(s,{firstPoint:t,lastPoint:e},n=1e-9){const r=q(e,t);return Math.abs(r[0])<n?r[1]>0?t[0]-s[0]:s[0]-t[0]:Math.abs(r[1])<n?r[0]>0?s[1]-t[1]:t[1]-s[1]:dt(r,q(s,t))/ss(r)}class ye{constructor(t,e,n,r){this.firstPoint=t,this.lastPoint=e,this.negativeThickness=n,this.positiveThickness=r}get width(){return this.positiveThickness-this.negativeThickness}}const lr=3/4,ur=4/9;function cr(s){const t=et(s.firstControlPoint,s),e=et(s.lastControlPoint,s),n=t*e>0?lr:ur;return new ye(s.firstPoint,s.lastPoint,n*Math.min(0,t,e),n*Math.max(0,t,e))}function fr(s){const t=et(s.controlPoint,s);return new ye(s.firstPoint,s.lastPoint,Math.min(0,t/2),Math.max(0,t/2))}function mr(s){if(s instanceof K)return cr(s);if(s instanceof N)return fr(s);throw new Error("Not implemented")}function gr(s){const t=s.paramPoint(.5),e=G(q(t,s.firstPoint)),n=V(t,e),r={firstPoint:t,lastPoint:n},o=[et(s.firstPoint,r),et(s.lastPoint,r)];return s instanceof K?o.push(et(s.firstControlPoint,r),et(s.lastControlPoint,r)):s instanceof N&&o.push(et(s.controlPoint,r)),new ye(t,n,Math.min(...o),Math.max(...o))}function fn(s,t){const e=[];for(let n=1;n<s.length;n++){const r=s[n];if(r[1]===t){e.push(r[0]);continue}const o=s[n-1],i=t-o[1],h=t-r[1];if(i*h<0){e.push(o[0]+(t-o[1])*(r[0]-o[0])/(r[1]-o[1]));continue}}return e}class Vt{constructor(t,e){this.from=t,this.to=e}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 pr(s,t){if(s instanceof K)return new dr([et(s.firstPoint,t),et(s.firstControlPoint,t),et(s.lastControlPoint,t),et(s.lastPoint,t)]);if(s instanceof N)return new wr([et(s.firstPoint,t),et(s.controlPoint,t),et(s.lastPoint,t)]);throw new Error("Not implemented")}class wr{constructor(t){this.distances=t,this.topHull=[],this.bottomHull=[];const[e,n,r]=t,o=[0,e],i=[1/2,n],h=[1,r],l=r-e,a=e;n-(l*(1/2)+a)>0?(this.topHull=[o,i,h],this.bottomHull=[o,h]):(this.topHull=[o,h],this.bottomHull=[o,i,h])}get startDistance(){return this.distances[0]}get endDistance(){return this.distances[2]}}class dr{constructor(t){this.distances=t,this.topHull=[],this.bottomHull=[];const[e,n,r,o]=t,i=[0,e],h=[1/3,n],l=[2/3,r],a=[1,o],c=o-e,u=e,f=n-(c*(1/3)+u),p=r-(c*(2/3)+u);let g=null,w=null;if(f*p<0)g=[i,h,a],w=[i,l,a];else{const P=f/p;P>=2?(g=[i,h,a],w=[i,a]):P<=.5?(g=[i,l,a],w=[i,a]):(g=[i,h,l,a],w=[i,a])}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 mn(s,t){const e=pr(t,s),n=fn(e.topHull,s.negativeThickness),r=fn(e.bottomHull,s.positiveThickness),o=e.endDistance>=s.negativeThickness&&e.endDistance<=s.positiveThickness;if(!n.length&&!r.length)return o?new Vt("start","end"):null;if(n.length===1&&r.length===1)return new Vt(n[0],r[0]);if(n.length===2&&r.length===2)throw new Error("Bug in the clipping algorithm, unexpected number of crossing points");const i=n.length?n:r;return i.length===2?new Vt(i[0],i[1]):o?new Vt(i[0],"end"):new Vt("start",i[0])}function gn(s,t){const e=mr(s),n=mn(e,t);if(!n)return null;const r=gr(s),o=mn(r,t);return o?n.size>o.size?o.clipCurve(t):n.clipCurve(t):null}const se=s=>s instanceof N?ct(q(s.controlPoint,s.firstPoint))+ct(q(s.controlPoint,s.lastPoint)):ct(q(s.firstControlPoint,s.firstPoint))+ct(q(s.lastControlPoint,s.firstControlPoint))+ct(q(s.lastControlPoint,s.lastPoint));function kt(s,t,e=1e-9,{maxIterations:n=100}={}){const r=Math.max(e*e,Number.EPSILON*10);let o=s,i=t,h=se(o),l=se(i);for(let a=0;a<n;a++){const c=h>r?gn(i,o):o;if(!c)return[];const u=se(c),f=l>r?gn(c,i):i;if(!f)return[];const p=se(f);if(u<=r&&p<=r)return[c.boundingBox.intersection(f.boundingBox).center];if(y(c.firstPoint,c.lastPoint)&&f.isOnSegment(c.firstPoint))return[c.firstPoint];if(y(f.firstPoint,f.lastPoint)&&c.isOnSegment(f.firstPoint))return[f.firstPoint];if(u>.8*h&&p>.8*l)if(u/h>p/l){const[g,w]=c.splitAtParameters([.5]);return mt([...kt(g,f,e,{maxIterations:n-a}),...kt(w,f,e,{maxIterations:n-a})],e)}else{const[g,w]=f.splitAtParameters([.5]);return mt([...kt(c,g,e,{maxIterations:n-a}),...kt(c,w,e,{maxIterations:n-a})],e)}o=c,i=f,h=u,l=p}throw new Error("Bézier clip: Maximum number of iterations reached")}function Pr(s,t){const e=[];if([[s.firstPoint,t],[s.lastPoint,t],[t.firstPoint,s],[t.lastPoint,s]].forEach(([n,r])=>{r.isOnSegment(n)&&e.push(n)}),e.length<2)return null;if(e.length===2)return[s.splitAt(e)[1]];if(e.length===3)return y(e[0],s.firstPoint)&&y(e[1],s.lastPoint)?[s]:[t];if(e.length===4)return[s]}function Mr(s,t,e=!1){const n=Math.max(s.precision,t.precision);if(e){const r=Pr(s,t);if(r)return r}return kt(s,t,n)}function yr(s,t){const e=[];if([[s.firstPoint,t],[s.lastPoint,t],[t.firstPoint,s],[t.lastPoint,s]].forEach(([n,r])=>{r.isOnSegment(n)&&e.push(n)}),e.length<2)return null;if(e.length===2)return[s.splitAt(e)[1]];if(e.length===3)return y(e[0],s.firstPoint)&&y(e[1],s.lastPoint)?[s]:[t];if(e.length===4)return[s]}function br(s,t,e=!1){const n=Math.max(s.precision,t.precision);if(e){const r=yr(s,t);if(r)return r}return kt(s,t,n)}function xr(s,t,e){if(s instanceof B&&t instanceof B){const n=Ue(s,t,!1,e);return n===null?[]:[n]}if(s instanceof B&&t instanceof T)return ne(s,t,e);if(s instanceof T&&t instanceof B)return ne(t,s,e);if(s instanceof T&&t instanceof T)return rn(s,t,!1,e);throw new Error("Not implemented")}function zt(s,t,e){if(s instanceof B&&t instanceof B){const n=Ue(s,t,!0,e);return n===null?{intersections:[],overlaps:[],count:0}:n instanceof B?{intersections:[],overlaps:[n],count:1}:{intersections:[n],overlaps:[],count:1}}if(!s.boundingBox.overlaps(t.boundingBox))return{intersections:[],overlaps:[],count:0};if(s instanceof B&&t instanceof T){const n=ne(s,t,e);return{intersections:n,overlaps:[],count:n.length}}if(s instanceof T&&t instanceof B){const n=ne(t,s,e);return{intersections:n,overlaps:[],count:n.length}}if(s instanceof T&&t instanceof T){const n=rn(s,t,!0,e);return n.length?n[0]instanceof T?{intersections:[],overlaps:n,count:n.length}:{intersections:n,overlaps:[],count:n.length}:{intersections:[],overlaps:[],count:0}}if(s instanceof B&&t instanceof Z){const n=Me(s,t,e);return{intersections:n,overlaps:[],count:n.length}}if(t instanceof B&&s instanceof Z){const n=Me(t,s,e);return{intersections:n,overlaps:[],count:n.length}}if(s instanceof T&&t instanceof Z){const n=hn(s,t);return{intersections:n,overlaps:[],count:n.length}}if(t instanceof T&&s instanceof Z){const n=hn(t,s);return{intersections:n,overlaps:[],count:n.length}}if(s instanceof Z&&t instanceof Z){const n=or(s,t,!0);return n.length?n[0]instanceof Z?{intersections:[],overlaps:n,count:n.length}:{intersections:n,overlaps:[],count:n.length}:{intersections:[],overlaps:[],count:0}}if(s instanceof B&&(t instanceof K||t instanceof N)){const n=an(s,t);return{intersections:n,overlaps:[],count:n.length}}if(t instanceof B&&(s instanceof K||s instanceof N)){const n=an(t,s);return{intersections:n,overlaps:[],count:n.length}}if((s instanceof T||s instanceof Z)&&t instanceof N){const n=cn(s,t);return{intersections:n,overlaps:[],count:n.length}}if((t instanceof T||t instanceof Z)&&s instanceof N){const n=cn(t,s);return{intersections:n,overlaps:[],count:n.length}}if((s instanceof T||s instanceof Z)&&t instanceof K){const n=un(s,t);return{intersections:n,overlaps:[],count:n.length}}if((t instanceof T||t instanceof Z)&&s instanceof K){const n=un(t,s);return{intersections:n,overlaps:[],count:n.length}}if(s instanceof N&&t instanceof N){const n=br(s,t);return n.length?n[0]instanceof N?{intersections:[],overlaps:n,count:n.length}:{intersections:n,overlaps:[],count:n.length}:{intersections:[],overlaps:[],count:0}}if(s instanceof N&&t instanceof K||t instanceof N&&s instanceof K){const n=kt(s,t);return{intersections:n,overlaps:[],count:n.length}}if(s instanceof K&&t instanceof K){const n=Mr(s,t);return n.length?n[0]instanceof K?{intersections:[],overlaps:n,count:n.length}:{intersections:n,overlaps:[],count:n.length}:{intersections:[],overlaps:[],count:0}}throw new Error("Not implemented")}function pn(s){const t=[];for(let e=0;e<s;e++)for(let n=0;n<=e;n++)t.push([e,n]);return t}function*be(s){for(const[t,e]of pn(s.length))t!==e&&(yield[s[t],s[e]])}class wn extends At{constructor(t,{ignoreChecks:e=!1}={}){super(),this._boundingBox=null,e||dn(t),this.segments=t}get repr(){return this.segments.map(t=>t.repr).join(`
|
|
1
|
+
(function(X,ht){typeof exports=="object"&&typeof module<"u"?ht(exports):typeof define=="function"&&define.amd?define(["exports"],ht):(X=typeof globalThis<"u"?globalThis:X||self,ht(X.Dielines={}))})(this,function(X){"use strict";var xr=Object.defineProperty;var vr=(X,ht,Ft)=>ht in X?xr(X,ht,{enumerable:!0,configurable:!0,writable:!0,value:Ft}):X[ht]=Ft;var Et=(X,ht,Ft)=>vr(X,typeof ht!="symbol"?ht+"":ht,Ft);var ht=Object.defineProperty,Ft=(i,t,e)=>t in i?ht(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,A=(i,t,e)=>(Ft(i,typeof t!="symbol"?t+"":t,e),e);function Kt(i,t,e,s){return i<=s&&t>=e}let Yt=class Qt{constructor(t=1/0,e=1/0,s=-1/0,n=-1/0){A(this,"xMin"),A(this,"yMin"),A(this,"xMax"),A(this,"yMax"),this.xMin=t,this.yMin=e,this.xMax=s,this.yMax=n}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 Qt(this.xMin-t,this.yMin-t,this.xMax+t,this.yMax+t)}contains(t){const[e,s]=t;return Kt(this.xMin,this.xMax,e,e)&&Kt(this.yMin,this.yMax,s,s)}overlaps(t){return Kt(this.xMin,this.xMax,t.xMin,t.xMax)&&Kt(this.yMin,this.yMax,t.yMin,t.yMax)}addPoint(t){const[e,s]=t;return new Qt(Math.min(this.xMin,e),Math.min(this.yMin,s),Math.max(this.xMax,e),Math.max(this.yMax,s))}merge(t){return new Qt(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 Qt(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 xe(i){return i.reduce((t,e)=>t.addPoint(e),new Yt)}const Ze=(i,t=1e-9)=>{let e=i;return Math.abs(i)<t&&(e=0),e.toFixed(-Math.log10(t))};function Pt(i,t=1e-9){return Array.from(new Map(i.map(([e,s])=>[`[${Ze(e,t)},${Ze(s,t)}]`,[e,s]])).values())}const St=Math.PI/180,ve=180/Math.PI,H=i=>`[${i[0]}, ${i[1]}]`,y=([i,t],[e,s],n=1e-9)=>Math.abs(i-e)<=n&&Math.abs(t-s)<=n,O=([i,t],[e,s])=>[i+e,t+s],q=([i,t],[e,s])=>[i-e,t-s],wt=([i,t])=>i*i+t*t,fi=([i,t])=>Math.sqrt(wt([i,t])),G=([i,t],e)=>[i*e,t*e],kt=([i,t],[e,s]=[0,0])=>(i-e)**2+(t-s)**2,N=(i,t=[0,0])=>Math.sqrt(kt(i,t));function bt([i,t],[e,s]){return i*s-t*e}function Ae([i,t],[e,s]){return i*e+t*s}function W([i,t]){const e=N([i,t]);return[i/e,t/e]}function Mt(i,t){const e=Math.cos(t)*i,s=Math.sin(t)*i;return[e,s]}function mi([i,t]){return Math.atan2(t,i)}function Qe(i){const t=N(i),e=mi(i);return[t,e]}function Ee(i,t,e=1e-9){const s=bt(i,t),n=wt(i),r=wt(t);return s*s<n*r*e*e}function tt(i){return[-i[1],i[0]]}function Dt(i){return[i[1],-i[0]]}function gi(i,t,e){return y(e,i)||Ee(q(e,i),t)}const It=(i,t)=>{const[e,s,n,r,o,h,l,a,c]=i,[u,f,p,g,w,P,m,d,M]=t;return[e*u+s*g+n*m,e*f+s*w+n*d,e*p+s*P+n*M,r*u+o*g+h*m,r*f+o*w+h*d,r*p+o*P+h*M,l*u+a*g+c*m,l*f+a*w+c*d,l*p+a*P+c*M]},pi=i=>{const[t,e,s,n,r,o,h,l,a]=i,c=t*(r*a-o*l)-e*(n*a-o*h)+s*(n*l-r*h);return[(r*a-o*l)/c,(s*l-e*a)/c,(e*o-s*r)/c,(o*h-n*a)/c,(t*a-s*h)/c,(s*n-t*o)/c,(n*l-r*h)/c,(e*h-t*l)/c,(t*r-e*n)/c]},wi=i=>{const[t,e,s,n,r,o,h,l,a]=i;return[t,n,h,e,r,l,s,o,a]};let at=class ui{constructor(t){A(this,"_matrix",[1,0,0,0,1,0,0,0,1]),t&&(this._matrix=[...t])}clone(){return new ui(this._matrix)}transpose(){return this._matrix=wi(this._matrix),this}inverse(){return this._matrix=pi(this._matrix),this}translate(t,e){return this._matrix=It(this._matrix,[1,0,t,0,1,e,0,0,1]),this}rotate(t,e){const s=Math.cos(t),n=Math.sin(t),r=[s,-n,0,n,s,0,0,0,1];return e&&this.translate(e[0],e[1]),this._matrix=It(this._matrix,r),e&&this.translate(-e[0],-e[1]),this}mirrorX(){return this._matrix=It(this._matrix,[1,0,0,0,-1,0,0,0,1]),this}mirrorY(){return this._matrix=It(this._matrix,[-1,0,0,0,1,0,0,0,1]),this}mirrorLine(t,e){const[s,n]=t,r=Math.atan2(n,s);return e&&this.translate(e[0],e[1]),this.rotate(r),this.mirrorX(),this.rotate(-r),e&&this.translate(-e[0],-e[1]),this}mirrorCenter(t){return t&&this.translate(t[0],t[1]),this._matrix=It(this._matrix,[-1,0,0,0,-1,0,0,0,1]),t&&this.translate(-t[0],-t[1]),this}scale(t,e){return e&&this.translate(e[0],e[1]),this._matrix=It(this._matrix,[t,0,0,0,t,0,0,0,1]),e&&this.translate(-e[0],-e[1]),this}transform(t){const[e,s]=t,[n,r,o,h,l,a]=this._matrix;return[n*e+r*s+o,h*e+l*s+a]}transformAngle(t){const[e,s]=this.transform([Math.cos(t),Math.sin(t)]),[n,r]=this.transform([0,0]);return Math.atan2(s-r,e-n)}keepsOrientation(){const[t,,,,e]=this._matrix;return t*e>0}scaleFactor(){const[t,,,e]=this._matrix;return Math.sqrt(t*t+e*e)}};class $t{translateX(t){const e=new at().translate(t,0);return this.transform(e)}translateY(t){const e=new at().translate(0,t);return this.transform(e)}translate(t,e){const s=new at().translate(t,e);return this.transform(s)}translateTo([t,e]){const s=new at().translate(t,e);return this.transform(s)}rotate(t,e){const s=new at().rotate(t*St,e);return this.transform(s)}scale(t,e){const s=new at().scale(t,e);return this.transform(s)}mirrorCenter(t){const e=new at().mirrorCenter(t);return this.transform(e)}mirror(t="x",e){const s=new at;return t==="x"?s.mirrorX():t==="y"?s.mirrorY():s.mirrorLine(t,e),this.transform(s)}}let Xt=class extends $t{constructor(t,e){super(),A(this,"precision",1e-9),this.firstPoint=t,this.lastPoint=e,this.firstPoint=t,this.lastPoint=e}get repr(){return`${this.segmentType} ${H(this.firstPoint)} - ${H(this.lastPoint)}`}get info(){return this.repr}[Symbol.for("nodejs.util.inspect.custom")](){return this.repr}},B=class Ot extends Xt{constructor(){super(...arguments),A(this,"segmentType","LINE"),A(this,"_V",null),A(this,"_slope",null),A(this,"_yIntercept",null),A(this,"_boundingBox",null)}isValidParameter(t){const e=this.length*this.precision;return t>=-e&&1-t>=-e}paramPoint(t){return O(this.firstPoint,G(this.V,t))}get length(){return N(this.firstPoint,this.lastPoint)}get squareLength(){return kt(this.firstPoint,this.lastPoint)}get V(){return this._V===null&&(this._V=q(this.lastPoint,this.firstPoint)),this._V}get slope(){if(this._slope===null){const[t,e]=this.V;this._slope=e/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 O(this.firstPoint,G(this.V,.5))}isSame(t){return t instanceof Ot?y(this.firstPoint,t.firstPoint)&&y(this.lastPoint,t.lastPoint)||y(this.lastPoint,t.firstPoint)&&y(this.firstPoint,t.lastPoint):!1}clone(){return new Ot(this.firstPoint,this.lastPoint)}reverse(){return new Ot(this.lastPoint,this.firstPoint)}get boundingBox(){return this._boundingBox===null&&(this._boundingBox=new Yt(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 e=q(t,this.firstPoint),s=Ae(e,this.V)/this.squareLength;if(s<0)return N(t,this.firstPoint);if(s>1)return N(t,this.lastPoint);const n=this.paramPoint(s);return N(t,n)}isOnSegment(t){if(y(t,this.firstPoint,this.precision))return!0;const e=q(t,this.firstPoint);if(!Ee(this.V,e))return!1;const s=Ae(e,this.V)/this.squareLength;return this.isValidParameter(s)}gradientAt(t){return this.V}tangentAt(t){if(!this.isOnSegment(t))throw new Error("Point is not on segment");return W(this.V)}get normalVector(){return tt(W(this.V))}get tangentAtFirstPoint(){return W(this.V)}get tangentAtLastPoint(){return W(this.V)}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t],e.forEach(l=>{if(!this.isOnSegment(l))throw new Error(`Point ${H(l)} is not on segment ${this.repr}`)});const s=[this.firstPoint,...e,this.lastPoint],n=Pt(s),r=this.lastPoint[0]-this.firstPoint[0];let o=Math.sign(r),h=0;return Math.abs(r)<this.precision&&(o=Math.sign(this.lastPoint[1]-this.firstPoint[1]),h=1),n.sort((l,a)=>o*(l[h]-a[h])),n.flatMap((l,a)=>a===n.length-1?[]:new Ot(l,n[a+1]))}transform(t){return new Ot(t.transform(this.firstPoint),t.transform(this.lastPoint))}};function di(i){return Array.from(Array(i).keys())}function mt(i){const t=Math.min(...i.map(e=>e.length));return di(t).map(e=>i.map(s=>s[e]))}function Ct(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 te(i,t,e,s=1e-9){let n=t-i;return e&&(n=-n),n<0&&(n+=2*Math.PI),n>2*Math.PI-s?0:n}const ee=(i,t,e)=>{const s=bt(i.V,t.V),n=wt(i.V),r=wt(t.V),o=e?e*e:i.precision*t.precision;if(s*s<n*r*o)return"parallel";const h=q(t.firstPoint,i.firstPoint),l=bt(h,t.V)/s,a=bt(h,i.V)/s;return{intersectionParam1:l,intersectionParam2:a}};function Je(i,t,e=!1,s){const n=ee(i,t,s);if(n==="parallel"){if(!e)return null;if(i.isSame(t))return i;const h=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(l=>l!==null)).sort((l,a)=>l[0]-a[0]);if(h.length===0||h.length===1)return null;if(h.length===2)return new B(h[0],h[1]);throw console.error(h),new Error("Unexpected number of points while intersecting parallel lines")}const{intersectionParam1:r,intersectionParam2:o}=n;return!i.isValidParameter(r)||!t.isValidParameter(o)?null:i.paramPoint(r)}const se=(i,t)=>{const e=q(i,t);return Qe(e)};let L=class Nt extends Xt{constructor(t,e,s,n=!1,{ignoreChecks:r=!1}={}){if(super(t,e),A(this,"segmentType","ARC"),A(this,"center"),A(this,"clockwise"),A(this,"_coefficients",null),A(this,"_angularLength",null),A(this,"_radius",null),A(this,"_firstAngle",null),A(this,"_lastAngle",null),A(this,"_boundingBox",null),this.center=s,this.clockwise=n,!r){if(y(t,e))throw new Error("Invalid arc, cannot be a full circle");if(Math.abs(this.radius-N(this.lastPoint,this.center))>this.precision)throw new Error(`Invalid arc, radius does not match between ${H(t)} and ${H(e)}} (center ${H(s)})`)}}get info(){return`ARC(${H(this.firstPoint)}, ${H(this.lastPoint)}, ${H(this.center)}, ${this.clockwise?"CW":"CCW"})`}get coefficients(){if(this._coefficients===null){const[t,e]=this.center,s=this.radius*this.radius;this._coefficients={x2:1/s,xy:0,y2:1/s,x:-(2*t)/s,y:-(2*e)/s,c:(t*t+e*e-s)/s}}return this._coefficients}isValidParameter(t){return 1-t>=-this.precision&&t>=-this.precision}angleToParam(t){return te(this.firstAngle,Ct(t),this.clockwise)/this.angularLength}get angularLength(){return this._angularLength||(this._angularLength=te(this.firstAngle,this.lastAngle,this.clockwise)),this._angularLength}paramPoint(t){return O(this.center,Mt(this.radius,this.firstAngle+t*this.angularLength*(this.clockwise?-1:1)))}pointToParam(t){const[e,s]=se(t,this.center);if(Math.abs(e-this.radius)>this.precision)throw new Error(`Point ${H(t)} is not on segment ${this.repr}`);const n=this.angleToParam(s);if(!this.isValidParameter(n))throw new Error(`Point ${H(t)} is not on segment ${this.repr}`);return n}get radius(){return this._radius===null&&(this._radius=N(this.firstPoint,this.center)),this._radius}get firstAngle(){if(this._firstAngle===null){const[t,e]=q(this.firstPoint,this.center);this._firstAngle=Ct(Math.atan2(e,t))}return this._firstAngle}get lastAngle(){if(this._lastAngle===null){const[t,e]=q(this.lastPoint,this.center);this._lastAngle=Ct(Math.atan2(e,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 Nt)||!y(this.center,t.center)?!1:y(this.firstPoint,t.firstPoint)&&y(this.lastPoint,t.lastPoint)&&this.clockwise===t.clockwise||y(this.lastPoint,t.firstPoint)&&y(this.firstPoint,t.lastPoint)&&this.clockwise===!t.clockwise}clone(){return new Nt(this.firstPoint,this.lastPoint,this.center,this.clockwise)}reverse(){return new Nt(this.lastPoint,this.firstPoint,this.center,!this.clockwise)}get boundingBox(){if(this._boundingBox===null){const t=this.radius+this.precision,e=s=>this.isValidParameter(this.angleToParam(s));this._boundingBox=new Yt(e(Math.PI)?this.center[0]-t:Math.min(this.firstPoint[0],this.lastPoint[0])-this.precision,e(Math.PI*1.5)?this.center[1]-t:Math.min(this.firstPoint[1],this.lastPoint[1])-this.precision,e(0)?this.center[0]+t:Math.max(this.firstPoint[0],this.lastPoint[0])+this.precision,e(Math.PI/2)?this.center[1]+t:Math.max(this.firstPoint[1],this.lastPoint[1])+this.precision)}return this._boundingBox}distanceFrom(t){const[e,s]=se(t,this.center);return this.isValidParameter(this.angleToParam(s))?Math.abs(e-this.radius):Math.sqrt(Math.min(kt(t,this.firstPoint),kt(t,this.lastPoint)))}isOnSegment(t){if(y(t,this.firstPoint)||y(t,this.lastPoint))return!0;const[e,s]=se(t,this.center);if(Math.abs(e-this.radius)>this.precision)return!1;const n=this.angleToParam(s);return this.isValidParameter(n)}gradientAt(t){const e=this.firstAngle+t*this.angularLength*(this.clockwise?-1:1),s=this.radius*this.angularLength,n=-s*Math.sin(e),r=s*Math.cos(e);return this.clockwise?[-n,-r]:[n,r]}tangentAt(t){const[e,s]=se(t,this.center);if(Math.abs(e-this.radius)>this.precision)throw new Error("Point is not on the arc");const n=this.angleToParam(s);if(!this.isValidParameter(n))throw new Error("Point is not on the arc");const r=Mt(1,s);return(this.clockwise?Dt:tt)(W(r))}get tangentAtFirstPoint(){const t=Mt(1,this.firstAngle);return(this.clockwise?Dt:tt)(W(t))}get tangentAtLastPoint(){const t=Mt(1,this.lastAngle);return(this.clockwise?Dt:tt)(W(t))}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const s=[0,1,...e.map(o=>this.pointToParam(o))],n=new Map(mt([s,[this.firstPoint,this.lastPoint,...e]]));s.sort((o,h)=>o-h);let r=null;return s.flatMap((o,h)=>{if(h===s.length-1)return[];const l=s[h+1];if(l-o<this.precision)return r===null&&(r=o),[];const a=r===null?o:r,c=new Nt(n.get(a)||this.paramPoint(a),n.get(l)||this.paramPoint(l),this.center,this.clockwise);return r=null,c})}transform(t){return new Nt(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.center),t.keepsOrientation()?this.clockwise:!this.clockwise)}};function Pi(i,t,e){const s=new B(t,i),n=new B(t,e),r=tt(s.tangentAtFirstPoint),o=tt(n.tangentAtLastPoint),h=ee({firstPoint:s.midPoint,V:r,precision:1e-9},{firstPoint:n.midPoint,V:o,precision:1e-9});if(h==="parallel")throw new Error("Cannot create an arc from three colinear points");const l=bt(q(i,t),q(e,t))>0;return new L(i,e,O(s.midPoint,G(r,h.intersectionParam1)),l,{ignoreChecks:!0})}function Ke(i,t,e){const s=new B(t,i),n=tt(s.tangentAtFirstPoint),r=ee({firstPoint:s.midPoint,V:n,precision:1e-9},{firstPoint:i,V:tt(e),precision:1e-9});if(r==="parallel")throw new Error("Cannot create an arc from three colinear points");const o=O(s.midPoint,G(n,r.intersectionParam1)),h=bt(q(o,i),q(o,O(i,e)))<0;return new L(i,t,o,h,{ignoreChecks:!0})}const ts=1e-21,ie=1.618034;function es(i,t=0,e=1,s=110,n=1e3){let r,o,h,l,a,c,u,f,p,g,w,P,m;for(o=i(t),h=i(e),o<h&&([t,e]=[e,t],[o,h]=[h,o]),m=e+ie*(e-t),l=i(m),a=3,u=0;l<h;){if(f=(e-t)*(h-l),p=(e-m)*(h-o),g=p-f,Math.abs(g)<ts?r=2*ts:r=2*g,w=e-((e-m)*p-(e-t)*f)/r,P=e+s*(m-e),u>n)throw new Error("Too many iterations.");if(u+=1,(w-m)*(e-w)>0){if(c=i(w),a+=1,c<l)return t=e,e=w,o=h,h=c,[t,e,m,o,h,l,a];if(c>h)return m=w,l=c,[t,e,m,o,h,l,a];w=m+ie*(m-e),c=i(w),a+=1}else(w-P)*(P-m)>=0?(w=P,c=i(w),a+=1):(w-P)*(m-w)>0?(c=i(w),a+=1,c<l&&(e=m,m=w,w=m+ie*(m-e),h=l,l=c,c=i(w),a+=1)):(w=m+ie*(m-e),c=i(w),a+=1);t=e,e=m,m=w,o=h,h=l,l=c}return[t,e,m,o,h,l,a]}const ss=.381966,Mi=1e-11;class yi{constructor(t,e=148e-10,s=500){A(this,"xmin"),A(this,"fval"),A(this,"iter"),A(this,"funcalls"),A(this,"brack"),this.func=t,this.tol=e,this.maxiter=s,this.func=t,this.tol=e,this.maxiter=s,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,e,s,n,r,o,h;const l=this.func,a=this.brack;if(a===null)[r,o,h,t,e,s,n]=es(l);else if(a.length===2)[r,o,h,t,e,s,n]=es(l,a[0],a[1]);else if(a.length===3){if([r,o,h]=a,r>h&&([h,r]=[r,h]),!(r<o&&o<h))throw new Error("Not a bracketing interval.");if(t=l(r),e=l(o),s=l(h),!(e<t&&e<s))throw new Error("Not a bracketing interval.");n=3}else throw new Error("Bracketing interval must be length 2 or 3 sequence.");return[r,o,h,t,e,s,n]}optimize(){const t=this.func;let[e,s,n,,,,r]=this.getBracketInfo(),o=s,h=s,l=s,a=t(o),c=a,u=a,f,p;e<n?(f=e,p=n):(f=n,p=e);let g=0;r=1;let w=0,P=0,m;for(;w<this.maxiter;){const d=this.tol*Math.abs(o)+Mi,M=2*d,x=.5*(f+p);if(Math.abs(o-x)<M-.5*(p-f))break;if(Math.abs(g)<=d)o>=x?g=f-o:g=p-o,P=ss*g;else{const $=(o-h)*(u-c);let E=(o-l)*(u-a),b=(o-l)*E-(o-h)*$;E=2*(E-$),E>0&&(b=-b),E=Math.abs(E);const T=g;g=P,b>E*(f-o)&&b<E*(p-o)&&Math.abs(b)<Math.abs(.5*E*T)?(P=b*1/E,m=o+P,(m-f<M||p-m<M)&&(x-o>=0?P=d:P=-d)):(o>=x?g=f-o:g=p-o,P=ss*g)}Math.abs(P)<d?P>=0?m=o+d:m=o-d:m=o+P;const v=t(m);r+=1,v>u?(m<o?f=m:p=m,v<=a||h===o?(l=h,h=m,c=a,a=v):(v<=c||l===o||l===h)&&(l=m,c=v)):(m>=o?f=o:p=o,l=h,h=o,o=m,c=a,a=u,u=v),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 is(i,t=1e-8,e=1e3){const s=new yi(i,t,e);return s.setBracket([0,1]),s.run()}const bi=Object.prototype.toString;function lt(i){const t=bi.call(i);return t.endsWith("Array]")&&!t.includes("Big")}function xi(i){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!lt(i))throw new TypeError("input must be an array");if(i.length===0)throw new TypeError("input must not be empty");var e=t.fromIndex,s=e===void 0?0:e,n=t.toIndex,r=n===void 0?i.length:n;if(s<0||s>=i.length||!Number.isInteger(s))throw new Error("fromIndex must be a positive integer smaller than length");if(r<=s||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[s],h=s+1;h<r;h++)i[h]>o&&(o=i[h]);return o}function vi(i){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(!lt(i))throw new TypeError("input must be an array");if(i.length===0)throw new TypeError("input must not be empty");var e=t.fromIndex,s=e===void 0?0:e,n=t.toIndex,r=n===void 0?i.length:n;if(s<0||s>=i.length||!Number.isInteger(s))throw new Error("fromIndex must be a positive integer smaller than length");if(r<=s||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[s],h=s+1;h<r;h++)i[h]<o&&(o=i[h]);return o}function ns(i){var t=arguments.length>1&&arguments[1]!==void 0?arguments[1]:{};if(lt(i)){if(i.length===0)throw new TypeError("input must not be empty")}else throw new TypeError("input must be an array");var e;if(t.output!==void 0){if(!lt(t.output))throw new TypeError("output option must be an array if specified");e=t.output}else e=new Array(i.length);var s=vi(i),n=xi(i);if(s===n)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?s:0:r,h=t.max,l=h===void 0?t.autoMinMax?n:1:h;if(o>=l)throw new RangeError("min option must be smaller than max option");for(var a=(l-o)/(n-s),c=0;c<i.length;c++)e[c]=(i[c]-s)*a+o;return e}const ne=" ".repeat(2),rs=" ".repeat(4);function Ai(){return os(this)}function os(i,t={}){const{maxRows:e=15,maxColumns:s=10,maxNumSize:n=8,padMinus:r="auto"}=t;return`${i.constructor.name} {
|
|
2
|
+
${ne}[
|
|
3
|
+
${rs}${Ei(i,e,s,n,r)}
|
|
4
|
+
${ne}]
|
|
5
|
+
${ne}rows: ${i.rows}
|
|
6
|
+
${ne}columns: ${i.columns}
|
|
7
|
+
}`}function Ei(i,t,e,s,n){const{rows:r,columns:o}=i,h=Math.min(r,t),l=Math.min(o,e),a=[];if(n==="auto"){n=!1;t:for(let c=0;c<h;c++)for(let u=0;u<l;u++)if(i.get(c,u)<0){n=!0;break t}}for(let c=0;c<h;c++){let u=[];for(let f=0;f<l;f++)u.push(Si(i.get(c,f),s,n));a.push(`${u.join(" ")}`)}return l!==o&&(a[a.length-1]+=` ... ${o-e} more columns`),h!==r&&a.push(`... ${r-t} more rows`),a.join(`
|
|
8
|
+
${rs}`)}function Si(i,t,e){return(i>=0&&e?` ${hs(i,t-1)}`:hs(i,t)).padEnd(t)}function hs(i,t){let e=i.toString();if(e.length<=t)return e;let s=i.toFixed(t);if(s.length>t&&(s=i.toFixed(Math.max(0,t-(s.length-t)))),s.length<=t&&!s.startsWith("0.000")&&!s.startsWith("-0.000"))return s;let n=i.toExponential(t);return n.length>t&&(n=i.toExponential(Math.max(0,t-(n.length-t)))),n.slice(0)}function ki(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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)+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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)+e.get(s,n));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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)-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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)-e.get(s,n));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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)*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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)*e.get(s,n));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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)/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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)/e.get(s,n));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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)%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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)%e.get(s,n));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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)&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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)&e.get(s,n));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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)|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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)|e.get(s,n));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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)^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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)^e.get(s,n));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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)<<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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)<<e.get(s,n));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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)>>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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)>>e.get(s,n));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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)>>>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 n=0;n<this.columns;n++)this.set(s,n,this.get(s,n)>>>e.get(s,n));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 n=0;n<this.columns;n++)this.set(s,n,Math.pow(this.get(s,n),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 n=0;n<this.columns;n++)this.set(s,n,Math.pow(this.get(s,n),e.get(s,n)));return this}}function gt(i,t,e){let s=e?i.rows:i.rows-1;if(t<0||t>s)throw new RangeError("Row index out of range")}function pt(i,t,e){let s=e?i.columns:i.columns-1;if(t<0||t>s)throw new RangeError("Column index out of range")}function _t(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 Lt(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 $i(i,t){if(!lt(t))throw new TypeError("row indices must be an array");for(let e=0;e<t.length;e++)if(t[e]<0||t[e]>=i.rows)throw new RangeError("row indices are out of range")}function Ci(i,t){if(!lt(t))throw new TypeError("column indices must be an array");for(let e=0;e<t.length;e++)if(t[e]<0||t[e]>=i.columns)throw new RangeError("column indices are out of range")}function as(i,t,e,s,n){if(arguments.length!==5)throw new RangeError("expected 4 arguments");if(oe("startRow",t),oe("endRow",e),oe("startColumn",s),oe("endColumn",n),t>e||s>n||t<0||t>=i.rows||e<0||e>=i.rows||s<0||s>=i.columns||n<0||n>=i.columns)throw new RangeError("Submatrix indices are out of range")}function re(i,t=0){let e=[];for(let s=0;s<i;s++)e.push(t);return e}function oe(i,t){if(typeof t!="number")throw new TypeError(`${i} must be a number`)}function Bt(i){if(i.isEmpty())throw new Error("Empty matrix has no elements to index")}function Ri(i){let t=re(i.rows);for(let e=0;e<i.rows;++e)for(let s=0;s<i.columns;++s)t[e]+=i.get(e,s);return t}function Ti(i){let t=re(i.columns);for(let e=0;e<i.rows;++e)for(let s=0;s<i.columns;++s)t[s]+=i.get(e,s);return t}function Fi(i){let t=0;for(let e=0;e<i.rows;e++)for(let s=0;s<i.columns;s++)t+=i.get(e,s);return t}function Ii(i){let t=re(i.rows,1);for(let e=0;e<i.rows;++e)for(let s=0;s<i.columns;++s)t[e]*=i.get(e,s);return t}function _i(i){let t=re(i.columns,1);for(let e=0;e<i.rows;++e)for(let s=0;s<i.columns;++s)t[s]*=i.get(e,s);return t}function Li(i){let t=1;for(let e=0;e<i.rows;e++)for(let s=0;s<i.columns;s++)t*=i.get(e,s);return t}function Bi(i,t,e){const s=i.rows,n=i.columns,r=[];for(let o=0;o<s;o++){let h=0,l=0,a=0;for(let c=0;c<n;c++)a=i.get(o,c)-e[o],h+=a,l+=a*a;t?r.push((l-h*h/n)/(n-1)):r.push((l-h*h/n)/n)}return r}function ji(i,t,e){const s=i.rows,n=i.columns,r=[];for(let o=0;o<n;o++){let h=0,l=0,a=0;for(let c=0;c<s;c++)a=i.get(c,o)-e[o],h+=a,l+=a*a;t?r.push((l-h*h/s)/(s-1)):r.push((l-h*h/s)/s)}return r}function qi(i,t,e){const s=i.rows,n=i.columns,r=s*n;let o=0,h=0,l=0;for(let a=0;a<s;a++)for(let c=0;c<n;c++)l=i.get(a,c)-e,o+=l,h+=l*l;return t?(h-o*o/r)/(r-1):(h-o*o/r)/r}function Vi(i,t){for(let e=0;e<i.rows;e++)for(let s=0;s<i.columns;s++)i.set(e,s,i.get(e,s)-t[e])}function Oi(i,t){for(let e=0;e<i.rows;e++)for(let s=0;s<i.columns;s++)i.set(e,s,i.get(e,s)-t[s])}function Ni(i,t){for(let e=0;e<i.rows;e++)for(let s=0;s<i.columns;s++)i.set(e,s,i.get(e,s)-t)}function zi(i){const t=[];for(let e=0;e<i.rows;e++){let s=0;for(let n=0;n<i.columns;n++)s+=Math.pow(i.get(e,n),2)/(i.columns-1);t.push(Math.sqrt(s))}return t}function Ui(i,t){for(let e=0;e<i.rows;e++)for(let s=0;s<i.columns;s++)i.set(e,s,i.get(e,s)/t[e])}function Yi(i){const t=[];for(let e=0;e<i.columns;e++){let s=0;for(let n=0;n<i.rows;n++)s+=Math.pow(i.get(n,e),2)/(i.rows-1);t.push(Math.sqrt(s))}return t}function Di(i,t){for(let e=0;e<i.rows;e++)for(let s=0;s<i.columns;s++)i.set(e,s,i.get(e,s)/t[s])}function Xi(i){const t=i.size-1;let e=0;for(let s=0;s<i.columns;s++)for(let n=0;n<i.rows;n++)e+=Math.pow(i.get(n,s),2)/t;return Math.sqrt(e)}function Hi(i,t){for(let e=0;e<i.rows;e++)for(let s=0;s<i.columns;s++)i.set(e,s,i.get(e,s)/t)}let Z=class Q{static from1DArray(t,e,s){if(t*e!==s.length)throw new RangeError("data length does not match given dimensions");let n=new R(t,e);for(let r=0;r<t;r++)for(let o=0;o<e;o++)n.set(r,o,s[r*e+o]);return n}static rowVector(t){let e=new R(1,t.length);for(let s=0;s<t.length;s++)e.set(0,s,t[s]);return e}static columnVector(t){let e=new R(t.length,1);for(let s=0;s<t.length;s++)e.set(s,0,t[s]);return e}static zeros(t,e){return new R(t,e)}static ones(t,e){return new R(t,e).fill(1)}static rand(t,e,s={}){if(typeof s!="object")throw new TypeError("options must be an object");const{random:n=Math.random}=s;let r=new R(t,e);for(let o=0;o<t;o++)for(let h=0;h<e;h++)r.set(o,h,n());return r}static randInt(t,e,s={}){if(typeof s!="object")throw new TypeError("options must be an object");const{min:n=0,max:r=1e3,random:o=Math.random}=s;if(!Number.isInteger(n))throw new TypeError("min must be an integer");if(!Number.isInteger(r))throw new TypeError("max must be an integer");if(n>=r)throw new RangeError("min must be smaller than max");let h=r-n,l=new R(t,e);for(let a=0;a<t;a++)for(let c=0;c<e;c++){let u=n+Math.round(o()*h);l.set(a,c,u)}return l}static eye(t,e,s){e===void 0&&(e=t),s===void 0&&(s=1);let n=Math.min(t,e),r=this.zeros(t,e);for(let o=0;o<n;o++)r.set(o,o,s);return r}static diag(t,e,s){let n=t.length;e===void 0&&(e=n),s===void 0&&(s=e);let r=Math.min(n,e,s),o=this.zeros(e,s);for(let h=0;h<r;h++)o.set(h,h,t[h]);return o}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let s=t.rows,n=t.columns,r=new R(s,n);for(let o=0;o<s;o++)for(let h=0;h<n;h++)r.set(o,h,Math.min(t.get(o,h),e.get(o,h)));return r}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let s=t.rows,n=t.columns,r=new this(s,n);for(let o=0;o<s;o++)for(let h=0;h<n;h++)r.set(o,h,Math.max(t.get(o,h),e.get(o,h)));return r}static checkMatrix(t){return Q.isMatrix(t)?t:new R(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 e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)t.call(this,e,s);return this}to1DArray(){let t=[];for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)t.push(this.get(e,s));return t}to2DArray(){let t=[];for(let e=0;e<this.rows;e++){t.push([]);for(let s=0;s<this.columns;s++)t[e].push(this.get(e,s))}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 e=0;e<=t;e++)if(this.get(t,e)!==this.get(e,t))return!1;return!0}return!1}isEchelonForm(){let t=0,e=0,s=-1,n=!0,r=!1;for(;t<this.rows&&n;){for(e=0,r=!1;e<this.columns&&r===!1;)this.get(t,e)===0?e++:this.get(t,e)===1&&e>s?(r=!0,s=e):(n=!1,r=!0);t++}return n}isReducedEchelonForm(){let t=0,e=0,s=-1,n=!0,r=!1;for(;t<this.rows&&n;){for(e=0,r=!1;e<this.columns&&r===!1;)this.get(t,e)===0?e++:this.get(t,e)===1&&e>s?(r=!0,s=e):(n=!1,r=!0);for(let o=e+1;o<this.rows;o++)this.get(t,o)!==0&&(n=!1);t++}return n}echelonForm(){let t=this.clone(),e=0,s=0;for(;e<t.rows&&s<t.columns;){let n=e;for(let r=e;r<t.rows;r++)t.get(r,s)>t.get(n,s)&&(n=r);if(t.get(n,s)===0)s++;else{t.swapRows(e,n);let r=t.get(e,s);for(let o=s;o<t.columns;o++)t.set(e,o,t.get(e,o)/r);for(let o=e+1;o<t.rows;o++){let h=t.get(o,s)/t.get(e,s);t.set(o,s,0);for(let l=s+1;l<t.columns;l++)t.set(o,l,t.get(o,l)-t.get(e,l)*h)}e++,s++}}return t}reducedEchelonForm(){let t=this.echelonForm(),e=t.columns,s=t.rows,n=s-1;for(;n>=0;)if(t.maxRow(n)===0)n--;else{let r=0,o=!1;for(;r<s&&o===!1;)t.get(n,r)===1?o=!0:r++;for(let h=0;h<n;h++){let l=t.get(h,r);for(let a=r;a<e;a++){let c=t.get(h,a)-l*t.get(n,a);t.set(h,a,c)}}n--}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:e=1,columns:s=1}=t;if(!Number.isInteger(e)||e<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(s)||s<=0)throw new TypeError("columns must be a positive integer");let n=new R(this.rows*e,this.columns*s);for(let r=0;r<e;r++)for(let o=0;o<s;o++)n.setSubMatrix(this,this.rows*r,this.columns*o);return n}fill(t){for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,t);return this}neg(){return this.mulS(-1)}getRow(t){gt(this,t);let e=[];for(let s=0;s<this.columns;s++)e.push(this.get(t,s));return e}getRowVector(t){return R.rowVector(this.getRow(t))}setRow(t,e){gt(this,t),e=_t(this,e);for(let s=0;s<this.columns;s++)this.set(t,s,e[s]);return this}swapRows(t,e){gt(this,t),gt(this,e);for(let s=0;s<this.columns;s++){let n=this.get(t,s);this.set(t,s,this.get(e,s)),this.set(e,s,n)}return this}getColumn(t){pt(this,t);let e=[];for(let s=0;s<this.rows;s++)e.push(this.get(s,t));return e}getColumnVector(t){return R.columnVector(this.getColumn(t))}setColumn(t,e){pt(this,t),e=Lt(this,e);for(let s=0;s<this.rows;s++)this.set(s,t,e[s]);return this}swapColumns(t,e){pt(this,t),pt(this,e);for(let s=0;s<this.rows;s++){let n=this.get(s,t);this.set(s,t,this.get(s,e)),this.set(s,e,n)}return this}addRowVector(t){t=_t(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)+t[s]);return this}subRowVector(t){t=_t(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)-t[s]);return this}mulRowVector(t){t=_t(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)*t[s]);return this}divRowVector(t){t=_t(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)/t[s]);return this}addColumnVector(t){t=Lt(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)+t[e]);return this}subColumnVector(t){t=Lt(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)-t[e]);return this}mulColumnVector(t){t=Lt(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)*t[e]);return this}divColumnVector(t){t=Lt(this,t);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)this.set(e,s,this.get(e,s)/t[e]);return this}mulRow(t,e){gt(this,t);for(let s=0;s<this.columns;s++)this.set(t,s,this.get(t,s)*e);return this}mulColumn(t,e){pt(this,t);for(let s=0;s<this.rows;s++)this.set(s,t,this.get(s,t)*e);return this}max(t){if(this.isEmpty())return NaN;switch(t){case"row":{const e=new Array(this.rows).fill(Number.NEGATIVE_INFINITY);for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)>e[s]&&(e[s]=this.get(s,n));return e}case"column":{const e=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)>e[n]&&(e[n]=this.get(s,n));return e}case void 0:{let e=this.get(0,0);for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)>e&&(e=this.get(s,n));return e}default:throw new Error(`invalid option: ${t}`)}}maxIndex(){Bt(this);let t=this.get(0,0),e=[0,0];for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)>t&&(t=this.get(s,n),e[0]=s,e[1]=n);return e}min(t){if(this.isEmpty())return NaN;switch(t){case"row":{const e=new Array(this.rows).fill(Number.POSITIVE_INFINITY);for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)<e[s]&&(e[s]=this.get(s,n));return e}case"column":{const e=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)<e[n]&&(e[n]=this.get(s,n));return e}case void 0:{let e=this.get(0,0);for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)<e&&(e=this.get(s,n));return e}default:throw new Error(`invalid option: ${t}`)}}minIndex(){Bt(this);let t=this.get(0,0),e=[0,0];for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)this.get(s,n)<t&&(t=this.get(s,n),e[0]=s,e[1]=n);return e}maxRow(t){if(gt(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let s=1;s<this.columns;s++)this.get(t,s)>e&&(e=this.get(t,s));return e}maxRowIndex(t){gt(this,t),Bt(this);let e=this.get(t,0),s=[t,0];for(let n=1;n<this.columns;n++)this.get(t,n)>e&&(e=this.get(t,n),s[1]=n);return s}minRow(t){if(gt(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let s=1;s<this.columns;s++)this.get(t,s)<e&&(e=this.get(t,s));return e}minRowIndex(t){gt(this,t),Bt(this);let e=this.get(t,0),s=[t,0];for(let n=1;n<this.columns;n++)this.get(t,n)<e&&(e=this.get(t,n),s[1]=n);return s}maxColumn(t){if(pt(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let s=1;s<this.rows;s++)this.get(s,t)>e&&(e=this.get(s,t));return e}maxColumnIndex(t){pt(this,t),Bt(this);let e=this.get(0,t),s=[0,t];for(let n=1;n<this.rows;n++)this.get(n,t)>e&&(e=this.get(n,t),s[0]=n);return s}minColumn(t){if(pt(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let s=1;s<this.rows;s++)this.get(s,t)<e&&(e=this.get(s,t));return e}minColumnIndex(t){pt(this,t),Bt(this);let e=this.get(0,t),s=[0,t];for(let n=1;n<this.rows;n++)this.get(n,t)<e&&(e=this.get(n,t),s[0]=n);return s}diag(){let t=Math.min(this.rows,this.columns),e=[];for(let s=0;s<t;s++)e.push(this.get(s,s));return e}norm(t="frobenius"){let e=0;if(t==="max")return this.max();if(t==="frobenius"){for(let s=0;s<this.rows;s++)for(let n=0;n<this.columns;n++)e=e+this.get(s,n)*this.get(s,n);return Math.sqrt(e)}else throw new RangeError(`unknown norm type: ${t}`)}cumulativeSum(){let t=0;for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)t+=this.get(e,s),this.set(e,s,t);return this}dot(t){Q.isMatrix(t)&&(t=t.to1DArray());let e=this.to1DArray();if(e.length!==t.length)throw new RangeError("vectors do not have the same size");let s=0;for(let n=0;n<e.length;n++)s+=e[n]*t[n];return s}mmul(t){t=R.checkMatrix(t);let e=this.rows,s=this.columns,n=t.columns,r=new R(e,n),o=new Float64Array(s);for(let h=0;h<n;h++){for(let l=0;l<s;l++)o[l]=t.get(l,h);for(let l=0;l<e;l++){let a=0;for(let c=0;c<s;c++)a+=this.get(l,c)*o[c];r.set(l,h,a)}}return r}strassen2x2(t){t=R.checkMatrix(t);let e=new R(2,2);const s=this.get(0,0),n=t.get(0,0),r=this.get(0,1),o=t.get(0,1),h=this.get(1,0),l=t.get(1,0),a=this.get(1,1),c=t.get(1,1),u=(s+a)*(n+c),f=(h+a)*n,p=s*(o-c),g=a*(l-n),w=(s+r)*c,P=(h-s)*(n+o),m=(r-a)*(l+c),d=u+g-w+m,M=p+w,x=f+g,v=u-f+p+P;return e.set(0,0,d),e.set(0,1,M),e.set(1,0,x),e.set(1,1,v),e}strassen3x3(t){t=R.checkMatrix(t);let e=new R(3,3);const s=this.get(0,0),n=this.get(0,1),r=this.get(0,2),o=this.get(1,0),h=this.get(1,1),l=this.get(1,2),a=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),M=t.get(2,1),x=t.get(2,2),v=(s+n+r-o-h-c-u)*P,$=(s-o)*(-p+P),E=h*(-f+p+w-P-m-d+x),b=(-s+o+h)*(f-p+P),T=(o+h)*(-f+p),V=s*f,Y=(-s+a+c)*(f-g+m),nt=(-s+a)*(g-m),_=(a+c)*(-f+g),C=(s+n+r-h-l-a-c)*m,k=c*(-f+g+w-P-m-d+M),S=(-r+c+u)*(P+d-M),vt=(r-u)*(P-M),I=r*d,j=(c+u)*(-d+M),F=(-r+h+l)*(m+d-x),z=(r-l)*(m-x),D=(h+l)*(-d+x),U=n*w,rt=l*M,yt=o*g,dt=a*p,At=u*x,Xe=V+I+U,He=v+b+T+V+S+I+j,Ge=V+Y+_+C+I+F+D,We=$+E+b+V+I+F+z,we=$+b+T+V+rt,de=I+F+z+D+yt,Pe=V+Y+nt+k+S+vt+I,Me=S+vt+I+j+dt,ye=V+Y+nt+_+At;return e.set(0,0,Xe),e.set(0,1,He),e.set(0,2,Ge),e.set(1,0,We),e.set(1,1,we),e.set(1,2,de),e.set(2,0,Pe),e.set(2,1,Me),e.set(2,2,ye),e}mmulStrassen(t){t=R.checkMatrix(t);let e=this.clone(),s=e.rows,n=e.columns,r=t.rows,o=t.columns;n!==r&&console.warn(`Multiplying ${s} x ${n} and ${r} x ${o} matrix: dimensions do not match.`);function h(u,f,p){let g=u.rows,w=u.columns;if(g===f&&w===p)return u;{let P=Q.zeros(f,p);return P=P.setSubMatrix(u,0,0),P}}let l=Math.max(s,r),a=Math.max(n,o);e=h(e,l,a),t=h(t,l,a);function c(u,f,p,g){if(p<=512||g<=512)return u.mmul(f);p%2===1&&g%2===1?(u=h(u,p+1,g+1),f=h(f,p+1,g+1)):p%2===1?(u=h(u,p+1,g),f=h(f,p+1,g)):g%2===1&&(u=h(u,p,g+1),f=h(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),M=u.subMatrix(0,w-1,P,u.columns-1),x=f.subMatrix(0,w-1,P,f.columns-1),v=u.subMatrix(w,u.rows-1,0,P-1),$=f.subMatrix(w,f.rows-1,0,P-1),E=u.subMatrix(w,u.rows-1,P,u.columns-1),b=f.subMatrix(w,f.rows-1,P,f.columns-1),T=c(Q.add(m,E),Q.add(d,b),w,P),V=c(Q.add(v,E),d,w,P),Y=c(m,Q.sub(x,b),w,P),nt=c(E,Q.sub($,d),w,P),_=c(Q.add(m,M),b,w,P),C=c(Q.sub(v,m),Q.add(d,x),w,P),k=c(Q.sub(M,E),Q.add($,b),w,P),S=Q.add(T,nt);S.sub(_),S.add(k);let vt=Q.add(Y,_),I=Q.add(V,nt),j=Q.sub(T,V);j.add(Y),j.add(C);let F=Q.zeros(2*S.rows,2*S.columns);return F=F.setSubMatrix(S,0,0),F=F.setSubMatrix(vt,S.rows,0),F=F.setSubMatrix(I,0,S.columns),F=F.setSubMatrix(j,S.rows,S.columns),F.subMatrix(0,p-1,0,g-1)}return c(e,t,l,a)}scaleRows(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{min:e=0,max:s=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(s))throw new TypeError("max must be a number");if(e>=s)throw new RangeError("min must be smaller than max");let n=new R(this.rows,this.columns);for(let r=0;r<this.rows;r++){const o=this.getRow(r);o.length>0&&ns(o,{min:e,max:s,output:o}),n.setRow(r,o)}return n}scaleColumns(t={}){if(typeof t!="object")throw new TypeError("options must be an object");const{min:e=0,max:s=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(s))throw new TypeError("max must be a number");if(e>=s)throw new RangeError("min must be smaller than max");let n=new R(this.rows,this.columns);for(let r=0;r<this.columns;r++){const o=this.getColumn(r);o.length&&ns(o,{min:e,max:s,output:o}),n.setColumn(r,o)}return n}flipRows(){const t=Math.ceil(this.columns/2);for(let e=0;e<this.rows;e++)for(let s=0;s<t;s++){let n=this.get(e,s),r=this.get(e,this.columns-1-s);this.set(e,s,r),this.set(e,this.columns-1-s,n)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let e=0;e<this.columns;e++)for(let s=0;s<t;s++){let n=this.get(s,e),r=this.get(this.rows-1-s,e);this.set(s,e,r),this.set(this.rows-1-s,e,n)}return this}kroneckerProduct(t){t=R.checkMatrix(t);let e=this.rows,s=this.columns,n=t.rows,r=t.columns,o=new R(e*n,s*r);for(let h=0;h<e;h++)for(let l=0;l<s;l++)for(let a=0;a<n;a++)for(let c=0;c<r;c++)o.set(n*h+a,r*l+c,this.get(h,l)*t.get(a,c));return o}kroneckerSum(t){if(t=R.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let e=this.rows,s=t.rows,n=this.kroneckerProduct(R.eye(s,s)),r=R.eye(e,e).kroneckerProduct(t);return n.add(r)}transpose(){let t=new R(this.columns,this.rows);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)t.set(s,e,this.get(e,s));return t}sortRows(t=ls){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=ls){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,s,n){as(this,t,e,s,n);let r=new R(e-t+1,n-s+1);for(let o=t;o<=e;o++)for(let h=s;h<=n;h++)r.set(o-t,h-s,this.get(o,h));return r}subMatrixRow(t,e,s){if(e===void 0&&(e=0),s===void 0&&(s=this.columns-1),e>s||e<0||e>=this.columns||s<0||s>=this.columns)throw new RangeError("Argument out of range");let n=new R(t.length,s-e+1);for(let r=0;r<t.length;r++)for(let o=e;o<=s;o++){if(t[r]<0||t[r]>=this.rows)throw new RangeError(`Row index out of range: ${t[r]}`);n.set(r,o-e,this.get(t[r],o))}return n}subMatrixColumn(t,e,s){if(e===void 0&&(e=0),s===void 0&&(s=this.rows-1),e>s||e<0||e>=this.rows||s<0||s>=this.rows)throw new RangeError("Argument out of range");let n=new R(s-e+1,t.length);for(let r=0;r<t.length;r++)for(let o=e;o<=s;o++){if(t[r]<0||t[r]>=this.columns)throw new RangeError(`Column index out of range: ${t[r]}`);n.set(o-e,r,this.get(o,t[r]))}return n}setSubMatrix(t,e,s){if(t=R.checkMatrix(t),t.isEmpty())return this;let n=e+t.rows-1,r=s+t.columns-1;as(this,e,n,s,r);for(let o=0;o<t.rows;o++)for(let h=0;h<t.columns;h++)this.set(e+o,s+h,t.get(o,h));return this}selection(t,e){$i(this,t),Ci(this,e);let s=new R(t.length,e.length);for(let n=0;n<t.length;n++){let r=t[n];for(let o=0;o<e.length;o++){let h=e[o];s.set(n,o,this.get(r,h))}}return s}trace(){let t=Math.min(this.rows,this.columns),e=0;for(let s=0;s<t;s++)e+=this.get(s,s);return e}clone(){let t=new R(this.rows,this.columns);for(let e=0;e<this.rows;e++)for(let s=0;s<this.columns;s++)t.set(e,s,this.get(e,s));return t}sum(t){switch(t){case"row":return Ri(this);case"column":return Ti(this);case void 0:return Fi(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return Ii(this);case"column":return _i(this);case void 0:return Li(this);default:throw new Error(`invalid option: ${t}`)}}mean(t){const e=this.sum(t);switch(t){case"row":{for(let s=0;s<this.rows;s++)e[s]/=this.columns;return e}case"column":{for(let s=0;s<this.columns;s++)e[s]/=this.rows;return e}case void 0:return e/this.size;default:throw new Error(`invalid option: ${t}`)}}variance(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");const{unbiased:s=!0,mean:n=this.mean(t)}=e;if(typeof s!="boolean")throw new TypeError("unbiased must be a boolean");switch(t){case"row":{if(!lt(n))throw new TypeError("mean must be an array");return Bi(this,s,n)}case"column":{if(!lt(n))throw new TypeError("mean must be an array");return ji(this,s,n)}case void 0:{if(typeof n!="number")throw new TypeError("mean must be a number");return qi(this,s,n)}default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,e){typeof t=="object"&&(e=t,t=void 0);const s=this.variance(t,e);if(t===void 0)return Math.sqrt(s);for(let n=0;n<s.length;n++)s[n]=Math.sqrt(s[n]);return s}center(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");const{center:s=this.mean(t)}=e;switch(t){case"row":{if(!lt(s))throw new TypeError("center must be an array");return Vi(this,s),this}case"column":{if(!lt(s))throw new TypeError("center must be an array");return Oi(this,s),this}case void 0:{if(typeof s!="number")throw new TypeError("center must be a number");return Ni(this,s),this}default:throw new Error(`invalid option: ${t}`)}}scale(t,e={}){if(typeof t=="object"&&(e=t,t=void 0),typeof e!="object")throw new TypeError("options must be an object");let s=e.scale;switch(t){case"row":{if(s===void 0)s=zi(this);else if(!lt(s))throw new TypeError("scale must be an array");return Ui(this,s),this}case"column":{if(s===void 0)s=Yi(this);else if(!lt(s))throw new TypeError("scale must be an array");return Di(this,s),this}case void 0:{if(s===void 0)s=Xi(this);else if(typeof s!="number")throw new TypeError("scale must be a number");return Hi(this,s),this}default:throw new Error(`invalid option: ${t}`)}}toString(t){return os(this,t)}};Z.prototype.klass="Matrix",typeof Symbol<"u"&&(Z.prototype[Symbol.for("nodejs.util.inspect.custom")]=Ai);function ls(i,t){return i-t}function Gi(i){return i.every(t=>typeof t=="number")}Z.random=Z.rand,Z.randomInt=Z.randInt,Z.diagonal=Z.diag,Z.prototype.diagonal=Z.prototype.diag,Z.identity=Z.eye,Z.prototype.negate=Z.prototype.neg,Z.prototype.tensorProduct=Z.prototype.kroneckerProduct;let R=class ci extends Z{constructor(t,e){if(super(),ci.isMatrix(t))return t.clone();if(Number.isInteger(t)&&t>=0)if(this.data=[],Number.isInteger(e)&&e>=0)for(let s=0;s<t;s++)this.data.push(new Float64Array(e));else throw new TypeError("nColumns must be a positive integer");else if(lt(t)){const s=t;if(t=s.length,e=t?s[0].length:0,typeof e!="number")throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let n=0;n<t;n++){if(s[n].length!==e)throw new RangeError("Inconsistent array dimensions");if(!Gi(s[n]))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(s[n]))}}else throw new TypeError("First argument must be a positive number or an array");this.rows=t,this.columns=e}set(t,e,s){return this.data[t][e]=s,this}get(t,e){return this.data[t][e]}removeRow(t){return gt(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return e===void 0&&(e=t,t=this.rows),gt(this,t,!0),e=Float64Array.from(_t(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){pt(this,t);for(let e=0;e<this.rows;e++){const s=new Float64Array(this.columns-1);for(let n=0;n<t;n++)s[n]=this.data[e][n];for(let n=t+1;n<this.columns;n++)s[n-1]=this.data[e][n];this.data[e]=s}return this.columns-=1,this}addColumn(t,e){typeof e>"u"&&(e=t,t=this.columns),pt(this,t,!0),e=Lt(this,e);for(let s=0;s<this.rows;s++){const n=new Float64Array(this.columns+1);let r=0;for(;r<t;r++)n[r]=this.data[s][r];for(n[r++]=e[s];r<this.columns+1;r++)n[r]=this.data[s][r-1];this.data[s]=n}return this.columns+=1,this}};ki(Z,R);class Wi extends Z{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,e,s){return this.data[t][e]=s,this}get(t,e){return this.data[t][e]}}function us(i,t){let e=0;return Math.abs(i)>Math.abs(t)?(e=t/i,Math.abs(i)*Math.sqrt(1+e*e)):t!==0?(e=i/t,Math.abs(t)*Math.sqrt(1+e*e)):0}class Zi{constructor(t,e={}){const{assumeSymmetric:s=!1}=e;if(t=Wi.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 n=t.columns,r=new R(n,n),o=new Float64Array(n),h=new Float64Array(n),l=t,a,c,u=!1;if(s?u=!0:u=t.isSymmetric(),u){for(a=0;a<n;a++)for(c=0;c<n;c++)r.set(a,c,l.get(a,c));Qi(n,h,o,r),Ji(n,h,o,r)}else{let f=new R(n,n),p=new Float64Array(n);for(c=0;c<n;c++)for(a=0;a<n;a++)f.set(a,c,l.get(a,c));Ki(n,f,p,r),tn(n,h,o,r,f)}this.n=n,this.e=h,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,e=this.e,s=this.d,n=new R(t,t),r,o;for(r=0;r<t;r++){for(o=0;o<t;o++)n.set(r,o,0);n.set(r,r,s[r]),e[r]>0?n.set(r,r+1,e[r]):e[r]<0&&n.set(r,r-1,e[r])}return n}}function Qi(i,t,e,s){let n,r,o,h,l,a,c,u;for(l=0;l<i;l++)e[l]=s.get(i-1,l);for(h=i-1;h>0;h--){for(u=0,o=0,a=0;a<h;a++)u=u+Math.abs(e[a]);if(u===0)for(t[h]=e[h-1],l=0;l<h;l++)e[l]=s.get(h-1,l),s.set(h,l,0),s.set(l,h,0);else{for(a=0;a<h;a++)e[a]/=u,o+=e[a]*e[a];for(n=e[h-1],r=Math.sqrt(o),n>0&&(r=-r),t[h]=u*r,o=o-n*r,e[h-1]=n-r,l=0;l<h;l++)t[l]=0;for(l=0;l<h;l++){for(n=e[l],s.set(l,h,n),r=t[l]+s.get(l,l)*n,a=l+1;a<=h-1;a++)r+=s.get(a,l)*e[a],t[a]+=s.get(a,l)*n;t[l]=r}for(n=0,l=0;l<h;l++)t[l]/=o,n+=t[l]*e[l];for(c=n/(o+o),l=0;l<h;l++)t[l]-=c*e[l];for(l=0;l<h;l++){for(n=e[l],r=t[l],a=l;a<=h-1;a++)s.set(a,l,s.get(a,l)-(n*t[a]+r*e[a]));e[l]=s.get(h-1,l),s.set(h,l,0)}}e[h]=o}for(h=0;h<i-1;h++){if(s.set(i-1,h,s.get(h,h)),s.set(h,h,1),o=e[h+1],o!==0){for(a=0;a<=h;a++)e[a]=s.get(a,h+1)/o;for(l=0;l<=h;l++){for(r=0,a=0;a<=h;a++)r+=s.get(a,h+1)*s.get(a,l);for(a=0;a<=h;a++)s.set(a,l,s.get(a,l)-r*e[a])}}for(a=0;a<=h;a++)s.set(a,h+1,0)}for(l=0;l<i;l++)e[l]=s.get(i-1,l),s.set(i-1,l,0);s.set(i-1,i-1,1),t[0]=0}function Ji(i,t,e,s){let n,r,o,h,l,a,c,u,f,p,g,w,P,m,d,M;for(o=1;o<i;o++)t[o-1]=t[o];t[i-1]=0;let x=0,v=0,$=Number.EPSILON;for(a=0;a<i;a++){for(v=Math.max(v,Math.abs(e[a])+Math.abs(t[a])),c=a;c<i&&!(Math.abs(t[c])<=$*v);)c++;if(c>a)do{for(n=e[a],u=(e[a+1]-n)/(2*t[a]),f=us(u,1),u<0&&(f=-f),e[a]=t[a]/(u+f),e[a+1]=t[a]*(u+f),p=e[a+1],r=n-e[a],o=a+2;o<i;o++)e[o]-=r;for(x=x+r,u=e[c],g=1,w=g,P=g,m=t[a+1],d=0,M=0,o=c-1;o>=a;o--)for(P=w,w=g,M=d,n=g*t[o],r=g*u,f=us(u,t[o]),t[o+1]=d*f,d=t[o]/f,g=u/f,u=g*e[o]-d*n,e[o+1]=r+d*(g*n+d*e[o]),l=0;l<i;l++)r=s.get(l,o+1),s.set(l,o+1,d*s.get(l,o)+g*r),s.set(l,o,g*s.get(l,o)-d*r);u=-d*M*P*m*t[a]/p,t[a]=d*u,e[a]=g*u}while(Math.abs(t[a])>$*v);e[a]=e[a]+x,t[a]=0}for(o=0;o<i-1;o++){for(l=o,u=e[o],h=o+1;h<i;h++)e[h]<u&&(l=h,u=e[h]);if(l!==o)for(e[l]=e[o],e[o]=u,h=0;h<i;h++)u=s.get(h,o),s.set(h,o,s.get(h,l)),s.set(h,l,u)}}function Ki(i,t,e,s){let n=0,r=i-1,o,h,l,a,c,u,f;for(u=n+1;u<=r-1;u++){for(f=0,a=u;a<=r;a++)f=f+Math.abs(t.get(a,u-1));if(f!==0){for(l=0,a=r;a>=u;a--)e[a]=t.get(a,u-1)/f,l+=e[a]*e[a];for(h=Math.sqrt(l),e[u]>0&&(h=-h),l=l-e[u]*h,e[u]=e[u]-h,c=u;c<i;c++){for(o=0,a=r;a>=u;a--)o+=e[a]*t.get(a,c);for(o=o/l,a=u;a<=r;a++)t.set(a,c,t.get(a,c)-o*e[a])}for(a=0;a<=r;a++){for(o=0,c=r;c>=u;c--)o+=e[c]*t.get(a,c);for(o=o/l,c=u;c<=r;c++)t.set(a,c,t.get(a,c)-o*e[c])}e[u]=f*e[u],t.set(u,u-1,f*h)}}for(a=0;a<i;a++)for(c=0;c<i;c++)s.set(a,c,a===c?1:0);for(u=r-1;u>=n+1;u--)if(t.get(u,u-1)!==0){for(a=u+1;a<=r;a++)e[a]=t.get(a,u-1);for(c=u;c<=r;c++){for(h=0,a=u;a<=r;a++)h+=e[a]*s.get(a,c);for(h=h/e[u]/t.get(u,u-1),a=u;a<=r;a++)s.set(a,c,s.get(a,c)+h*e[a])}}}function tn(i,t,e,s,n){let r=i-1,o=0,h=i-1,l=Number.EPSILON,a=0,c=0,u=0,f=0,p=0,g=0,w=0,P=0,m,d,M,x,v,$,E,b,T,V,Y,nt,_,C,k;for(m=0;m<i;m++)for((m<o||m>h)&&(e[m]=n.get(m,m),t[m]=0),d=Math.max(m-1,0);d<i;d++)c=c+Math.abs(n.get(m,d));for(;r>=o;){for(x=r;x>o&&(g=Math.abs(n.get(x-1,x-1))+Math.abs(n.get(x,x)),g===0&&(g=c),!(Math.abs(n.get(x,x-1))<l*g));)x--;if(x===r)n.set(r,r,n.get(r,r)+a),e[r]=n.get(r,r),t[r]=0,r--,P=0;else if(x===r-1){if(E=n.get(r,r-1)*n.get(r-1,r),u=(n.get(r-1,r-1)-n.get(r,r))/2,f=u*u+E,w=Math.sqrt(Math.abs(f)),n.set(r,r,n.get(r,r)+a),n.set(r-1,r-1,n.get(r-1,r-1)+a),b=n.get(r,r),f>=0){for(w=u>=0?u+w:u-w,e[r-1]=b+w,e[r]=e[r-1],w!==0&&(e[r]=b-E/w),t[r-1]=0,t[r]=0,b=n.get(r,r-1),g=Math.abs(b)+Math.abs(w),u=b/g,f=w/g,p=Math.sqrt(u*u+f*f),u=u/p,f=f/p,d=r-1;d<i;d++)w=n.get(r-1,d),n.set(r-1,d,f*w+u*n.get(r,d)),n.set(r,d,f*n.get(r,d)-u*w);for(m=0;m<=r;m++)w=n.get(m,r-1),n.set(m,r-1,f*w+u*n.get(m,r)),n.set(m,r,f*n.get(m,r)-u*w);for(m=o;m<=h;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)}else e[r-1]=b+u,e[r]=b+u,t[r-1]=w,t[r]=-w;r=r-2,P=0}else{if(b=n.get(r,r),T=0,E=0,x<r&&(T=n.get(r-1,r-1),E=n.get(r,r-1)*n.get(r-1,r)),P===10){for(a+=b,m=o;m<=r;m++)n.set(m,m,n.get(m,m)-b);g=Math.abs(n.get(r,r-1))+Math.abs(n.get(r-1,r-2)),b=T=.75*g,E=-.4375*g*g}if(P===30&&(g=(T-b)/2,g=g*g+E,g>0)){for(g=Math.sqrt(g),T<b&&(g=-g),g=b-E/((T-b)/2+g),m=o;m<=r;m++)n.set(m,m,n.get(m,m)-g);a+=g,b=T=E=.964}for(P=P+1,v=r-2;v>=x&&(w=n.get(v,v),p=b-w,g=T-w,u=(p*g-E)/n.get(v+1,v)+n.get(v,v+1),f=n.get(v+1,v+1)-w-p-g,p=n.get(v+2,v+1),g=Math.abs(u)+Math.abs(f)+Math.abs(p),u=u/g,f=f/g,p=p/g,!(v===x||Math.abs(n.get(v,v-1))*(Math.abs(f)+Math.abs(p))<l*(Math.abs(u)*(Math.abs(n.get(v-1,v-1))+Math.abs(w)+Math.abs(n.get(v+1,v+1))))));)v--;for(m=v+2;m<=r;m++)n.set(m,m-2,0),m>v+2&&n.set(m,m-3,0);for(M=v;M<=r-1&&(C=M!==r-1,M!==v&&(u=n.get(M,M-1),f=n.get(M+1,M-1),p=C?n.get(M+2,M-1):0,b=Math.abs(u)+Math.abs(f)+Math.abs(p),b!==0&&(u=u/b,f=f/b,p=p/b)),b!==0);M++)if(g=Math.sqrt(u*u+f*f+p*p),u<0&&(g=-g),g!==0){for(M!==v?n.set(M,M-1,-g*b):x!==v&&n.set(M,M-1,-n.get(M,M-1)),u=u+g,b=u/g,T=f/g,w=p/g,f=f/u,p=p/u,d=M;d<i;d++)u=n.get(M,d)+f*n.get(M+1,d),C&&(u=u+p*n.get(M+2,d),n.set(M+2,d,n.get(M+2,d)-u*w)),n.set(M,d,n.get(M,d)-u*b),n.set(M+1,d,n.get(M+1,d)-u*T);for(m=0;m<=Math.min(r,M+3);m++)u=b*n.get(m,M)+T*n.get(m,M+1),C&&(u=u+w*n.get(m,M+2),n.set(m,M+2,n.get(m,M+2)-u*p)),n.set(m,M,n.get(m,M)-u),n.set(m,M+1,n.get(m,M+1)-u*f);for(m=o;m<=h;m++)u=b*s.get(m,M)+T*s.get(m,M+1),C&&(u=u+w*s.get(m,M+2),s.set(m,M+2,s.get(m,M+2)-u*p)),s.set(m,M,s.get(m,M)-u),s.set(m,M+1,s.get(m,M+1)-u*f)}}}if(c!==0){for(r=i-1;r>=0;r--)if(u=e[r],f=t[r],f===0)for(x=r,n.set(r,r,1),m=r-1;m>=0;m--){for(E=n.get(m,m)-u,p=0,d=x;d<=r;d++)p=p+n.get(m,d)*n.get(d,r);if(t[m]<0)w=E,g=p;else if(x=m,t[m]===0?n.set(m,r,E!==0?-p/E:-p/(l*c)):(b=n.get(m,m+1),T=n.get(m+1,m),f=(e[m]-u)*(e[m]-u)+t[m]*t[m],$=(b*g-w*p)/f,n.set(m,r,$),n.set(m+1,r,Math.abs(b)>Math.abs(w)?(-p-E*$)/b:(-g-T*$)/w)),$=Math.abs(n.get(m,r)),l*$*$>1)for(d=m;d<=r;d++)n.set(d,r,n.get(d,r)/$)}else if(f<0)for(x=r-1,Math.abs(n.get(r,r-1))>Math.abs(n.get(r-1,r))?(n.set(r-1,r-1,f/n.get(r,r-1)),n.set(r-1,r,-(n.get(r,r)-u)/n.get(r,r-1))):(k=he(0,-n.get(r-1,r),n.get(r-1,r-1)-u,f),n.set(r-1,r-1,k[0]),n.set(r-1,r,k[1])),n.set(r,r-1,0),n.set(r,r,1),m=r-2;m>=0;m--){for(V=0,Y=0,d=x;d<=r;d++)V=V+n.get(m,d)*n.get(d,r-1),Y=Y+n.get(m,d)*n.get(d,r);if(E=n.get(m,m)-u,t[m]<0)w=E,p=V,g=Y;else if(x=m,t[m]===0?(k=he(-V,-Y,E,f),n.set(m,r-1,k[0]),n.set(m,r,k[1])):(b=n.get(m,m+1),T=n.get(m+1,m),nt=(e[m]-u)*(e[m]-u)+t[m]*t[m]-f*f,_=(e[m]-u)*2*f,nt===0&&_===0&&(nt=l*c*(Math.abs(E)+Math.abs(f)+Math.abs(b)+Math.abs(T)+Math.abs(w))),k=he(b*p-w*V+f*Y,b*g-w*Y-f*V,nt,_),n.set(m,r-1,k[0]),n.set(m,r,k[1]),Math.abs(b)>Math.abs(w)+Math.abs(f)?(n.set(m+1,r-1,(-V-E*n.get(m,r-1)+f*n.get(m,r))/b),n.set(m+1,r,(-Y-E*n.get(m,r)-f*n.get(m,r-1))/b)):(k=he(-p-T*n.get(m,r-1),-g-T*n.get(m,r),w,f),n.set(m+1,r-1,k[0]),n.set(m+1,r,k[1]))),$=Math.max(Math.abs(n.get(m,r-1)),Math.abs(n.get(m,r))),l*$*$>1)for(d=m;d<=r;d++)n.set(d,r-1,n.get(d,r-1)/$),n.set(d,r,n.get(d,r)/$)}for(m=0;m<i;m++)if(m<o||m>h)for(d=m;d<i;d++)s.set(m,d,n.get(m,d));for(d=i-1;d>=o;d--)for(m=o;m<=h;m++){for(w=0,M=o;M<=Math.min(d,h);M++)w=w+s.get(m,M)*n.get(M,d);s.set(m,d,w)}}}function he(i,t,e,s){let n,r;return Math.abs(e)>Math.abs(s)?(n=s/e,r=e+n*s,[(i+n*t)/r,(t-n*i)/r]):(n=e/s,r=s+n*e,[(n*i+t)/r,(n*t-i)/r])}function ae(i,t,e){const s=i/e,n=t/e;if(!(isFinite(s)&&isFinite(n))){const l=-i/t;return isFinite(l)?[l]:i==0&&t==0?[0]:[]}const r=n*n-4*s;let o=0;if(isFinite(r)){if(r<0)return[];if(r==0)return[-.5*n];o=-.5*(n+jt(Math.sqrt(r),n))}else o=-n;const h=s/o;return isFinite(h)?h>o?[o,h]:[h,o]:[o]}function cs(i,t,e,s){const n=e/(3*s),r=t/(3*s),o=i/s;if(!(isFinite(o)&&isFinite(r)&&isFinite(n)))return ae(i,t,e);const h=-n*n+r,l=-r*n+o,a=n*o-r*r,c=4*h*a-l*l,u=-2*n*h+l;if(c<0){const f=Math.sqrt(-.25*c),p=-.5*u;return[Math.cbrt(p+f)+Math.cbrt(p-f)-n]}else if(c==0){const f=jt(Math.sqrt(-h),u);return[f-n,-2*f-n]}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(-h);return[m*p-n,m*w-n,m*P-n]}}function en(i,t,e,s,n){if(n==0)return cs(i,t,e,s);const r=s/n,o=e/n,h=t/n,l=i/n;let a=fs(r,o,h,l,!1);if(a!==null)return a;const c=716e74;for(let u=0;u<2;u++)if(a=fs(r/c,o/(c*c),h/(c*c*c),l/(c*c*c*c),u!=0),a!==null){for(let f=0;f<a.length;f++)a[f]*=c;return a}return[]}function Rt(i,t){return t==0?Math.abs(i):Math.abs((i-t)/t)}function fs(i,t,e,s,n){const r=sn(i,t,e,s,n);if(r!==null&&r.length==4){let o=[];for(let h=0;h<2;h++){const l=r[h*2],a=r[h*2+1];o=o.concat(ae(a,l,1))}return o}return null}function sn(i,t,e,s,n){function r(I,j,F,z){const D=Rt(I+F,i),U=Rt(j+I*F+z,t),rt=Rt(j*F+I*z,e);return D+U+rt}function o(I,j,F,z){return r(I,j,F,z)+Rt(j*z,s)}const h=9*i*i-24*t,l=h>=0?-2*t/(3*i+jt(Math.sqrt(h),i)):-.25*i,a=i+4*l,c=t+3*l*(i+2*l),u=e+l*(2*t+l*(3*i+4*l)),f=s+l*(e+l*(t+l*(i+l)));let p=0,g=0;const w=349e100;if(n){const I=a/w,j=c/w,F=u/w,z=f/w;p=I*F-4/w*z-1/3*j*j,g=(I*F-8/w*z-2/9*j*j)*(1/3)*j-F*(F/w)-I*I*z}else p=a*u-4*f-1/3*c*c,g=(a*u+8*f-2/9*c*c)*(1/3)*c-u*u-a*a*f;if(!isFinite(p)&&isFinite(g))return null;let P=nn(p,g);n&&(P*=w);const m=i*.5,d=1/6*t+.5*P,M=e-i*d,x=2/3*t-P-m*m,v=.5*M/x,$=2*(s-d*d)/M,E=.5*M/$;let b=0,T=0,V=0;for(let I=0;I<3;I++){const j=I==1?E:x,F=I==0?v:$,z=Rt(j+m*m+2*d,t),D=Rt(2*(j*F+m*d),e),U=Rt(j*F*F+d*d,s),rt=z+D+U;(I==0||rt<V)&&(b=j,T=F,V=rt)}const Y=b,nt=T;let _=0,C=0,k=0,S=0;if(Y<0){const I=Math.sqrt(-Y);if(_=m+I,C=d+I*nt,k=m-I,S=d-I*nt,Math.abs(S)<Math.abs(C)?S=s/C:Math.abs(S)>Math.abs(C)&&(C=s/S),Math.abs(_)!=Math.abs(k)){let j=null,F=null;if(Math.abs(_)<Math.abs(k)){const D=(e-C*k)/S,U=(t-S-C)/k;j=[i-k,D,U],F=[k,k,k]}else{const D=(e-_*S)/C,U=(t-S-C)/_,rt=i-_;j=[_,_,_],F=[rt,D,U]}let z=0;for(let D=0;D<3;D++){const U=j[D],rt=F[D];if(isFinite(U)&&isFinite(rt)){const yt=r(U,C,rt,S);(D==0||yt<z)&&(_=U,k=rt,z=yt)}}}}else if(Y==0){const I=s-d*d;_=m,C=d+Math.sqrt(-I),k=m,S=d-Math.sqrt(-I),Math.abs(C)>Math.abs(S)?S=s/C:Math.abs(S)>Math.abs(C)&&(C=s/S)}else return[];let vt=o(_,C,k,S);for(let I=0;I<8&&vt!=0;I++){const j=C*S-s,F=C*k+_*S-e,z=C+_*k+S-t,D=_+k-i,U=_-k,rt=C*C-C*(k*U+2*S)+S*(_*U+S);if(rt==0)break;const yt=1/rt,dt=S-C,At=C*k-_*S,Xe=U*j+dt*F+At*z-(C*dt+_*At)*D,He=(_*U+dt)*j-C*(U*F+dt*z+At*D),Ge=-U*j-dt*F-At*z+(k*At+S*dt)*D,We=-(k*U+dt)*j+S*(U*F+dt*z+At*D),we=_-yt*Xe,de=C-yt*He,Pe=k-yt*Ge,Me=S-yt*We,ye=o(we,de,Pe,Me);if(ye<vt)_=we,C=de,k=Pe,S=Me,vt=ye;else break}return[_,C,k,S]}function nn(i,t){const e=-.3333333333333333*i,s=.5*t;let n,r=null;if((Math.abs(e)>=1e102||Math.abs(s)>=1e164)&&(Math.abs(e)<Math.abs(s)?r=1-e*(e/s)*(e/s):r=Math.sign(e)*(s/e*(s/e)/e-1)),r!==null&&s==0)i>0?n=0:n=Math.sqrt(-i);else if(r!==null?r<0:s*s<e*e*e){const a=r!==null?s/e/Math.sqrt(e):s/Math.sqrt(e*e*e);n=-2*Math.sqrt(e)*jt(Math.cos(Math.acos(Math.abs(a))*(1/3)),a)}else{let a;r!==null?Math.abs(e)<Math.abs(s)?a=-s*(1+Math.sqrt(r)):a=-s-jt(Math.sqrt(Math.abs(e))*e*Math.sqrt(r),s):a=Math.cbrt(-s-jt(Math.sqrt(s*s-e*e*e),s));const c=a==0?0:e/a;n=a+c}let o=n,h=(o*o+i)*o+t;if(Math.abs(h)<222045e-21*Math.max(o*o*o,i*o,t))return o;for(let a=0;a<8;a++){const c=3*o*o+i;if(c==0)break;const u=o-h/c,f=(u*u+i)*u+t;if(f==0)return u;if(Math.abs(f)>=Math.abs(h))break;o=u,h=f}return o}function jt(i,t){const e=Math.abs(i);return t<0?-e:e}function Se(i,t=1e-9){if(i[i.length-1]==0)return Se(i.slice(0,-1),t);const e=new R([i.slice(0,-1).reverse().map(n=>-n/i[i.length-1]),...Array.from({length:i.length-2},(n,r)=>{const o=new Array(i.length-1).fill(0);return o[r]=1,o})]),s=new Zi(e);return s.realEigenvalues.filter((n,r)=>Math.abs(s.imaginaryEigenvalues[r])<t)}const rn=(i,t,e)=>[i[0]*(1-e)+t[0]*e,i[1]*(1-e)+t[1]*e];function on(i){const t=[];for(let e=0;e<i.length-1;e++)t.push([i[e],i[e+1]]);return t}function ke(i,t){const e=on(i).map(([s,n])=>rn(s,n,t));return i.length===2?[e]:[...ke(e,t),e]}let et=class zt extends Xt{constructor(t,e,s,n){super(t,e),A(this,"segmentType","CUBIC_BEZIER"),A(this,"firstControlPoint"),A(this,"lastControlPoint"),A(this,"_boundingBox",null),A(this,"_polynomialCoefficients",null),this.firstControlPoint=s,this.lastControlPoint=n}get midPoint(){return this.paramPoint(.5)}get boundingBox(){if(this._boundingBox===null){const t=e=>{const[s,n,r,o]=[this.firstPoint[e],this.firstControlPoint[e],this.lastControlPoint[e],this.lastPoint[e]],h=-s+3*n-3*r+o,l=2*s-4*n+2*r,a=-s+n;return ae(a,l,h).filter(c=>c>=-this.precision&&c<=1+this.precision)};this._boundingBox=xe([this.firstPoint,...t(0).map(e=>this.paramPoint(e)),...t(1).map(e=>this.paramPoint(e)),this.lastPoint]).grow(this.precision)}return this._boundingBox}clone(){return new zt(this.firstPoint,this.lastPoint,this.firstControlPoint,this.lastControlPoint)}reverse(){return new zt(this.lastPoint,this.firstPoint,this.lastControlPoint,this.firstControlPoint)}isSame(t){return t.segmentType!=="CUBIC_BEZIER"?!1:y(this.firstPoint,t.firstPoint)&&y(this.lastPoint,t.lastPoint)&&y(this.firstControlPoint,t.firstControlPoint)&&y(this.lastControlPoint,t.lastControlPoint)}distanceFrom(t){const e=is(s=>kt(this.paramPoint(s),t),this.precision);return e.argMin<-this.precision||e.argMin>1+this.precision?Math.min(N(this.firstPoint,t),N(this.lastPoint,t)):Math.sqrt(e.fMin)}isOnSegment(t){if(!this.boundingBox.contains(t))return!1;try{const e=this.pointToParam(t);return e>=-this.precision&&e<=1+this.precision}catch{return!1}}gradientAt(t){const e=1-t,s=e*e,n=t*t,r=3*s,o=6*e*t,h=3*n,l=q(this.firstControlPoint,this.firstPoint),a=q(this.lastControlPoint,this.firstControlPoint),c=q(this.lastPoint,this.lastControlPoint);return[r*l[0]+o*a[0]+h*c[0],r*l[1]+o*a[1]+h*c[1]]}tangentAt(t){const e=this.pointToParam(t);return W(this.gradientAt(e))}get tangentAtFirstPoint(){return W(q(this.firstControlPoint,this.firstPoint))}get tangentAtLastPoint(){return W(q(this.lastControlPoint,this.lastPoint))}normalAt(t){const e=this.tangentAt(t);return tt(e)}splitAtParameters(t,e=null){const s=[...t];s.sort((r,o)=>r-o);let n={originalParam:0,param:0,p0:this.firstPoint,p1:this.firstControlPoint,p2:this.lastControlPoint};return s.flatMap((r,o)=>{if(r-n.originalParam<this.precision)return[];let h=!1;r>1-this.precision&&(h=!0);const l=(r-n.originalParam)/(1-n.originalParam),a=ke([n.p0,n.p1,n.p2,this.lastPoint],h?1:l),c=h?this.lastPoint:(e==null?void 0:e.get(r))??a[0][0],u=a[1][0],f=a[2][0],p=a[1][1],g=a[2][2],w=new zt(n.p0,c,f,u);return n={param:l,originalParam:r,p0:c,p1:p,p2:g},o===s.length-1&&!h?[w,new zt(n.p0,this.lastPoint,n.p1,n.p2)]:w})}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const s=e.map(r=>this.pointToParam(r)),n=new Map(mt([s,e]));return this.splitAtParameters(s,n)}transform(t){return new zt(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.firstControlPoint),t.transform(this.lastControlPoint))}paramPoint(t){const e=1-t,s=e*e,n=t*t,r=s*e,o=3*s*t,h=3*e*n,l=n*t;return[r*this.firstPoint[0]+o*this.firstControlPoint[0]+h*this.lastControlPoint[0]+l*this.lastPoint[0],r*this.firstPoint[1]+o*this.firstControlPoint[1]+h*this.lastControlPoint[1]+l*this.lastPoint[1]]}get polynomialCoefficients(){if(this._polynomialCoefficients===null){const t=e=>{const s=this.firstPoint[e],n=this.firstControlPoint[e],r=this.lastControlPoint[e],o=this.lastPoint[e],h=-s+3*n-3*r+o,l=3*s-6*n+3*r,a=-3*s+3*n;return[s,a,l,h]};this._polynomialCoefficients=[t(0),t(1)]}return this._polynomialCoefficients}paramsAtY(t){const[e,s,n,r]=this.polynomialCoefficients[1];return cs(e-t,s,n,r).filter(o=>o>=-this.precision&&o<=1+this.precision)}pointToParam(t){const e=this.paramsAtY(t[1]);if(e.length===0)throw new Error("Point is not on the curve");const s=r=>Math.abs(this.paramPoint(r)[0]-t[0])<=this.precision,n=e.find(s);if(n===void 0)throw new Error("Point is not on the curve");return n}},J=class Jt extends Xt{constructor(t,e,s,n,r,o,h=!1,{ignoreChecks:l=!1,angleUnits:a="deg"}={}){super(t,e),A(this,"segmentType","ELLIPSE_ARC"),A(this,"precision",1e-6),A(this,"majorRadius"),A(this,"minorRadius"),A(this,"center"),A(this,"tiltAngle"),A(this,"clockwise"),A(this,"_coefficients",null),A(this,"_boundingBox"),A(this,"_linearExentricity"),A(this,"_exentricity"),A(this,"_focals"),A(this,"_ellipseReferenceFrameTransform"),A(this,"_reverseEllipseReferenceFrameTransform"),A(this,"_rotateFromEllipseReferenceFrame"),A(this,"_firstAngle"),A(this,"_lastAngle"),A(this,"_deltaAngle"),this.center=s;const c=n>=r;this.majorRadius=c?n:r,this.minorRadius=c?r:n;const u=a==="deg"?o*St:o;if(this.tiltAngle=Ct(c?u:u+Math.PI/2),this.clockwise=h,!l){if(y(t,e))throw new Error("Invalid arc, cannot be a full circle");if(!this.isPointOnEllipse(t))throw new Error(`First point ${H(t)} not on the ellipse defined by ${this.info}`);if(!this.isPointOnEllipse(e))throw new Error(`Last point ${H(e)} 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,e]=this.center,s=this.majorRadius*this.majorRadius,n=this.minorRadius*this.minorRadius,r=Math.cos(this.tiltAngle),o=Math.sin(this.tiltAngle),h=s*o*o+n*r*r,l=2*(n-s)*o*r,a=s*r*r+n*o*o;this._coefficients={x2:h,xy:l,y2:a,x:-2*h*t-l*e,y:-2*a*e-l*t,c:h*t*t+l*t*e+a*e*e-s*n}}return this._coefficients}get info(){return`ELLIPSE_ARC(${H(this.firstPoint)}, ${H(this.lastPoint)}, ${H(this.center)}, ${this.majorRadius}, ${this.minorRadius}, ${this.tiltAngle*ve}, ${this.clockwise?"CW":"CCW"})`}reverse(){return new Jt(this.lastPoint,this.firstPoint,this.center,this.majorRadius,this.minorRadius,this.tiltAngle,!this.clockwise,{ignoreChecks:!0,angleUnits:"rad"})}clone(){return new Jt(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),e=this.minorRadius/this.majorRadius,s=-Math.atan(e*t),n=Math.atan(e/t);return[s,Math.PI+s,n,Math.PI+n].map(r=>this.thetaToParam(r)).filter(r=>this.isValidParameter(r)).map(r=>this.paramPoint(r))}get boundingBox(){if(this._boundingBox===void 0){const t=this.computeEllipseExtrema(),e=[this.firstPoint,this.lastPoint,...t];this._boundingBox=xe(e).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=[O(this.center,G(t,this.linearExentricity)),O(this.center,G(t,-this.linearExentricity))]}return this._focals}get majorAxis(){return Mt(1,this.tiltAngle)}paramPoint(t){const e=this.firstAngle+t*this.deltaAngle*(this.clockwise?-1:1);return this.reverseEllipseReferenceFrameTransform.transform([this.majorRadius*Math.cos(e),this.minorRadius*Math.sin(e)])}pointToParam(t){if(!this.isPointOnEllipse(t))throw new Error(`Point ${H(t)} not on the ellipse defined by ${this.repr}`);const e=this.thetaToParam(this.pointTheta(t));if(!this.isValidParameter(e))throw new Error(`Point ${H(t)} is not on segment ${this.repr}`);return e}get midPoint(){return this.paramPoint(.5)}isValidParameter(t){return 1-t>=-this.precision&&t>=-this.precision}isSame(t){return y(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)&&(y(this.firstPoint,t.firstPoint)&&this.clockwise===t.clockwise||y(this.firstPoint,t.lastPoint)&&this.clockwise!==t.clockwise)}pointTheta(t){const e=this.ellipseReferenceFrameTransform.transform(t),s=Math.atan2(e[1]/this.minorRadius,e[0]/this.majorRadius);return Ct(s)}thetaToParam(t){return te(this.firstAngle,Ct(t),this.clockwise)/this.deltaAngle}isPointOnEllipse(t){const[e,s]=this.focals,n=N(t,e),r=N(t,s);return Math.abs(2*this.majorRadius-n-r)<this.precision}isOnSegment(t){return this.isPointOnEllipse(t)?this.isValidParameter(this.thetaToParam(this.pointTheta(t))):!1}distanceFrom(t){let e;if(y(t,this.center)?e=O(this.center,G(tt(this.majorAxis),this.minorRadius)):e=this.reverseEllipseReferenceFrameTransform.transform(ln(this.majorRadius,this.minorRadius,this.ellipseReferenceFrameTransform.transform(t))),this.isValidParameter(this.thetaToParam(this.pointTheta(t))))return N(t,e);if(gi(t,this.majorAxis,this.center)){const s=Ct(2*Math.PI-this.pointTheta(t)),n=this.thetaToParam(s);if(this.isValidParameter(n))return N(t,this.paramPoint(n))}return Math.min(N(t,this.firstPoint),N(t,this.lastPoint))}get ellipseReferenceFrameTransform(){return this._ellipseReferenceFrameTransform===void 0&&(this._ellipseReferenceFrameTransform=new at().rotate(-this.tiltAngle).translate(-this.center[0],-this.center[1])),this._ellipseReferenceFrameTransform}get reverseEllipseReferenceFrameTransform(){return this._reverseEllipseReferenceFrameTransform===void 0&&(this._reverseEllipseReferenceFrameTransform=new at().translate(this.center[0],this.center[1]).rotate(this.tiltAngle)),this._reverseEllipseReferenceFrameTransform}get rotateFromEllipseReferenceFrame(){return this._rotateFromEllipseReferenceFrame===void 0&&(this._rotateFromEllipseReferenceFrame=new at().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=te(this.firstAngle,this.lastAngle,this.clockwise)),this._deltaAngle}normalAt(t){const e=this.tangentAt(t);return this.clockwise?tt(e):Dt(e)}gradientAt(t){const e=this.firstAngle+t*this.deltaAngle*(this.clockwise?-1:1),s=-this.majorRadius*this.deltaAngle*Math.sin(e),n=this.minorRadius*this.deltaAngle*Math.cos(e),r=this.clockwise?[-s,-n]:[s,n];return this.rotateFromEllipseReferenceFrame.transform(r)}tangentAt(t){const e=this.pointTheta(t),s=-this.majorRadius*Math.sin(e),n=this.minorRadius*Math.cos(e),r=this.clockwise?[-s,-n]:[s,n];return W(this.rotateFromEllipseReferenceFrame.transform(r))}get tangentAtFirstPoint(){const t=-this.majorRadius*Math.sin(this.firstAngle),e=this.minorRadius*Math.cos(this.firstAngle),s=this.clockwise?[-t,-e]:[t,e];return W(this.rotateFromEllipseReferenceFrame.transform(s))}get tangentAtLastPoint(){const t=-this.majorRadius*Math.sin(this.lastAngle),e=this.minorRadius*Math.cos(this.lastAngle),s=this.clockwise?[-t,-e]:[t,e];return W(this.rotateFromEllipseReferenceFrame.transform(s))}transform(t){const e=t.transformAngle(this.tiltAngle),s=t.scaleFactor();return new Jt(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.center),this.majorRadius*s,this.minorRadius*s,e,t.keepsOrientation()?this.clockwise:!this.clockwise,{angleUnits:"rad"})}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const s=[0,1,...e.map(o=>this.pointToParam(o))],n=new Map(mt([s,[this.firstPoint,this.lastPoint,...e]]));s.sort((o,h)=>o-h);let r=null;return s.flatMap((o,h)=>{if(h===s.length-1)return[];const l=s[h+1];if(l-o<this.precision)return r===null&&(r=o),[];const a=r===null?o:r,c=new Jt(n.get(a)||this.paramPoint(a),n.get(l)||this.paramPoint(l),this.center,this.majorRadius,this.minorRadius,this.tiltAngle,this.clockwise,{angleUnits:"rad"});return r=null,c})}};function hn(i,t,e,s,n,r,o){const{center:h,rx:l,ry:a}=an(i,t,e,s,n*St,!r,o);return Math.abs(l-a)<1e-9?new L(i,t,h,o):new J(i,t,h,l,a,n,o)}function an([i,t],[e,s],n,r,o,h,l){if(n<0&&(n=-n),r<0&&(r=-r),n==0||r==0)throw Error("rx and ry can not be 0");const a=Math.sin(o),c=Math.cos(o),u=(i-e)/2,f=(t-s)/2,p=(i+e)/2,g=(t+s)/2,w=c*u+a*f,P=c*f-a*u,m=w*w/(n*n)+P*P/(r*r);m>1&&(n=n*Math.sqrt(m),r=r*Math.sqrt(m));const d=n*r,M=n*P,x=r*w,v=M*M+x*x;if(!v)throw Error("start point can not be same as end point");let $=Math.sqrt(Math.abs((d*d-v)/v));h==l&&($=-$);const E=$*M/r,b=-$*x/n,T=c*E-a*b+p,V=a*E+c*b+g;return{center:[T,V],rx:n,ry:r}}function ln(i,t,e){const s=Math.abs(e[0]),n=Math.abs(e[1]);let r=.707,o=.707;const h=i,l=t;for(let a=0;a<3;a++){const c=h*r,u=l*o,f=(h*h-l*l)*r**3/h,p=(l*l-h*h)*o**3/l,g=c-f,w=u-p,P=s-f,m=n-p,d=Math.hypot(g,w),M=Math.hypot(P,m);r=Math.min(1,Math.max(0,(P*d/M+f)/h)),o=Math.min(1,Math.max(0,(m*d/M+p)/l));const x=Math.hypot(r,o);r/=x,o/=x}return[h*r*Math.sign(e[0]),l*o*Math.sign(e[1])]}let K=class Ut extends Xt{constructor(t,e,s){super(t,e),A(this,"segmentType","QUADRATIC_BEZIER"),A(this,"controlPoint"),A(this,"_boundingBox",null),A(this,"_polynomialCoefficients",null),this.controlPoint=s}get midPoint(){return this.paramPoint(.5)}get boundingBox(){if(this._boundingBox===null){const t=e=>{const[s,n,r]=[this.firstPoint[e],this.controlPoint[e],this.lastPoint[e]],o=s-2*n+r;if(!o)return[];const h=(s-n)/o;return h>=-this.precision&&h<=1+this.precision?[h]:[]};this._boundingBox=xe([this.firstPoint,...t(0).map(e=>this.paramPoint(e)),...t(1).map(e=>this.paramPoint(e)),this.lastPoint]).grow(this.precision)}return this._boundingBox}clone(){return new Ut(this.firstPoint,this.lastPoint,this.controlPoint)}reverse(){return new Ut(this.lastPoint,this.firstPoint,this.controlPoint)}isSame(t){return t.segmentType!=="QUADRATIC_BEZIER"?!1:y(this.firstPoint,t.firstPoint)&&y(this.lastPoint,t.lastPoint)&&y(this.controlPoint,t.controlPoint)}distanceFrom(t){const e=is(s=>kt(this.paramPoint(s),t),this.precision);return e.argMin<-this.precision||e.argMin>1+this.precision?Math.min(N(this.firstPoint,t),N(this.lastPoint,t)):Math.sqrt(e.fMin)}isOnSegment(t){if(!this.boundingBox.contains(t))return!1;try{const e=this.pointToParam(t);return e>=-this.precision&&e<=1+this.precision}catch{return!1}}gradientAt(t){const e=s=>{const n=this.firstPoint[s],r=this.controlPoint[s],o=this.lastPoint[s];return 2*(t*(n-2*r+o)-n+r)};return[e(0),e(1)]}tangentAt(t){const e=this.pointToParam(t);return W(this.gradientAt(e))}get tangentAtFirstPoint(){return W(q(this.controlPoint,this.firstPoint))}get tangentAtLastPoint(){return W(q(this.controlPoint,this.lastPoint))}normalAt(t){const e=this.tangentAt(t);return tt(e)}splitAtParameters(t,e=null){const s=[...t];s.sort((r,o)=>r-o);let n={originalParam:0,param:0,p0:this.firstPoint,p1:this.controlPoint};return s.flatMap((r,o)=>{if(r-n.originalParam<this.precision)return[];let h=!1;r>1-this.precision&&(h=!0);const l=(r-n.originalParam)/(1-n.originalParam),a=ke([n.p0,n.p1,this.lastPoint],h?1:l),c=h?this.lastPoint:(e==null?void 0:e.get(r))??a[0][0],u=a[1][0],f=a[1][1],p=new Ut(n.p0,c,u);return n={param:l,originalParam:r,p0:c,p1:f},o===s.length-1&&!h?[p,new Ut(n.p0,this.lastPoint,n.p1)]:p})}splitAt(t){let e;if(Array.isArray(t)&&t.length===0)return[this];Array.isArray(t[0])?e=t:e=[t];const s=e.map(r=>this.pointToParam(r)),n=new Map(mt([s,e]));return this.splitAtParameters(s,n)}transform(t){return new Ut(t.transform(this.firstPoint),t.transform(this.lastPoint),t.transform(this.controlPoint))}paramPoint(t){const e=1-t,s=e*e,n=2*e*t,r=t*t;return[s*this.firstPoint[0]+n*this.controlPoint[0]+r*this.lastPoint[0],s*this.firstPoint[1]+n*this.controlPoint[1]+r*this.lastPoint[1]]}get polynomialCoefficients(){if(this._polynomialCoefficients===null){const t=e=>{const s=this.firstPoint[e],n=this.controlPoint[e],r=this.lastPoint[e],o=s-2*n+r,h=2*(n-s);return[s,h,o]};this._polynomialCoefficients=[t(0),t(1)]}return this._polynomialCoefficients}paramsAtY(t){const[e,s,n]=this.polynomialCoefficients[1];return ae(e-t,s,n).filter(r=>r>=-this.precision&&r<=1+this.precision)}pointToParam(t){const e=this.paramsAtY(t[1]);if(e.length===0)throw new Error("Point is not on the curve!");const s=r=>Math.abs(this.paramPoint(r)[0]-t[0])<=this.precision,n=e.find(s);if(n===void 0)throw new Error("Point is not on the curve");return n}};var un=Object.defineProperty,cn=(i,t,e)=>t in i?un(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,st=(i,t,e)=>(cn(i,typeof t!="symbol"?t+"":t,e),e);function fn(i,t){const e=q(t,i.firstPoint),s=Ae(e,i.V)/i.squareLength;return i.paramPoint(s)}function le(i,t,e){const s=e||i.precision,n=fn(i,t.center),r=N(n,t.center);if(r>t.radius+s)return[];if(Math.abs(r-t.radius)<s){const u=n;return i.isOnSegment(u)&&t.isOnSegment(u)?[u]:[]}const o=[],h=Math.sqrt(t.radius*t.radius-r*r),l=i.tangentAtFirstPoint,a=O(n,G(l,h));i.isOnSegment(a)&&t.isOnSegment(a)&&o.push(a);const c=O(n,G(l,-h));return i.isOnSegment(c)&&t.isOnSegment(c)&&o.push(c),o}const mn=i=>{const{firstPoint:t,lastPoint:e,center:s,clockwise:n}=i;return new L(e,t,s,n,{ignoreChecks:!0})},gn=(i,t)=>{if(i.isSame(t))return[i];const 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,n)=>i.pointToParam(s)-i.pointToParam(n));if(e.length===0)return[];if(e.length===1)return[];if(e.length===2)return i.isSame(mn(t))?[]:[new L(e[0],e[1],i.center,i.clockwise)];if(e.length===3){const s=y(e[0],t.lastPoint)||y(e[0],t.firstPoint)?1:0;return[new L(e[0+s],e[1+s],i.center,i.clockwise)]}else if(e.length===4)return[new L(e[0],e[1],i.center,i.clockwise),new L(e[2],e[3],i.center,i.clockwise)];throw new Error("Bug in the arc arc overlap algorithm")};function ms(i,t,e=!1,s){const n=s||i.precision,r=N(i.center,t.center),o=i.radius+t.radius;if(r>o+n)return[];const h=Math.abs(i.radius-t.radius);if(r<h-n)return[];if(r<n)return h>n?[]:e?gn(i,t):[];const l=W(q(t.center,i.center)),a=r>o-n;if(a||Math.abs(r-h)<n){const m=a||i.radius>t.radius?1:-1,d=O(i.center,G(l,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=O(i.center,G(l,c)),f=Math.sqrt(i.radius*i.radius-c*c),p=tt(l),g=O(u,G(p,f)),w=O(u,G(p,-f)),P=[];return i.isOnSegment(g)&&t.isOnSegment(g)&&P.push(g),i.isOnSegment(w)&&t.isOnSegment(w)&&P.push(w),P}function $e(i,t,e=1e-9){const s=i.transform(t.ellipseReferenceFrameTransform),n=s.slope,r=s.yIntercept,o=t.majorRadius*t.majorRadius,h=t.minorRadius*t.minorRadius,l=t.majorRadius*t.minorRadius,a=s.slope*s.slope,c=s.yIntercept*s.yIntercept,u=m=>m.map(d=>t.reverseEllipseReferenceFrameTransform.transform(d)).filter(d=>i.isOnSegment(d)&&t.isOnSegment(d));if(!Number.isFinite(n)){const m=s.firstPoint[0];if(Math.abs(m)-t.majorRadius>e)return[];if(Math.abs(Math.abs(m)-t.majorRadius)<e)return u([[m,0]]);const d=t.minorRadius*Math.sqrt(1-m*m/o),M=[m,d],x=[m,-d];return u([M,x])}const f=o*a+h-c;if(f<-e)return[];const p=o*a+h;if(Math.abs(f)<e){const m=-(o*n*r)/p,d=h*r/p;return u([[m,d]])}const g=Math.sqrt(f),w=[-(o*n*r+l*g)/p,(h*r-l*n*g)/p],P=[-(o*n*r-l*g)/p,(h*r+l*n*g)/p];return u([w,P])}function gs(i,t){const e=Math.max(i.precision,t.precision),s=i.coefficients,n=s.x2,r=s.xy,o=s.y2,h=s.x,l=s.y,a=s.c,c=t.coefficients,u=c.x2,f=c.xy,p=c.y2,g=c.x,w=c.y,P=c.c,m={z0:a*n*g*g+n*n*P*P-h*n*g*P+u*u*a*a-2*n*P*u*a-h*g*u*a+u*h*h*P,z1:w*h*h*u-P*g*n*r-2*n*P*u*l-a*u*f*h+2*g*f*n*a+2*w*P*n*n+g*g*n*l-w*g*n*h-2*n*w*u*a-a*u*g*r+2*a*l*u*u-P*f*n*h-l*u*g*h+2*P*r*u*h,z2:w*w*n*n+2*p*P*n*n-l*u*g*r+P*u*r*r-l*u*f*h-P*f*n*r-2*n*w*u*l+2*g*f*n*l-p*g*n*h-2*n*p*u*a+f*f*n*a+2*w*r*u*h+l*l*u*u-o*u*g*h-w*f*n*h+2*a*o*u*u-a*u*f*r+p*h*h*u+g*g*n*o-w*g*n*r-2*n*P*u*o,z3:-2*n*u*o*w+w*u*r*r+2*p*r*u*h-o*u*f*h+f*f*n*l-w*f*n*r-2*n*p*u*l-l*u*f*r-p*f*n*h+2*w*p*n*n+2*l*o*u*u-o*u*g*r+2*g*f*n*o-p*g*n*r,z4:n*n*p*p-2*n*p*u*o+u*u*o*o-r*n*f*p-r*f*u*o+r*r*u*p+o*n*f*f},d=Se([m.z0,m.z1,m.z2,m.z3,m.z4],e).flatMap(M=>{const x=n*f*M+n*g-u*r*M-u*h;if(x)return[[-(n*P+n*p*M*M-u*o*M*M+n*w*M-u*l*M-u*a)/x,M]];const v=r*M+h,$=-v/(2*n),E=o*M*M+l*M+a,b=v*v/(4*n*n)-E/n;if(Math.abs(b)<e)return[[$,M]];if(b>0){const T=Math.sqrt(b);return[[$+T,M],[$-T,M]]}return[]});return Pt(d,e)}function ps(i,t){return gs(i,t).filter(e=>i.isOnSegment(e)&&t.isOnSegment(e))}const pn=i=>{const{firstPoint:t,lastPoint:e,center:s,majorRadius:n,minorRadius:r,tiltAngle:o,clockwise:h}=i;return new J(e,t,s,n,r,o,h,{ignoreChecks:!0,angleUnits:"rad"})},wn=(i,t)=>{if(i.isSame(t))return[i];const e=(n,r)=>new J(n,r,i.center,i.majorRadius,i.minorRadius,i.tiltAngle,i.clockwise,{ignoreChecks:!0,angleUnits:"rad"}),s=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(n=>n!==null)).sort((n,r)=>i.pointToParam(n)-i.pointToParam(r));if(s.length===0)return[];if(s.length===1)return[];if(s.length===2)return i.isSame(pn(t))?[]:[e(s[0],s[1])];if(s.length===3){const n=y(s[0],t.lastPoint)||y(s[0],t.firstPoint)?1:0;return[e(s[0+n],s[1+n])]}else if(s.length===4)return[e(s[0],s[1]),e(s[2],s[3])];throw new Error("Bug in the ellipse arc ellipse arc overlap algorithm")};function dn(i,t,e=!1){const s=Math.max(i.precision,t.precision);return y(i.center,t.center)&&Math.abs(i.majorRadius-t.majorRadius)<s&&Math.abs(i.minorRadius-t.minorRadius)<s&&(Math.abs(i.tiltAngle-t.tiltAngle)<s||Math.abs(Math.abs(i.tiltAngle-t.tiltAngle)-Math.PI)<s)?e?wn(i,t):[]:gs(i,t).filter(n=>i.isOnSegment(n)&&t.isOnSegment(n))}function ws(i,t){const[e,s]=i.firstPoint,[n,r]=i.lastPoint,o=new at().rotate(-Math.atan2(r-s,n-e)).translate(-e,-s),h=o.clone().inverse(),l=t.transform(o);return l.paramsAtY(0).map(a=>l.paramPoint(a)).map(a=>h.transform(a)).filter(a=>i.isOnSegment(a))}const Pn=(i,t=1e-9)=>{let e=i;return Math.abs(i)<t&&(e=0),e.toFixed(-Math.log10(t))};function ds(i,t=1e-9){return Array.from(new Map(i.map(e=>[Pn(e,t),e])).values())}const Mn=(i,t)=>{const[[e,s,n,r],[o,h,l,a]]=t.polynomialCoefficients,c=i.coefficients,u=c.x2,f=c.xy,p=c.y2,g=c.x,w=c.y,P=c.c,m=e*e,d=s*s,M=n*n,x=r*r,v=o*o,$=h*h,E=l*l,b=a*a,T=P+g*e+u*m+w*o+f*e*o+p*v,V=g*s+2*u*e*s+f*s*o+w*h+f*e*h+2*p*o*h,Y=u*d+g*n+2*u*e*n+f*n*o+f*s*h+p*$+w*l+f*e*l+2*p*o*l,nt=2*u*s*n+g*r+2*u*e*r+f*r*o+f*n*h+f*s*l+2*p*h*l+w*a+f*e*a+2*p*o*a,_=u*M+2*u*s*r+f*r*h+f*n*l+p*E+f*s*a+2*p*h*a,C=2*u*n*r+f*r*l+f*n*a+2*p*l*a,k=u*x+f*r*a+p*b;return[T,V,Y,nt,_,C,k]};function Ps(i,t){const e=Math.max(i.precision,t.precision),s=Mn(i,t),n=Se(s,e).filter(r=>r>=-t.precision&&r<=1+t.precision);return ds(n,e).map(r=>t.paramPoint(r)).filter(r=>i.isOnSegment(r))}const yn=(i,t)=>{const[[e,s,n],[r,o,h]]=t.polynomialCoefficients,l=i.coefficients,a=l.x2,c=l.xy,u=l.y2,f=l.x,p=l.y,g=l.c,w=e*e,P=s*s,m=n*n,d=r*r,M=o*o,x=h*h,v=a*w+c*e*r+u*d+f*e+p*r+g,$=2*a*e*s+c*e*o+c*s*r+2*u*r*o+f*s+p*o,E=2*a*e*n+a*P+c*e*h+c*s*o+c*n*r+2*u*r*h+u*M+f*n+p*h,b=2*a*s*n+c*s*h+c*n*o+2*u*o*h,T=a*m+c*n*h+u*x;return[v,$,E,b,T]};function Ms(i,t){const e=Math.max(i.precision,t.precision),s=yn(i,t),n=en(...s).filter(r=>r>=-t.precision&&r<=1+t.precision);return ds(n,e).map(r=>t.paramPoint(r)).filter(r=>i.isOnSegment(r))}function it(i,{firstPoint:t,lastPoint:e},s=1e-9){const n=q(e,t);return Math.abs(n[0])<s?n[1]>0?t[0]-i[0]:i[0]-t[0]:Math.abs(n[1])<s?n[0]>0?i[1]-t[1]:t[1]-i[1]:bt(n,q(i,t))/fi(n)}class Ce{constructor(t,e,s,n){this.firstPoint=t,this.lastPoint=e,this.negativeThickness=s,this.positiveThickness=n}get width(){return this.positiveThickness-this.negativeThickness}}const bn=3/4,xn=4/9;function vn(i){const t=it(i.firstControlPoint,i),e=it(i.lastControlPoint,i),s=t*e>0?bn:xn;return new Ce(i.firstPoint,i.lastPoint,s*Math.min(0,t,e),s*Math.max(0,t,e))}function An(i){const t=it(i.controlPoint,i);return new Ce(i.firstPoint,i.lastPoint,Math.min(0,t/2),Math.max(0,t/2))}function En(i){if(i instanceof et)return vn(i);if(i instanceof K)return An(i);throw new Error("Not implemented")}function Sn(i){const t=i.paramPoint(.5),e=tt(q(t,i.firstPoint)),s=O(t,e),n={firstPoint:t,lastPoint:s},r=[it(i.firstPoint,n),it(i.lastPoint,n)];return i instanceof et?r.push(it(i.firstControlPoint,n),it(i.lastControlPoint,n)):i instanceof K&&r.push(it(i.controlPoint,n)),new Ce(t,s,Math.min(...r),Math.max(...r))}function ys(i,t){const e=[];for(let s=1;s<i.length;s++){const n=i[s];if(n[1]===t){e.push(n[0]);continue}const r=i[s-1],o=t-r[1],h=t-n[1];if(o*h<0){e.push(r[0]+(t-r[1])*(n[0]-r[0])/(n[1]-r[1]));continue}}return e}let Ht=class{constructor(t,e){this.from=t,this.to=e}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 kn(i,t){if(i instanceof et)return new Cn([it(i.firstPoint,t),it(i.firstControlPoint,t),it(i.lastControlPoint,t),it(i.lastPoint,t)]);if(i instanceof K)return new $n([it(i.firstPoint,t),it(i.controlPoint,t),it(i.lastPoint,t)]);throw new Error("Not implemented")}class $n{constructor(t){st(this,"topHull",[]),st(this,"bottomHull",[]),this.distances=t;const[e,s,n]=t,r=[0,e],o=[1/2,s],h=[1,n],l=n-e,a=e;s-(l*(1/2)+a)>0?(this.topHull=[r,o,h],this.bottomHull=[r,h]):(this.topHull=[r,h],this.bottomHull=[r,o,h])}get startDistance(){return this.distances[0]}get endDistance(){return this.distances[2]}}class Cn{constructor(t){st(this,"topHull",[]),st(this,"bottomHull",[]),this.distances=t;const[e,s,n,r]=t,o=[0,e],h=[1/3,s],l=[2/3,n],a=[1,r],c=r-e,u=e,f=s-(c*(1/3)+u),p=n-(c*(2/3)+u);let g=null,w=null;if(f*p<0)g=[o,h,a],w=[o,l,a];else{const P=f/p;P>=2?(g=[o,h,a],w=[o,a]):P<=.5?(g=[o,l,a],w=[o,a]):(g=[o,h,l,a],w=[o,a])}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 bs(i,t){const e=kn(t,i),s=ys(e.topHull,i.negativeThickness),n=ys(e.bottomHull,i.positiveThickness),r=e.endDistance>=i.negativeThickness&&e.endDistance<=i.positiveThickness;if(!s.length&&!n.length)return r?new Ht("start","end"):null;if(s.length===1&&n.length===1)return new Ht(s[0],n[0]);if(s.length===2&&n.length===2)throw new Error("Bug in the clipping algorithm, unexpected number of crossing points");const o=s.length?s:n;return o.length===2?new Ht(o[0],o[1]):r?new Ht(o[0],"end"):new Ht("start",o[0])}function xs(i,t){const e=En(i),s=bs(e,t);if(!s)return null;const n=Sn(i),r=bs(n,t);return r?s.size>r.size?r.clipCurve(t):s.clipCurve(t):null}const ue=i=>i instanceof K?wt(q(i.controlPoint,i.firstPoint))+wt(q(i.controlPoint,i.lastPoint)):wt(q(i.firstControlPoint,i.firstPoint))+wt(q(i.lastControlPoint,i.firstControlPoint))+wt(q(i.lastControlPoint,i.lastPoint));function Tt(i,t,e=1e-9,{maxIterations:s=100}={}){const n=Math.max(e*e,Number.EPSILON*10);let r=i,o=t,h=ue(r),l=ue(o);for(let a=0;a<s;a++){const c=h>n?xs(o,r):r;if(!c)return[];const u=ue(c),f=l>n?xs(c,o):o;if(!f)return[];const p=ue(f);if(u<=n&&p<=n)return[c.boundingBox.intersection(f.boundingBox).center];if(y(c.firstPoint,c.lastPoint)&&f.isOnSegment(c.firstPoint))return[c.firstPoint];if(y(f.firstPoint,f.lastPoint)&&c.isOnSegment(f.firstPoint))return[f.firstPoint];if(u>.8*h&&p>.8*l)if(u/h>p/l){const[g,w]=c.splitAtParameters([.5]);return Pt([...Tt(g,f,e,{maxIterations:s-a}),...Tt(w,f,e,{maxIterations:s-a})],e)}else{const[g,w]=f.splitAtParameters([.5]);return Pt([...Tt(c,g,e,{maxIterations:s-a}),...Tt(c,w,e,{maxIterations:s-a})],e)}r=c,o=f,h=u,l=p}throw new Error("Bézier clip: Maximum number of iterations reached")}function Rn(i,t){const e=[];if([[i.firstPoint,t],[i.lastPoint,t],[t.firstPoint,i],[t.lastPoint,i]].forEach(([s,n])=>{n.isOnSegment(s)&&e.push(s)}),e.length<2)return null;if(e.length===2)return[i.splitAt(e)[1]];if(e.length===3)return y(e[0],i.firstPoint)&&y(e[1],i.lastPoint)?[i]:[t];if(e.length===4)return[i]}function Tn(i,t,e=!1){const s=Math.max(i.precision,t.precision);if(e){const n=Rn(i,t);if(n)return n}return Tt(i,t,s)}function Fn(i,t){const e=[];if([[i.firstPoint,t],[i.lastPoint,t],[t.firstPoint,i],[t.lastPoint,i]].forEach(([s,n])=>{n.isOnSegment(s)&&e.push(s)}),e.length<2)return null;if(e.length===2)return[i.splitAt(e)[1]];if(e.length===3)return y(e[0],i.firstPoint)&&y(e[1],i.lastPoint)?[i]:[t];if(e.length===4)return[i]}function In(i,t,e=!1){const s=Math.max(i.precision,t.precision);if(e){const n=Fn(i,t);if(n)return n}return Tt(i,t,s)}function _n(i,t,e){if(i instanceof B&&t instanceof B){const s=Je(i,t,!1,e);return s===null?[]:[s]}if(i instanceof B&&t instanceof L)return le(i,t,e);if(i instanceof L&&t instanceof B)return le(t,i,e);if(i instanceof L&&t instanceof L)return ms(i,t,!1,e);throw new Error("Not implemented")}function Gt(i,t,e){if(i instanceof B&&t instanceof B){const s=Je(i,t,!0,e);return s===null?{intersections:[],overlaps:[],count:0}:s instanceof B?{intersections:[],overlaps:[s],count:1}:{intersections:[s],overlaps:[],count:1}}if(!i.boundingBox.overlaps(t.boundingBox))return{intersections:[],overlaps:[],count:0};if(i instanceof B&&t instanceof L){const s=le(i,t,e);return{intersections:s,overlaps:[],count:s.length}}if(i instanceof L&&t instanceof B){const s=le(t,i,e);return{intersections:s,overlaps:[],count:s.length}}if(i instanceof L&&t instanceof L){const s=ms(i,t,!0,e);return s.length?s[0]instanceof L?{intersections:[],overlaps:s,count:s.length}:{intersections:s,overlaps:[],count:s.length}:{intersections:[],overlaps:[],count:0}}if(i instanceof B&&t instanceof J){const s=$e(i,t,e);return{intersections:s,overlaps:[],count:s.length}}if(t instanceof B&&i instanceof J){const s=$e(t,i,e);return{intersections:s,overlaps:[],count:s.length}}if(i instanceof L&&t instanceof J){const s=ps(i,t);return{intersections:s,overlaps:[],count:s.length}}if(t instanceof L&&i instanceof J){const s=ps(t,i);return{intersections:s,overlaps:[],count:s.length}}if(i instanceof J&&t instanceof J){const s=dn(i,t,!0);return s.length?s[0]instanceof J?{intersections:[],overlaps:s,count:s.length}:{intersections:s,overlaps:[],count:s.length}:{intersections:[],overlaps:[],count:0}}if(i instanceof B&&(t instanceof et||t instanceof K)){const s=ws(i,t);return{intersections:s,overlaps:[],count:s.length}}if(t instanceof B&&(i instanceof et||i instanceof K)){const s=ws(t,i);return{intersections:s,overlaps:[],count:s.length}}if((i instanceof L||i instanceof J)&&t instanceof K){const s=Ms(i,t);return{intersections:s,overlaps:[],count:s.length}}if((t instanceof L||t instanceof J)&&i instanceof K){const s=Ms(t,i);return{intersections:s,overlaps:[],count:s.length}}if((i instanceof L||i instanceof J)&&t instanceof et){const s=Ps(i,t);return{intersections:s,overlaps:[],count:s.length}}if((t instanceof L||t instanceof J)&&i instanceof et){const s=Ps(t,i);return{intersections:s,overlaps:[],count:s.length}}if(i instanceof K&&t instanceof K){const s=In(i,t);return s.length?s[0]instanceof K?{intersections:[],overlaps:s,count:s.length}:{intersections:s,overlaps:[],count:s.length}:{intersections:[],overlaps:[],count:0}}if(i instanceof K&&t instanceof et||t instanceof K&&i instanceof et){const s=Tt(i,t);return{intersections:s,overlaps:[],count:s.length}}if(i instanceof et&&t instanceof et){const s=Tn(i,t);return s.length?s[0]instanceof et?{intersections:[],overlaps:s,count:s.length}:{intersections:s,overlaps:[],count:s.length}:{intersections:[],overlaps:[],count:0}}throw new Error("Not implemented")}function vs(i){const t=[];for(let e=0;e<i;e++)for(let s=0;s<=e;s++)t.push([e,s]);return t}function*Re(i){for(const[t,e]of vs(i.length))t!==e&&(yield[i[t],i[e]])}class As extends $t{constructor(t,{ignoreChecks:e=!1}={}){super(),st(this,"segments"),st(this,"_boundingBox",null),e||Es(t),this.segments=t}get repr(){return this.segments.map(t=>t.repr).join(`
|
|
9
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(e=>e.isOnSegment(t))}intersects(t){return this.boundingBox.overlaps(t.boundingBox)?this.segments.some(e=>t.segments.some(n=>zt(e,n).count>0)):!1}overlappingSegments(t){return this.segments.flatMap(e=>t.segments.flatMap(n=>e.boundingBox.overlaps(n.boundingBox)?zt(e,n).overlaps:[]))}get boundingBox(){if(this._boundingBox===null){let t=this.segments[0].boundingBox;this.segments.slice(1).forEach(e=>{t=t.merge(e.boundingBox)}),this._boundingBox=t}return this._boundingBox}[Symbol.for("nodejs.util.inspect.custom")](){return this.repr}}function vr(s,t="Stroke"){pn(s.length).forEach(([e,n])=>{if(e===n)return;const r=s[e],o=s[n],i=zt(r,o),h=Math.max(r.precision,o.precision);if(i.count!==0){if(i.count===1&&!i.overlaps.length){const l=e-n,a=i.intersections[0];if(l===1&&y(r.firstPoint,a,h)||l===-1&&y(r.lastPoint,a,h)||l===s.length-1&&y(r.lastPoint,a,h)&&y(o.firstPoint,a,h)||-l===s.length-1&&y(r.firstPoint,a,h)&&y(o.lastPoint,a,h))return}if(!(i.count===2&&s.length===2&&(y(r.firstPoint,i.intersections[0],h)&&y(r.lastPoint,i.intersections[1],h)||y(r.firstPoint,i.intersections[1],h)&&y(r.lastPoint,i.intersections[0],h))))throw new Error(`${t} segments must not intersect, but segments ${r.info} and ${o.info} do at ${JSON.stringify(i.intersections)}`)}})}function dn(s,t="Stroke"){if(s.length===0)throw new Error(`${t} must have at least one segment`);at([s.slice(0,-1),s.slice(1)]).forEach(([e,n])=>{if(!y(e.lastPoint,n.firstPoint))throw new Error(`${t} segments must be connected, but ${e.info} and ${n.info} are not`)}),vr(s,t)}function Pn(s,t){return!!(s instanceof B&&t instanceof B&&we(s.V,t.V)||s instanceof T&&t instanceof T&&y(s.center,t.center)&&s.radius-t.radius<s.precision)}function Mn(s,t){if(s instanceof B&&t instanceof B)return new B(s.firstPoint,t.lastPoint);if(s instanceof T&&t instanceof T)return new T(s.firstPoint,t.lastPoint,s.center,s.clockwise);throw new Error("Not implemented")}function yn(s){let t=!1;const e=[];for(const n of s.segments){if(e.length===0){e.push(n);continue}const r=e[e.length-1];Pn(r,n)?(t=!0,e.pop(),e.push(Mn(r,n))):e.push(n)}if(y(s.firstPoint,s.lastPoint)&&Pn(e[0],e[e.length-1])){t=!0;const n=e.pop();e[0]=Mn(n,e[0])}return t?e:null}let Pt=class jt extends wn{constructor(){super(...arguments),this.strokeType="STRAND"}reverse(){const t=this.segments.map(e=>e.reverse());return t.reverse(),new jt(t,{ignoreChecks:!0})}clone(){return new jt(this.segments.map(t=>t.clone()),{ignoreChecks:!0})}extend(t){if(!y(this.lastPoint,t.firstPoint))throw console.error(this.repr,t.repr),new Error("Cannot extend strand: connection point is not the same");return new jt([...this.segments,...t.segments])}simplify(){const t=yn(this);return t?new jt(t,{ignoreChecks:!0}):this}transform(t){return new jt(this.segments.map(e=>e.transform(t)),{ignoreChecks:!0})}};const Ar=(s,t)=>{const e=Wt(t,{V:[1,0],firstPoint:s,precision:t.precision});if(e==="parallel")return 0;const{intersectionParam1:n,intersectionParam2:r}=e;if(!t.isValidParameter(n)||r<=-t.precision)return 0;if(Math.abs(n)<t.precision||Math.abs(n-1)<t.precision){const[,o]=t.midPoint;return s[1]-o<0?1:0}return 1};class xe{constructor(t){this._count=0,this.segment=t}update(t,e=!1){!e&&!this.segment.isOnSegment(t)||(y(t,this.segment.firstPoint)?this._count+=this.segment.tangentAtFirstPoint[1]>0?1:0:y(t,this.segment.lastPoint)?this._count+=this.segment.tangentAtLastPoint[1]>0?0:1:this._count+=1)}get count(){return this._count}}const Er=(s,t)=>{const e=t.precision,n=Math.abs(s[1]-t.center[1]);if(n>t.radius+e)return 0;const r=vt(s,t.center),o=t.radius*t.radius,i=e*e;if(Math.abs(r-o)<i&&t.isOnSegment(s))return 0;const h=r-o>i;if(h&&t.center[0]<s[0])return 0;const l=Math.sqrt(t.radius*t.radius-n*n),a=new xe(t);return a.update([t.center[0]+l,s[1]]),h&&a.update([t.center[0]-l,s[1]]),a.count},Sr=(s,t)=>{const e=t.boundingBox.xMax+t.boundingBox.width/2,n=new B(s,[e,s[1]]),r=new xe(t);return Me(n,t).forEach(o=>{r.update(o,!0)}),r.count},kr=(s,t)=>{const e=new xe(t);return t.paramsAtY(s[1]).map(n=>{try{return t.paramPoint(n)}catch{return null}}).filter(n=>n!==null).filter(n=>{const[r]=n;return r>=s[0]}).forEach(n=>{e.update(n,!0)}),e.count};function Cr(s,t){if(t instanceof B)return Ar(s,t);if(t instanceof T)return Er(s,t);if(t instanceof Z)return Sr(s,t);if(t instanceof K||t instanceof N)return kr(s,t);throw new Error("Not implemented")}class it extends wn{constructor(t,{ignoreChecks:e=!1}={}){super(t,{ignoreChecks:!0}),this.strokeType="LOOP",this._clockwise=null,e||Tr(t)}get clockwise(){if(this._clockwise===null){const t=this.segments.flatMap(n=>n instanceof B?[n.firstPoint]:[n.firstPoint,n.paramPoint(.5)]),e=t.map((n,r)=>{const o=t[(r+1)%t.length];return(o[0]-n[0])*(o[1]+n[1])}).reduce((n,r)=>n+r,0);this._clockwise=e>0}return this._clockwise}clone(){return new it(this.segments.map(t=>t.clone()),{ignoreChecks:!0})}reverse(){const t=this.segments.map(e=>e.reverse());return t.reverse(),new it(t,{ignoreChecks:!0})}transform(t){return new it(this.segments.map(e=>e.transform(t)),{ignoreChecks:!0})}contains(t){return this.onStroke(t)||!this.boundingBox.contains(t)?!1:this.segments.reduce((e,n)=>e+Cr(t,n),0)%2===1}simplify(){const t=yn(this);return t?new it(t,{ignoreChecks:!0}):this}}function Tr(s){if(dn(s,"Loop"),!y(s[0].firstPoint,s[s.length-1].lastPoint))throw new Error("Loop segment must be closed")}const Rr=[B,T,Z,N,K];function bn(s){return Rr.some(t=>s instanceof t)}function xn(s){if(s instanceof B)return{type:s.segmentType,firstPoint:s.firstPoint,lastPoint:s.lastPoint};if(s instanceof T)return{type:s.segmentType,firstPoint:s.firstPoint,lastPoint:s.lastPoint,center:s.center,clockwise:s.clockwise};if(s instanceof Z)return{type:s.segmentType,firstPoint:s.firstPoint,lastPoint:s.lastPoint,center:s.center,clockwise:s.clockwise,majorRadius:s.majorRadius,minorRadius:s.minorRadius,tiltAngle:s.tiltAngle};if(s instanceof N)return{type:s.segmentType,firstPoint:s.firstPoint,lastPoint:s.lastPoint,controlPoint:s.controlPoint};if(s instanceof K)return{type:s.segmentType,firstPoint:s.firstPoint,lastPoint:s.lastPoint,firstControlPoint:s.firstControlPoint,lastControlPoint:s.lastControlPoint};throw new Error("Unknown segment type")}function ve(s){return{type:"LOOP",segments:s.segments.map(xn)}}function vn(s){return{type:"FIGURE",contour:ve(s.contour),holes:s.holes.map(ve)}}function Fr(s){return{type:"DIAGRAM",figures:s.figures.map(vn)}}function An(s){if(s instanceof ht)return Fr(s);if(s instanceof Q)return vn(s);if(s instanceof it)return ve(s);if(bn(s))return xn(s);throw new Error("Unknown shape type")}class Ir{constructor(){this.ids=[],this.values=[],this.length=0}clear(){this.length=0}push(t,e){let n=this.length++;for(;n>0;){const r=n-1>>1,o=this.values[r];if(e>=o)break;this.ids[n]=this.ids[r],this.values[n]=o,n=r}this.ids[n]=t,this.values[n]=e}pop(){if(this.length===0)return;const t=this.ids[0];if(this.length--,this.length>0){const e=this.ids[0]=this.ids[this.length],n=this.values[0]=this.values[this.length],r=this.length>>1;let o=0;for(;o<r;){let i=(o<<1)+1;const h=i+1;let l=this.ids[i],a=this.values[i];const c=this.values[h];if(h<this.length&&c<a&&(i=h,l=this.ids[h],a=c),a>=n)break;this.ids[o]=l,this.values[o]=a,o=i}this.ids[o]=e,this.values[o]=n}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 En=[Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array],Ae=3;class Ee{static from(t){if(!t||t.byteLength===void 0||t.buffer)throw new Error("Data must be an instance of ArrayBuffer or SharedArrayBuffer.");const[e,n]=new Uint8Array(t,0,2);if(e!==251)throw new Error("Data does not appear to be in a Flatbush format.");if(n>>4!==Ae)throw new Error(`Got v${n>>4} data when expected v${Ae}.`);const[r]=new Uint16Array(t,2,1),[o]=new Uint32Array(t,4,1);return new Ee(o,r,En[n&15],void 0,t)}constructor(t,e=16,n=Float64Array,r=ArrayBuffer,o){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(+e,2),65535);let i=t,h=i;this._levelBounds=[i*4];do i=Math.ceil(i/this.nodeSize),h+=i,this._levelBounds.push(h*4);while(i!==1);this.ArrayType=n||Float64Array,this.IndexArrayType=h<16384?Uint16Array:Uint32Array;const l=En.indexOf(this.ArrayType),a=h*4*this.ArrayType.BYTES_PER_ELEMENT;if(l<0)throw new Error(`Unexpected typed array class: ${n}.`);o&&o.byteLength!==void 0&&!o.buffer?(this.data=o,this._boxes=new this.ArrayType(this.data,8,h*4),this._indices=new this.IndexArrayType(this.data,8+a,h),this._pos=h*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 r(8+a+h*this.IndexArrayType.BYTES_PER_ELEMENT),this._boxes=new this.ArrayType(this.data,8,h*4),this._indices=new this.IndexArrayType(this.data,8+a,h),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,(Ae<<4)+l]),new Uint16Array(this.data,2,1)[0]=e,new Uint32Array(this.data,4,1)[0]=t),this._queue=new Ir}add(t,e,n,r){const o=this._pos>>2,i=this._boxes;return this._indices[o]=o,i[this._pos++]=t,i[this._pos++]=e,i[this._pos++]=n,i[this._pos++]=r,t<this.minX&&(this.minX=t),e<this.minY&&(this.minY=e),n>this.maxX&&(this.maxX=n),r>this.maxY&&(this.maxY=r),o}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 e=this.maxX-this.minX||1,n=this.maxY-this.minY||1,r=new Uint32Array(this.numItems),o=(1<<16)-1;for(let i=0,h=0;i<this.numItems;i++){const l=t[h++],a=t[h++],c=t[h++],u=t[h++],f=Math.floor(o*((l+c)/2-this.minX)/e),p=Math.floor(o*((a+u)/2-this.minY)/n);r[i]=_r(f,p)}Se(r,t,this._indices,0,this.numItems-1,this.nodeSize);for(let i=0,h=0;i<this._levelBounds.length-1;i++){const l=this._levelBounds[i];for(;h<l;){const a=h;let c=t[h++],u=t[h++],f=t[h++],p=t[h++];for(let g=1;g<this.nodeSize&&h<l;g++)c=Math.min(c,t[h++]),u=Math.min(u,t[h++]),f=Math.max(f,t[h++]),p=Math.max(p,t[h++]);this._indices[this._pos>>2]=a,t[this._pos++]=c,t[this._pos++]=u,t[this._pos++]=f,t[this._pos++]=p}}}search(t,e,n,r,o){if(this._pos!==this._boxes.length)throw new Error("Data not yet indexed - call index.finish().");let i=this._boxes.length-4;const h=[],l=[];for(;i!==void 0;){const a=Math.min(i+this.nodeSize*4,kn(i,this._levelBounds));for(let c=i;c<a;c+=4){if(n<this._boxes[c]||r<this._boxes[c+1]||t>this._boxes[c+2]||e>this._boxes[c+3])continue;const u=this._indices[c>>2]|0;i>=this.numItems*4?h.push(u):(o===void 0||o(u))&&l.push(u)}i=h.pop()}return l}neighbors(t,e,n=1/0,r=1/0,o){if(this._pos!==this._boxes.length)throw new Error("Data not yet indexed - call index.finish().");let i=this._boxes.length-4;const h=this._queue,l=[],a=r*r;for(;i!==void 0;){const c=Math.min(i+this.nodeSize*4,kn(i,this._levelBounds));for(let u=i;u<c;u+=4){const f=this._indices[u>>2]|0,p=Sn(t,this._boxes[u],this._boxes[u+2]),g=Sn(e,this._boxes[u+1],this._boxes[u+3]),w=p*p+g*g;i>=this.numItems*4?h.push(f<<1,w):(o===void 0||o(f))&&h.push((f<<1)+1,w)}for(;h.length&&h.peek()&1;)if(h.peekValue()>a||(l.push(h.pop()>>1),l.length===n))return h.clear(),l;i=h.pop()>>1}return h.clear(),l}}function Sn(s,t,e){return s<t?t-s:s<=e?0:s-e}function kn(s,t){let e=0,n=t.length-1;for(;e<n;){const r=e+n>>1;t[r]>s?n=r:e=r+1}return t[e]}function Se(s,t,e,n,r,o){if(Math.floor(n/o)>=Math.floor(r/o))return;const i=s[n+r>>1];let h=n-1,l=r+1;for(;;){do h++;while(s[h]<i);do l--;while(s[l]>i);if(h>=l)break;$r(s,t,e,h,l)}Se(s,t,e,n,l,o),Se(s,t,e,l+1,r,o)}function $r(s,t,e,n,r){const o=s[n];s[n]=s[r],s[r]=o;const i=4*n,h=4*r,l=t[i],a=t[i+1],c=t[i+2],u=t[i+3];t[i]=t[h],t[i+1]=t[h+1],t[i+2]=t[h+2],t[i+3]=t[h+3],t[h]=l,t[h+1]=a,t[h+2]=c,t[h+3]=u;const f=e[n];e[n]=e[r],e[r]=f}function _r(s,t){let e=s^t,n=65535^e,r=65535^(s|t),o=s&(t^65535),i=e|n>>1,h=e>>1^e,l=r>>1^n&o>>1^r,a=e&r>>1^o>>1^o;e=i,n=h,r=l,o=a,i=e&e>>2^n&n>>2,h=e&n>>2^n&(e^n)>>2,l^=e&r>>2^n&o>>2,a^=n&r>>2^(e^n)&o>>2,e=i,n=h,r=l,o=a,i=e&e>>4^n&n>>4,h=e&n>>4^n&(e^n)>>4,l^=e&r>>4^n&o>>4,a^=n&r>>4^(e^n)&o>>4,e=i,n=h,r=l,o=a,l^=e&r>>8^n&o>>8,a^=n&r>>8^(e^n)&o>>8,e=l^l>>1,n=a^a>>1;let c=s^t,u=n|65535^(c|e);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 Cn(s,t=1e-7){if(s.length===0)return[];if(s.length===1)return[s];const e=new Ee(s.length);s.forEach(o=>{const[i,h]=o.firstPoint;e.add(i-t,h-t,i+t,h+t)}),e.finish();const n=[],r=new Set;return s.forEach((o,i)=>{if(r.has(i))return;const h=[o];let l=i;r.add(i);let a=s.length;for(;;){if(a--<0)throw new Error("Infinite loop detected");const c=h[h.length-1].lastPoint,[u,f]=c,p=e.search(u-t,f-t,u+t,f+t),g=d=>Math.abs((l-d)%s.length),w=p.filter(d=>!r.has(d)).map(d=>[s[d],d,g(d)]).sort(([,,d],[,,M])=>g(d)-g(M));if(w.length===0){n.push(h);break}const[P,m]=w[0];h.push(P),r.add(m),l=m}}),n}class Q extends At{constructor(t,e=[],{ignoreChecks:n=!1}={}){super(),n||Lr(t,e),this.contour=t,this.holes=e}get boundingBox(){return this.contour.boundingBox}get isFull(){return this.holes.length===0}get allLoops(){return[this.contour,...this.holes]}clone(){return new Q(this.contour.clone(),this.holes.map(t=>t.clone()))}transform(t){return new Q(this.contour.transform(t),this.holes.map(e=>e.transform(t)))}contains(t){return this.contour.contains(t)&&!this.holes.some(e=>e.contains(t))}intersects(t){return this.allLoops.some(e=>t.allLoops.some(n=>e.intersects(n)))}overlappingStrands(t){const e=t instanceof Q?t.allLoops:[t],n=this.allLoops.flatMap(r=>e.flatMap(o=>r.overlappingSegments(o)));return Cn(n).map(r=>new Pt(r))}}function Lr(s,t=[]){if(!s)throw new Error("Figure must have a contour");for(const[e,n]of be([s,...t]))if(e.intersects(n))throw new Error("Loops in a figure must not intersect");if(t.some(e=>!s.contains(e.firstPoint)&&!s.onStroke(e.firstPoint)))throw new Error("Holes must be inside the contour");for(const[e,n]of be(t))if(e.contains(n.firstPoint))throw console.error(An(e),An(n)),new Error("Holes must not be inside other holes")}const Br=s=>{const t=s.map((r,o)=>s.slice(o+1).map((i,h)=>[h+o+1,i]).filter(([,i])=>r.boundingBox.overlaps(i.boundingBox)).map(([i])=>i)),e=[],n=Array(t.length);return t.forEach((r,o)=>{let i=n[o];i||(i=[],e.push(i)),i.push(s[o]),r.length&&r.forEach(h=>{n[h]=i})}),e},Tn=s=>s.map((t,e)=>{const n=t.segments[0].midPoint,r=s.filter((o,i)=>e===i?!1:o.contains(n));return{loop:t,isIn:r}}),jr=(s,t)=>s.flatMap(({loop:e})=>ke(t.filter(({loop:n,isIn:r})=>n===e||r.indexOf(e)!==-1))),qr=(s,t)=>{const e=t.filter(({isIn:r})=>r.length<=1),n=ke(Tn(s.map(({loop:r})=>r)));return[e,...n]},ke=s=>{if(!s.length)return[];const t=s.filter(({isIn:n})=>!n.length),e=s.filter(({isIn:n})=>n.length>1);return t.length===1&&e.length===0?[s]:t.length>1?jr(t,s):qr(e,s)};function Ut(s){return Br(s).map(Tn).flatMap(ke).map(t=>{if(t.length===1)return new Q(t[0].loop);t.sort((r,o)=>r.isIn.length-o.isIn.length);const[e,...n]=t.map(({loop:r})=>r);return new Q(e,n)})}function Or(s,t){const e=[];for(const n of s)for(const r of t)e.push([n,r]);return e}function*Ce(s,t,e){const n=i=>t.some(h=>y(h,i.lastPoint)),r=i=>e.some(h=>i.isSame(h));let o=[];for(const i of s)n(i)?(o.push(i),yield new Pt(o,{ignoreChecks:!0}),o=[]):r(i)?(o.length&&(yield new Pt(o,{ignoreChecks:!0}),o=[]),yield new Pt([i],{ignoreChecks:!0})):o.push(i);o.length&&(yield new Pt(o,{ignoreChecks:!0}))}const Rn=(s,t)=>{const e=s.findIndex(r=>y(t,r.firstPoint)),n=s.slice(0,e);return s.slice(e).concat(n)},Fn=(s,t)=>{let e=s;const n=i=>y(i.firstPoint,t.firstPoint)&&y(i.lastPoint,t.lastPoint);let r=s.findIndex(n);if(r===-1){const i=s.map(h=>h.reverse());if(i.reverse(),r=i.findIndex(n),r===-1)throw console.error(i.map(h=>h.repr),t.repr),new Error("Failed to rotate to segment start");e=i}const o=e.slice(0,r);return e.slice(r).concat(o)};function Nr(s,t,e){return s.filter(n=>{const r=t.filter(i=>y(i.firstPoint,n)||y(i.lastPoint,n));if(r.length%2)throw new Error("Bug in the intersection algo on non crossing point");const o=r.map(i=>e.contains(i.midPoint));return!(o.every(i=>i)||!o.some(i=>i))})}function Vr(s,t,e){let n=[];const r=[],o=new Array(s.segments.length).fill(0).map(()=>[]),i=new Array(t.segments.length).fill(0).map(()=>[]);if(s.segments.forEach((f,p)=>{t.segments.forEach((g,w)=>{const{intersections:P,overlaps:m}=zt(f,g,e);n.push(...P),o[p].push(...P),i[w].push(...P),r.push(...m);const d=m.flatMap(M=>[M.firstPoint,M.lastPoint]);n.push(...d),o[p].push(...d),i[w].push(...d)})}),n=mt(n,e),!n.length||n.length===1)return null;const h=([f,p])=>p.length?f.splitAt(p):[f];let l=at([s.segments,o]).flatMap(h),a=at([t.segments,i]).flatMap(h);if(n=Nr(n,l,t),!n.length&&!r.length)return null;if(r.length){const f=r[0];l=Fn(l,f),a=Fn(a,f)}else{const f=n[0];l=Rn(l,f),a=Rn(a,f)}let c=Array.from(Ce(l,n,r)),u=Array.from(Ce(a,n,r));return(!y(u[0].lastPoint,c[0].lastPoint)||r.length>0&&u[0].segmentsCount!==1)&&(u=u.map(f=>f.reverse()).reverse(),y(u[0].lastPoint,c[0].lastPoint)||(c=c.map(f=>f.reverse()).reverse())),at([c,u]).map(([f,p])=>f.segmentsCount===1&&r.some(g=>f.segments[0].isSame(g))?[f,"same"]:[f,p])}function In(s){let t=s[0];for(const e of s.slice(1))t=t.extend(e);if(!y(t.firstPoint,t.lastPoint))throw console.error(H(t.firstPoint),H(t.lastPoint)),new Error("Bug in the intersection algo on non closing strand");return new it(t.segments)}function zr(s,t){const e=at([t.slice(0,-1),t.slice(1)]).map(([r,o])=>In(s.slice(r,o)));let n=s.slice(t[t.length-1]);return t[0]!==0&&(n=n.concat(s.slice(0,t[0]))),e.push(In(n)),e}function Ur(s){if(!s.length)return[];const t=s.map(r=>r.firstPoint);let e=s.map(r=>r.lastPoint);e=e.slice(-1).concat(e.slice(0,-1));const n=at([t,e]).flatMap(([r,o],i)=>y(r,o)?[]:i);try{return zr(s,n)}catch{return Cn(s.flatMap(r=>r.segments)).filter(r=>r.length>1).filter(r=>y(r[0].firstPoint,r.at(-1).lastPoint)).map(r=>new it(r))}}const $n=(s,t)=>{if(s.length===0)return[t];const e=s.at(-1);return y(e.lastPoint,t.firstPoint)?s.slice(0,-1).concat([e.extend(t)]):y(e.lastPoint,t.lastPoint)?s.slice(0,-1).concat([e.extend(t.reverse())]):s.concat([t])},Yr=(s,t)=>s.length===0?[t]:y(s[0].firstPoint,t.lastPoint)?[t.extend(s[0])].concat(s.slice(1)):[t].concat(s);function Te(s,t,{firstInside:e,secondInside:n}){const r=Vr(s,t);if(!r){const l=s.segments[0].midPoint,a=t.contains(l),c=t.segments[0].midPoint,u=s.contains(c);return{identical:!1,firstCurveInSecond:a,secondCurveInFirst:u}}if(r.every(([,l])=>l==="same"))return{identical:!0};let o=null,i=null;const h=r.flatMap(([l,a])=>{let c=[],u=0;if(a==="same")return i===1?(i=1,l):i===2||i===0?(i=null,[]):i===null?(o?o=o.extend(l):o=l,[]):(console.error("weird situation"),[]);const f=l.segments[0].midPoint,p=t.contains(f);(e==="keep"&&p||e==="remove"&&!p)&&(u+=1,c=$n(c,l));const g=a.segments[0].midPoint,w=s.contains(g);if(n==="keep"&&w||n==="remove"&&!w){const P=a;u+=1,u===2&&c.length?(c=$n(c,P),o=null):c=[P]}return i===null&&u===1&&o&&(c=Yr(c,o)),u===1&&(i=u,o=null),c.length?c:(o=null,[])});return Ur(h)}const Dr=(s,t)=>{const e=Te(s,t,{firstInside:"remove",secondInside:"remove"});return Array.isArray(e)?e:e.identical?[s]:e.firstCurveInSecond?[t]:e.secondCurveInFirst?[s]:[s,t]},re=(s,t)=>{const e=Te(s,t,{firstInside:"remove",secondInside:"keep"});return Array.isArray(e)?e:e.identical?[]:e.firstCurveInSecond?[]:e.secondCurveInFirst?[s,t]:[s]},Re=(s,t)=>{const e=Te(s,t,{firstInside:"keep",secondInside:"keep"});return Array.isArray(e)?e:e.identical?[s]:e.firstCurveInSecond?[s]:e.secondCurveInFirst?[t]:[]};function Xr(s){const t=new Map,e=[];return s.forEach((n,r)=>{let o;t.has(r)?o=t.get(r):(o={current:[n],fusedWith:new Set([r])},e.push(o)),s.slice(r+1).forEach((i,h)=>{const l=o.current,a=r+h+1;if(o.fusedWith.has(a))return;let c=[i],u=!1;if(t.has(a)&&(c=t.get(a).current,u=!0),!l.some(p=>c.some(g=>p.intersects(g))))return;let f;l.length>1||c.length>1?f=Yt(l,c):f=_n(l[0],c[0]),o.fusedWith.add(a),o.current=f,u||t.set(a,o)})}),e.flatMap(({current:n})=>n)}function _n(s,t){const e=Dr(s.contour,t.contour),n=t.holes.flatMap(i=>re(i,s.contour)),r=s.holes.flatMap(i=>re(i,t.contour)),o=Or(s.holes,t.holes).flatMap(([i,h])=>Re(i,h));return Ut([...e,...n,...r,...o])}function oe(s,t){if(s.isFull&&t.isFull)return Ut(re(s.contour,t.contour));if(s.isFull){const n=re(s.contour,t.contour),r=t.holes.flatMap(o=>Re(o,s.contour));return Ut([...n,...r])}else if(t.isFull&&!s.contour.intersects(t.contour))if(s.contour.contains(t.contour.firstPoint)){const n=Yt(s.holes.map(r=>new Q(r)),[t]);return Ut([s.contour,...n.flatMap(r=>r.allLoops)])}else return[s];let e=oe(new Q(s.contour),t);return s.holes.forEach(n=>{e=e.flatMap(r=>oe(r,new Q(n)))}),e}function Hr(s,t){const e=Re(s.contour,t.contour);if(!e.length)return[];let n=Ut(e);return n=$t(n,s.holes.map(r=>new Q(r))),$t(n,t.holes.map(r=>new Q(r)))}function Yt(s,t){if(!s.length)return t;if(!t.length)return s;if(s.length===1&&t.length>1||t.length===1&&s.length>1)return Xr([...s,...t]);if(s.length>1&&t.length>1){let e=Yt([s[0]],t);return s.slice(1).forEach(n=>{e=Yt([n],e)}),e}return s.length===1&&t.length===1?_n(s[0],t[0]):[]}function $t(s,t){if(!s.length)return[];if(!t.length)return s;if(s.length===1&&t.length===1)return oe(s[0],t[0]);if(s.length>1)return s.flatMap(n=>$t([n],t));let e=oe(s[0],t[0]);return t.slice(1).forEach(n=>{e=$t(e,[n])}),e}function Fe(s,t){return!s.length||!t.length?[]:s.length===1&&t.length===1?Hr(s[0],t[0]):s.length>1?s.flatMap(e=>Fe([e],t)):t.flatMap(e=>Fe(s,[e]))}class ht extends At{constructor(t=[],{ignoreChecks:e=!1}={}){super(),this._boundingBox=null,e||Wr(t),this.figures=t}get isEmpty(){return this.figures.length===0}get boundingBox(){if(this.isEmpty)return new qt;if(this._boundingBox===null){let t=this.figures[0].boundingBox;for(const e of this.figures.slice(1))t=t.merge(e.boundingBox);this._boundingBox=t}return this._boundingBox}clone(){return new ht(this.figures.map(t=>t.clone()))}transform(t){return new ht(this.figures.map(e=>e.transform(t)))}contains(t){return this.figures.some(e=>e.contains(t))}intersects(t){return this.figures.some(e=>t.figures.some(n=>e.intersects(n)))}overlappingStrands(t){return this.figures.flatMap(e=>t instanceof ht?t.figures.flatMap(n=>e.overlappingStrands(n)):e.overlappingStrands(t))}fuse(t){return new ht(Yt(this.figures,t.figures))}cut(t){return new ht($t(this.figures,t.figures))}intersect(t){return new ht(Fe(this.figures,t.figures))}}function Wr(s){for(const[t,e]of be(s))if(t.intersects(e))throw new Error("Diagram figures must not intersect")}let Ie=class{constructor(t,e){this.firstPoint=t,this.lastPoint=e}};function Ln(s,t){if(s instanceof B)return Jr(s,t);if(s instanceof T)return Zr(s,t);throw new Error("Not implemented")}function Jr(s,t){const{firstPoint:e,lastPoint:n}=s,r=s.normalVector;return new B(V(e,W(r,t)),V(n,W(r,t)))}function Zr(s,t){const e=V(s.firstPoint,W(G(s.tangentAtFirstPoint),t)),n=V(s.lastPoint,W(G(s.tangentAtLastPoint),t)),r=t*(s.clockwise?1:-1);return s.radius+r<s.precision?new Ie(e,n):new T(e,n,s.center,s.clockwise)}function Bn(s,t,e){const n=dt(s.tangentAtLastPoint,t.tangentAtFirstPoint);if(Math.abs(n)<1e-10)return null;const r=n>0?1:-1,o=Math.abs(e)*r,i=Ln(s,o),h=Ln(t,o);if(i instanceof Ie||h instanceof Ie)return null;let l;try{l=xr(i,h,1e-9).at(-1)}catch{return null}if(!l)return null;const a=l,c=(p,g)=>{const w=g.tangentAt(a),P=Ot(w),m=V(a,W(P,o));return p.splitAt(m)},[u]=c(s,i),[,f]=c(t,h);return{first:u,second:f,center:a}}function jn(s,t,e){const n=Bn(s,t,e);if(!n)return console.warn("Cannot fillet between segments",s.repr,t.repr),[s,t];const{first:r,second:o}=n;return[r,Ye(r.lastPoint,o.firstPoint,r.tangentAtLastPoint),o]}function qn(s,t,e){const n=Bn(s,t,e);if(!n)return console.warn("Cannot chamfer between segments",s.repr,t.repr),[s,t];const{first:r,second:o}=n;return[r,new B(r.lastPoint,o.firstPoint),o]}const Gr=s=>{let t;s?typeof s=="number"||Array.isArray(s)&&s.length===2?t={endTangent:s}:t={endTangent:0,...s}:t={endTangent:[1,0]};const{endTangent:e,startFactor:n=1,endFactor:r=1,startTangent:o}=t;let i;typeof e=="number"?i=gt(1,e*xt):i=e;let h;return typeof o=="number"?h=gt(1,o*xt):h=o,{endTangent:i,startFactor:n,endFactor:r,startTangent:h}};function On(s,{ignoreChecks:t=!1}={}){return new ht([new Q(new it([...s],{ignoreChecks:t}))])}let Kr=class{constructor(t=[0,0]){this.pointer=t,this.firstPoint=t,this.pendingSegments=[],this._nextCorner=null}movePointerTo(t){if(this.pendingSegments.length)throw new Error("You can only move the pointer if there is no segment defined");return this.pointer=t,this.firstPoint=t,this}saveSegment(t){if(y(t.firstPoint,t.lastPoint))throw new Error(`Segment has no length, ${t.repr}`);if(!this._nextCorner)return this.pendingSegments.push(t),this;const e=this.pendingSegments.pop();if(!e)throw new Error("bug in the custom corner algorithm");const n=this._nextCorner.mode==="chamfer"?qn:jn;return this.pendingSegments.push(...n(e,t,this._nextCorner.radius)),this._nextCorner=null,this}lineTo(t){const e=new B(this.pointer,t);return this.pointer=t,this.saveSegment(e)}line(t,e){return this.lineTo([this.pointer[0]+t,this.pointer[1]+e])}vLine(t){return this.line(0,t)}hLine(t){return this.line(t,0)}vLineTo(t){return this.lineTo([this.pointer[0],t])}hLineTo(t){return this.lineTo([t,this.pointer[1]])}polarLineTo([t,e]){const n=e*xt,r=gt(t,n);return this.lineTo(r)}polarLine(t,e){const n=e*xt,[r,o]=gt(t,n);return this.line(r,o)}tangentLine(t){const e=this.pendingSegments.at(-1);if(!e)throw new Error("You need a previous segment to sketch a tangent line");const[n,r]=e.tangentAtLastPoint;return this.line(n*t,r*t)}threePointsArcTo(t,e){return this.saveSegment(ls(this.pointer,e,t)),this.pointer=t,this}threePointsArc(t,e,n,r){const[o,i]=this.pointer;return this.threePointsArcTo([o+t,i+e],[o+n,i+r])}sagittaArcTo(t,e){if(!e)return this.lineTo(t);const n=new B(this.pointer,t),r=G(n.tangentAtFirstPoint),o=V(n.midPoint,W(r,e));return this.threePointsArcTo(t,o)}sagittaArc(t,e,n){return this.sagittaArcTo([t+this.pointer[0],e+this.pointer[1]],n)}vSagittaArc(t,e){return this.sagittaArc(0,t,e)}hSagittaArc(t,e){return this.sagittaArc(t,0,e)}bulgeArcTo(t,e){if(!e)return this.lineTo(t);const n=z(this.pointer,t)/2,r=-e*n;return this.sagittaArcTo(t,r)}bulgeArc(t,e,n){return this.bulgeArcTo([t+this.pointer[0],e+this.pointer[1]],n)}vBulgeArc(t,e){return this.bulgeArc(0,t,e)}hBulgeArc(t,e){return this.bulgeArc(t,0,e)}tangentArcTo(t,e){const n=this.pendingSegments.at(-1);if(!n)throw new Error("You need a previous curve to sketch a tangent arc");return this.saveSegment(Ye(this.pointer,t,e??n.tangentAtLastPoint)),this.pointer=t,this}tangentArc(t,e,n){const[r,o]=this.pointer;return this.tangentArcTo([t+r,e+o],n)}ellipseTo(t,e,n,r,o,i){return this.saveSegment(tr(this.pointer,t,e,n,r,o,i)),this.pointer=t,this}ellipse(t,e,n,r,o,i,h){return this.ellipseTo([t+this.pointer[0],e+this.pointer[1]],n,r,o,i,h)}halfEllipseTo(t,e){const[n,r]=ze(q(t,this.pointer));return this.ellipseTo(t,n/2,Math.abs(e),r*ge,!0,e>0)}halfEllipse(t,e,n){return this.halfEllipseTo([t+this.pointer[0],e+this.pointer[1]],n)}cubicBezierCurveTo(t,e,n){return this.saveSegment(new K(this.pointer,t,e,n)),this.pointer=t,this}quadraticBezierCurveTo(t,e){return this.saveSegment(new N(this.pointer,t,e)),this.pointer=t,this}smoothCurveTo(t,e){const{endTangent:n,startTangent:r,startFactor:o,endFactor:i}=Gr(e),h=this.pendingSegments.length?this.pendingSegments[this.pendingSegments.length-1]:null,l=z(this.pointer,t)/3;let a;r?a=r:h?a=h.tangentAtLastPoint:a=[1,0],a=J(a);const c=[this.pointer[0]+a[0]*o*l,this.pointer[1]+a[1]*o*l];let u=n;u=J(u);const f=[t[0]-u[0]*i*l,t[1]-u[1]*i*l];return this.cubicBezierCurveTo(t,c,f)}smoothCurve(t,e,n){return this.smoothCurveTo([t+this.pointer[0],e+this.pointer[1]],n)}customCorner(t,e="fillet"){if(!this.pendingSegments.length)throw new Error("You need a segment defined to fillet the angle");return t?(this._nextCorner={mode:e,radius:t},this):this}_customCornerLastWithFirst(t,e="fillet"){if(!t)return;const n=this.pendingSegments.pop(),r=this.pendingSegments.shift();if(!n||!r)throw new Error("Not enough curves to close and fillet");const o=e==="chamfer"?qn:jn;this.pendingSegments.push(...o(n,r,t))}close(t=!1){if(!this.pendingSegments.length)throw new Error("No segments to close");const e=this.pendingSegments[0],n=this.pendingSegments.at(-1);return y(e.firstPoint,n.lastPoint)||this.lineTo(e.firstPoint),this._nextCorner!==null&&(this._customCornerLastWithFirst(this._nextCorner.radius,this._nextCorner.mode),this._nextCorner=null),On(this.pendingSegments,{ignoreChecks:t})}closeWithMirror(t=!1){if(!this.pendingSegments.length)throw new Error("No segments to close");const e=this.pendingSegments[0],n=this.pendingSegments.at(-1),r=q(n.lastPoint,e.firstPoint),o=new tt().mirrorLine(r,e.firstPoint),i=this.pendingSegments.map(h=>h.transform(o).reverse());return i.reverse(),On([...this.pendingSegments,...i],{ignoreChecks:t})}asStrand(){return new Pt([...this.pendingSegments])}get isClosed(){var t;return y(this.pointer,(t=this.pendingSegments[0])==null?void 0:t.firstPoint)}};function Nn(s=[0,0]){return new Kr(s)}function Vn(s){if(s instanceof Q)return[s];if(s instanceof it)return[new Q(s)];if(s instanceof ht)return s.figures;throw new Error("Unknown shape")}function zn(s,t,e=1e-9){let n=[];const r=[],o=new Array(t.segments.length).fill(0).map(()=>[]);t.segments.forEach((h,l)=>{s.segments.forEach(a=>{const{intersections:c,overlaps:u}=zt(h,a,e);n.push(...c),o[l].push(...c),r.push(...u);const f=u.flatMap(p=>[p.firstPoint,p.lastPoint]);n.push(...f),o[l].push(...f)})}),n=mt(n,e);const i=at([t.segments,o]).flatMap(([h,l])=>l.length?h.splitAt(l):[h]);return Array.from(Ce(i,n,r))}function Un(s,t,e=!1){return zn(t,s).filter(n=>{const r=n.segments[0].midPoint;return t.onStroke(r)?!e:!t.contains(r)})}function Qr(s,t,e=!1){return zn(t,s).filter(n=>{const r=n.segments[0].midPoint;return t.onStroke(r)?!e:t.contains(r)})}function Yn(s,t,e=!1){const n=Un(s,t.contour,e),r=t.holes.flatMap(o=>Qr(s,o,e));return[...n,...r]}function to(s,t){return new ht($t(Vn(s),Vn(t)))}function $e(s,t,e=!0){if(t instanceof it)return Un(s,t,e);if(t instanceof Q)return Yn(s,t,e);let n=[new Pt([...s.segments])];return t.figures.forEach(r=>{n=n.flatMap(o=>Yn(o,r,e))}),n}function Mt([s,t]){return`${s} ${t}`}function _e(s){if(s instanceof B)return`L ${Mt(s.lastPoint)}`;if(s instanceof T)return`A ${s.radius} ${s.radius} 0 ${s.angularLength>Math.PI?"1":"0"} ${s.clockwise?"0":"1"} ${Mt(s.lastPoint)}`;if(s instanceof Z)return`A ${s.majorRadius} ${s.minorRadius} ${s.tiltAngle*ge} ${s.deltaAngle>Math.PI?"1":"0"} ${s.clockwise?"0":"1"} ${Mt(s.lastPoint)}`;if(s instanceof N)return`Q ${[Mt(s.controlPoint),Mt(s.lastPoint)].join(" ")}`;if(s instanceof K)return`C ${[Mt(s.firstControlPoint),Mt(s.lastControlPoint),Mt(s.lastPoint)].join(" ")}`;throw new Error("Unknown segment type")}function Dn(s){const t=`M ${s.firstPoint.join(" ")}`,e=s.segments.map(_e).join(" ");return`${t} ${e} Z`}function Xn(s){return`<path d="${s.allLoops.map(Dn).join(" ")}" />`}function eo(s){return`<g>
|
|
11
|
-
${
|
|
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(e=>e.isOnSegment(t))}intersects(t){return this.boundingBox.overlaps(t.boundingBox)?this.segments.some(e=>t.segments.some(s=>Gt(e,s).count>0)):!1}overlappingSegments(t){return this.segments.flatMap(e=>t.segments.flatMap(s=>e.boundingBox.overlaps(s.boundingBox)?Gt(e,s).overlaps:[]))}get boundingBox(){if(this._boundingBox===null){let t=this.segments[0].boundingBox;this.segments.slice(1).forEach(e=>{t=t.merge(e.boundingBox)}),this._boundingBox=t}return this._boundingBox}[Symbol.for("nodejs.util.inspect.custom")](){return this.repr}}function Ln(i,t="Stroke"){vs(i.length).forEach(([e,s])=>{if(e===s)return;const n=i[e],r=i[s],o=Gt(n,r),h=Math.max(n.precision,r.precision);if(o.count!==0){if(o.count===1&&!o.overlaps.length){const l=e-s,a=o.intersections[0];if(l===1&&y(n.firstPoint,a,h)||l===-1&&y(n.lastPoint,a,h)||l===i.length-1&&y(n.lastPoint,a,h)&&y(r.firstPoint,a,h)||-l===i.length-1&&y(n.firstPoint,a,h)&&y(r.lastPoint,a,h))return}if(!(o.count===2&&i.length===2&&(y(n.firstPoint,o.intersections[0],h)&&y(n.lastPoint,o.intersections[1],h)||y(n.firstPoint,o.intersections[1],h)&&y(n.lastPoint,o.intersections[0],h))))throw new Error(`${t} segments must not intersect, but segments ${n.info} and ${r.info} do at ${JSON.stringify(o.intersections)}`)}})}function Es(i,t="Stroke"){if(i.length===0)throw new Error(`${t} must have at least one segment`);mt([i.slice(0,-1),i.slice(1)]).forEach(([e,s])=>{if(!y(e.lastPoint,s.firstPoint))throw new Error(`${t} segments must be connected, but ${e.info} and ${s.info} are not`)}),Ln(i,t)}function Ss(i,t){return!!(i instanceof B&&t instanceof B&&Ee(i.V,t.V)||i instanceof L&&t instanceof L&&y(i.center,t.center)&&i.radius-t.radius<i.precision)}function ks(i,t){if(i instanceof B&&t instanceof B)return new B(i.firstPoint,t.lastPoint);if(i instanceof L&&t instanceof L)return new L(i.firstPoint,t.lastPoint,i.center,i.clockwise);throw new Error("Not implemented")}function $s(i){let t=!1;const e=[];for(const s of i.segments){if(e.length===0){e.push(s);continue}const n=e[e.length-1];Ss(n,s)?(t=!0,e.pop(),e.push(ks(n,s))):e.push(s)}if(y(i.firstPoint,i.lastPoint)&&Ss(e[0],e[e.length-1])){t=!0;const s=e.pop();e[0]=ks(s,e[0])}return t?e:null}class ot extends As{constructor(){super(...arguments),st(this,"strokeType","STRAND")}reverse(){const t=this.segments.map(e=>e.reverse());return t.reverse(),new ot(t,{ignoreChecks:!0})}clone(){return new ot(this.segments.map(t=>t.clone()),{ignoreChecks:!0})}extend(t){if(!y(this.lastPoint,t.firstPoint))throw console.error(this.repr,t.repr),new Error("Cannot extend strand: connection point is not the same");return new ot([...this.segments,...t.segments])}simplify(){const t=$s(this);return t?new ot(t,{ignoreChecks:!0}):this}transform(t){return new ot(this.segments.map(e=>e.transform(t)),{ignoreChecks:!0})}}const Bn=(i,t)=>{const e=ee(t,{V:[1,0],firstPoint:i,precision:t.precision});if(e==="parallel")return 0;const{intersectionParam1:s,intersectionParam2:n}=e;if(!t.isValidParameter(s)||n<=-t.precision)return 0;if(Math.abs(s)<t.precision||Math.abs(s-1)<t.precision){const[,r]=t.midPoint;return i[1]-r<0?1:0}return 1};class Te{constructor(t){st(this,"_count",0),st(this,"segment"),this.segment=t}update(t,e=!1){!e&&!this.segment.isOnSegment(t)||(y(t,this.segment.firstPoint)?this._count+=this.segment.tangentAtFirstPoint[1]>0?1:0:y(t,this.segment.lastPoint)?this._count+=this.segment.tangentAtLastPoint[1]>0?0:1:this._count+=1)}get count(){return this._count}}const jn=(i,t)=>{const e=t.precision,s=Math.abs(i[1]-t.center[1]);if(s>t.radius+e)return 0;const n=kt(i,t.center),r=t.radius*t.radius,o=e*e;if(Math.abs(n-r)<o&&t.isOnSegment(i))return 0;const h=n-r>o;if(h&&t.center[0]<i[0])return 0;const l=Math.sqrt(t.radius*t.radius-s*s),a=new Te(t);return a.update([t.center[0]+l,i[1]]),h&&a.update([t.center[0]-l,i[1]]),a.count},qn=(i,t)=>{const e=t.boundingBox.xMax+t.boundingBox.width/2,s=new B(i,[e,i[1]]),n=new Te(t);return $e(s,t).forEach(r=>{n.update(r,!0)}),n.count},Vn=(i,t)=>{const e=new Te(t);return t.paramsAtY(i[1]).map(s=>{try{return t.paramPoint(s)}catch{return null}}).filter(s=>s!==null).filter(s=>{const[n]=s;return n>=i[0]}).forEach(s=>{e.update(s,!0)}),e.count};function On(i,t){if(t instanceof B)return Bn(i,t);if(t instanceof L)return jn(i,t);if(t instanceof J)return qn(i,t);if(t instanceof et||t instanceof K)return Vn(i,t);throw new Error("Not implemented")}class ct extends As{constructor(t,{ignoreChecks:e=!1}={}){super(t,{ignoreChecks:!0}),st(this,"strokeType","LOOP"),st(this,"_clockwise",null),e||Nn(t)}get clockwise(){if(this._clockwise===null){const t=this.segments.flatMap(s=>s instanceof B?[s.firstPoint]:[s.firstPoint,s.paramPoint(.5)]),e=t.map((s,n)=>{const r=t[(n+1)%t.length];return(r[0]-s[0])*(r[1]+s[1])}).reduce((s,n)=>s+n,0);this._clockwise=e>0}return this._clockwise}clone(){return new ct(this.segments.map(t=>t.clone()),{ignoreChecks:!0})}reverse(){const t=this.segments.map(e=>e.reverse());return t.reverse(),new ct(t,{ignoreChecks:!0})}transform(t){return new ct(this.segments.map(e=>e.transform(t)),{ignoreChecks:!0})}contains(t){return this.onStroke(t)||!this.boundingBox.contains(t)?!1:this.segments.reduce((e,s)=>e+On(t,s),0)%2===1}simplify(){const t=$s(this);return t?new ct(t,{ignoreChecks:!0}):this}}function Nn(i){if(Es(i,"Loop"),!y(i[0].firstPoint,i[i.length-1].lastPoint))throw new Error("Loop segment must be closed")}const zn=[B,L,J,K,et];function Cs(i){return zn.some(t=>i instanceof t)}function Rs(i){if(i instanceof B)return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint};if(i instanceof L)return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint,center:i.center,clockwise:i.clockwise};if(i instanceof J)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 K)return{type:i.segmentType,firstPoint:i.firstPoint,lastPoint:i.lastPoint,controlPoint:i.controlPoint};if(i instanceof et)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(Rs)}}function Ts(i){return{type:"FIGURE",contour:Fe(i.contour),holes:i.holes.map(Fe)}}function Un(i){return{type:"DIAGRAM",figures:i.figures.map(Ts)}}function Fs(i){if(i instanceof ft)return Un(i);if(i instanceof ut)return Ts(i);if(i instanceof ct)return Fe(i);if(Cs(i))return Rs(i);throw new Error("Unknown shape type")}class Yn{constructor(){this.ids=[],this.values=[],this.length=0}clear(){this.length=0}push(t,e){let s=this.length++;for(;s>0;){const n=s-1>>1,r=this.values[n];if(e>=r)break;this.ids[s]=this.ids[n],this.values[s]=r,s=n}this.ids[s]=t,this.values[s]=e}pop(){if(this.length===0)return;const t=this.ids[0];if(this.length--,this.length>0){const e=this.ids[0]=this.ids[this.length],s=this.values[0]=this.values[this.length],n=this.length>>1;let r=0;for(;r<n;){let o=(r<<1)+1;const h=o+1;let l=this.ids[o],a=this.values[o];const c=this.values[h];if(h<this.length&&c<a&&(o=h,l=this.ids[h],a=c),a>=s)break;this.ids[r]=l,this.values[r]=a,r=o}this.ids[r]=e,this.values[r]=s}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 Is=[Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array],Ie=3;class _e{static from(t){if(!t||t.byteLength===void 0||t.buffer)throw new Error("Data must be an instance of ArrayBuffer or SharedArrayBuffer.");const[e,s]=new Uint8Array(t,0,2);if(e!==251)throw new Error("Data does not appear to be in a Flatbush format.");if(s>>4!==Ie)throw new Error(`Got v${s>>4} data when expected v${Ie}.`);const[n]=new Uint16Array(t,2,1),[r]=new Uint32Array(t,4,1);return new _e(r,n,Is[s&15],void 0,t)}constructor(t,e=16,s=Float64Array,n=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(+e,2),65535);let o=t,h=o;this._levelBounds=[o*4];do o=Math.ceil(o/this.nodeSize),h+=o,this._levelBounds.push(h*4);while(o!==1);this.ArrayType=s||Float64Array,this.IndexArrayType=h<16384?Uint16Array:Uint32Array;const l=Is.indexOf(this.ArrayType),a=h*4*this.ArrayType.BYTES_PER_ELEMENT;if(l<0)throw new Error(`Unexpected typed array class: ${s}.`);r&&r.byteLength!==void 0&&!r.buffer?(this.data=r,this._boxes=new this.ArrayType(this.data,8,h*4),this._indices=new this.IndexArrayType(this.data,8+a,h),this._pos=h*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 n(8+a+h*this.IndexArrayType.BYTES_PER_ELEMENT),this._boxes=new this.ArrayType(this.data,8,h*4),this._indices=new this.IndexArrayType(this.data,8+a,h),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,(Ie<<4)+l]),new Uint16Array(this.data,2,1)[0]=e,new Uint32Array(this.data,4,1)[0]=t),this._queue=new Yn}add(t,e,s,n){const r=this._pos>>2,o=this._boxes;return this._indices[r]=r,o[this._pos++]=t,o[this._pos++]=e,o[this._pos++]=s,o[this._pos++]=n,t<this.minX&&(this.minX=t),e<this.minY&&(this.minY=e),s>this.maxX&&(this.maxX=s),n>this.maxY&&(this.maxY=n),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 e=this.maxX-this.minX||1,s=this.maxY-this.minY||1,n=new Uint32Array(this.numItems),r=65535;for(let o=0,h=0;o<this.numItems;o++){const l=t[h++],a=t[h++],c=t[h++],u=t[h++],f=Math.floor(r*((l+c)/2-this.minX)/e),p=Math.floor(r*((a+u)/2-this.minY)/s);n[o]=Xn(f,p)}Le(n,t,this._indices,0,this.numItems-1,this.nodeSize);for(let o=0,h=0;o<this._levelBounds.length-1;o++){const l=this._levelBounds[o];for(;h<l;){const a=h;let c=t[h++],u=t[h++],f=t[h++],p=t[h++];for(let g=1;g<this.nodeSize&&h<l;g++)c=Math.min(c,t[h++]),u=Math.min(u,t[h++]),f=Math.max(f,t[h++]),p=Math.max(p,t[h++]);this._indices[this._pos>>2]=a,t[this._pos++]=c,t[this._pos++]=u,t[this._pos++]=f,t[this._pos++]=p}}}search(t,e,s,n,r){if(this._pos!==this._boxes.length)throw new Error("Data not yet indexed - call index.finish().");let o=this._boxes.length-4;const h=[],l=[];for(;o!==void 0;){const a=Math.min(o+this.nodeSize*4,Ls(o,this._levelBounds));for(let c=o;c<a;c+=4){if(s<this._boxes[c]||n<this._boxes[c+1]||t>this._boxes[c+2]||e>this._boxes[c+3])continue;const u=this._indices[c>>2]|0;o>=this.numItems*4?h.push(u):(r===void 0||r(u))&&l.push(u)}o=h.pop()}return l}neighbors(t,e,s=1/0,n=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 h=this._queue,l=[],a=n*n;for(;o!==void 0;){const c=Math.min(o+this.nodeSize*4,Ls(o,this._levelBounds));for(let u=o;u<c;u+=4){const f=this._indices[u>>2]|0,p=_s(t,this._boxes[u],this._boxes[u+2]),g=_s(e,this._boxes[u+1],this._boxes[u+3]),w=p*p+g*g;o>=this.numItems*4?h.push(f<<1,w):(r===void 0||r(f))&&h.push((f<<1)+1,w)}for(;h.length&&h.peek()&1;)if(h.peekValue()>a||(l.push(h.pop()>>1),l.length===s))return h.clear(),l;o=h.pop()>>1}return h.clear(),l}}function _s(i,t,e){return i<t?t-i:i<=e?0:i-e}function Ls(i,t){let e=0,s=t.length-1;for(;e<s;){const n=e+s>>1;t[n]>i?s=n:e=n+1}return t[e]}function Le(i,t,e,s,n,r){if(Math.floor(s/r)>=Math.floor(n/r))return;const o=i[s+n>>1];let h=s-1,l=n+1;for(;;){do h++;while(i[h]<o);do l--;while(i[l]>o);if(h>=l)break;Dn(i,t,e,h,l)}Le(i,t,e,s,l,r),Le(i,t,e,l+1,n,r)}function Dn(i,t,e,s,n){const r=i[s];i[s]=i[n],i[n]=r;const o=4*s,h=4*n,l=t[o],a=t[o+1],c=t[o+2],u=t[o+3];t[o]=t[h],t[o+1]=t[h+1],t[o+2]=t[h+2],t[o+3]=t[h+3],t[h]=l,t[h+1]=a,t[h+2]=c,t[h+3]=u;const f=e[s];e[s]=e[n],e[n]=f}function Xn(i,t){let e=i^t,s=65535^e,n=65535^(i|t),r=i&(t^65535),o=e|s>>1,h=e>>1^e,l=n>>1^s&r>>1^n,a=e&n>>1^r>>1^r;e=o,s=h,n=l,r=a,o=e&e>>2^s&s>>2,h=e&s>>2^s&(e^s)>>2,l^=e&n>>2^s&r>>2,a^=s&n>>2^(e^s)&r>>2,e=o,s=h,n=l,r=a,o=e&e>>4^s&s>>4,h=e&s>>4^s&(e^s)>>4,l^=e&n>>4^s&r>>4,a^=s&n>>4^(e^s)&r>>4,e=o,s=h,n=l,r=a,l^=e&n>>8^s&r>>8,a^=s&n>>8^(e^s)&r>>8,e=l^l>>1,s=a^a>>1;let c=i^t,u=s|65535^(c|e);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 Bs(i,t=1e-7){if(i.length===0)return[];if(i.length===1)return[i];const e=new _e(i.length);i.forEach(r=>{const[o,h]=r.firstPoint;e.add(o-t,h-t,o+t,h+t)}),e.finish();const s=[],n=new Set;return i.forEach((r,o)=>{if(n.has(o))return;const h=[r];let l=o;n.add(o);let a=i.length;for(;;){if(a--<0)throw new Error("Infinite loop detected");const c=h[h.length-1].lastPoint,[u,f]=c,p=e.search(u-t,f-t,u+t,f+t),g=d=>Math.abs((l-d)%i.length),w=p.filter(d=>!n.has(d)).map(d=>[i[d],d,g(d)]).sort(([,,d],[,,M])=>g(d)-g(M));if(w.length===0){s.push(h);break}const[P,m]=w[0];h.push(P),n.add(m),l=m}}),s}let ut=class be extends $t{constructor(t,e=[],{ignoreChecks:s=!1}={}){super(),st(this,"contour"),st(this,"holes"),s||Hn(t,e),this.contour=t,this.holes=e}get boundingBox(){return this.contour.boundingBox}get isFull(){return this.holes.length===0}get allLoops(){return[this.contour,...this.holes]}clone(){return new be(this.contour.clone(),this.holes.map(t=>t.clone()))}transform(t){return new be(this.contour.transform(t),this.holes.map(e=>e.transform(t)))}contains(t){return this.contour.contains(t)&&!this.holes.some(e=>e.contains(t))}intersects(t){return this.allLoops.some(e=>t.allLoops.some(s=>e.intersects(s)))}overlappingStrands(t){const e=t instanceof be?t.allLoops:[t],s=this.allLoops.flatMap(n=>e.flatMap(r=>n.overlappingSegments(r)));return Bs(s).map(n=>new ot(n))}};function Hn(i,t=[]){if(!i)throw new Error("Figure must have a contour");for(const[e,s]of Re([i,...t]))if(e.intersects(s))throw new Error("Loops in a figure must not intersect");if(t.some(e=>!i.contains(e.firstPoint)&&!i.onStroke(e.firstPoint)))throw new Error("Holes must be inside the contour");for(const[e,s]of Re(t))if(e.contains(s.firstPoint))throw console.error(Fs(e),Fs(s)),new Error("Holes must not be inside other holes")}const Gn=i=>{const t=i.map((n,r)=>i.slice(r+1).map((o,h)=>[h+r+1,o]).filter(([,o])=>n.boundingBox.overlaps(o.boundingBox)).map(([o])=>o)),e=[],s=Array(t.length);return t.forEach((n,r)=>{let o=s[r];o||(o=[],e.push(o)),o.push(i[r]),n.length&&n.forEach(h=>{s[h]=o})}),e},js=i=>i.map((t,e)=>{const s=t.segments[0].midPoint,n=i.filter((r,o)=>e===o?!1:r.contains(s));return{loop:t,isIn:n}}),Wn=(i,t)=>i.flatMap(({loop:e})=>Be(t.filter(({loop:s,isIn:n})=>s===e||n.indexOf(e)!==-1))),Zn=(i,t)=>{const e=t.filter(({isIn:n})=>n.length<=1),s=Be(js(i.map(({loop:n})=>n)));return[e,...s]},Be=i=>{if(!i.length)return[];const t=i.filter(({isIn:s})=>!s.length),e=i.filter(({isIn:s})=>s.length>1);return t.length===1&&e.length===0?[i]:t.length>1?Wn(t,i):Zn(e,i)};function Wt(i){return Gn(i).map(js).flatMap(Be).map(t=>{if(t.length===1)return new ut(t[0].loop);t.sort((n,r)=>n.isIn.length-r.isIn.length);const[e,...s]=t.map(({loop:n})=>n);return new ut(e,s)})}function Qn(i,t){const e=[];for(const s of i)for(const n of t)e.push([s,n]);return e}function*je(i,t,e){const s=o=>t.some(h=>y(h,o.lastPoint)),n=o=>e.some(h=>o.isSame(h));let r=[];for(const o of i)s(o)?(r.push(o),yield new ot(r,{ignoreChecks:!0}),r=[]):n(o)?(r.length&&(yield new ot(r,{ignoreChecks:!0}),r=[]),yield new ot([o],{ignoreChecks:!0})):r.push(o);r.length&&(yield new ot(r,{ignoreChecks:!0}))}const qs=(i,t)=>{const e=i.findIndex(n=>y(t,n.firstPoint)),s=i.slice(0,e);return i.slice(e).concat(s)},Vs=(i,t)=>{let e=i;const s=o=>y(o.firstPoint,t.firstPoint)&&y(o.lastPoint,t.lastPoint);let n=i.findIndex(s);if(n===-1){const o=i.map(h=>h.reverse());if(o.reverse(),n=o.findIndex(s),n===-1)throw console.error(o.map(h=>h.repr),t.repr),new Error("Failed to rotate to segment start");e=o}const r=e.slice(0,n);return e.slice(n).concat(r)};function Jn(i,t,e){return i.filter(s=>{const n=t.filter(o=>y(o.firstPoint,s)||y(o.lastPoint,s));if(n.length%2)throw new Error("Bug in the intersection algo on non crossing point");const r=n.map(o=>e.contains(o.midPoint));return!(r.every(o=>o)||!r.some(o=>o))})}function Kn(i,t,e){let s=[];const n=[],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}=Gt(f,g,e);s.push(...P),r[p].push(...P),o[w].push(...P),n.push(...m);const d=m.flatMap(M=>[M.firstPoint,M.lastPoint]);s.push(...d),r[p].push(...d),o[w].push(...d)})}),s=Pt(s,e),!s.length||s.length===1)return null;const h=([f,p])=>p.length?f.splitAt(p):[f];let l=mt([i.segments,r]).flatMap(h),a=mt([t.segments,o]).flatMap(h);if(s=Jn(s,l,t),!s.length&&!n.length)return null;if(n.length){const f=n[0];l=Vs(l,f),a=Vs(a,f)}else{const f=s[0];l=qs(l,f),a=qs(a,f)}let c=Array.from(je(l,s,n)),u=Array.from(je(a,s,n));return(!y(u[0].lastPoint,c[0].lastPoint)||n.length>0&&u[0].segmentsCount!==1)&&(u=u.map(f=>f.reverse()).reverse(),y(u[0].lastPoint,c[0].lastPoint)||(c=c.map(f=>f.reverse()).reverse())),mt([c,u]).map(([f,p])=>f.segmentsCount===1&&n.some(g=>f.segments[0].isSame(g))?[f,"same"]:[f,p])}function Os(i){let t=i[0];for(const e of i.slice(1))t=t.extend(e);if(!y(t.firstPoint,t.lastPoint))throw console.error(H(t.firstPoint),H(t.lastPoint)),new Error("Bug in the intersection algo on non closing strand");return new ct(t.segments)}function tr(i,t){const e=mt([t.slice(0,-1),t.slice(1)]).map(([n,r])=>Os(i.slice(n,r)));let s=i.slice(t[t.length-1]);return t[0]!==0&&(s=s.concat(i.slice(0,t[0]))),e.push(Os(s)),e}function er(i){if(!i.length)return[];const t=i.map(n=>n.firstPoint);let e=i.map(n=>n.lastPoint);e=e.slice(-1).concat(e.slice(0,-1));const s=mt([t,e]).flatMap(([n,r],o)=>y(n,r)?[]:o);try{return tr(i,s)}catch{return Bs(i.flatMap(n=>n.segments)).filter(n=>n.length>1).filter(n=>y(n[0].firstPoint,n.at(-1).lastPoint)).map(n=>new ct(n))}}const Ns=(i,t)=>{if(i.length===0)return[t];const e=i.at(-1);return y(e.lastPoint,t.firstPoint)?i.slice(0,-1).concat([e.extend(t)]):y(e.lastPoint,t.lastPoint)?i.slice(0,-1).concat([e.extend(t.reverse())]):i.concat([t])},sr=(i,t)=>i.length===0?[t]:y(i[0].firstPoint,t.lastPoint)?[t.extend(i[0])].concat(i.slice(1)):[t].concat(i);function qe(i,t,{firstInside:e,secondInside:s}){const n=Kn(i,t);if(!n){const l=i.segments[0].midPoint,a=t.contains(l),c=t.segments[0].midPoint,u=i.contains(c);return{identical:!1,firstCurveInSecond:a,secondCurveInFirst:u}}if(n.every(([,l])=>l==="same"))return{identical:!0};let r=null,o=null;const h=n.flatMap(([l,a])=>{let c=[],u=0;if(a==="same")return o===1?(o=1,l):o===2||o===0?(o=null,[]):o===null?(r?r=r.extend(l):r=l,[]):(console.error("weird situation"),[]);const f=l.segments[0].midPoint,p=t.contains(f);(e==="keep"&&p||e==="remove"&&!p)&&(u+=1,c=Ns(c,l));const g=a.segments[0].midPoint,w=i.contains(g);if(s==="keep"&&w||s==="remove"&&!w){const P=a;u+=1,u===2&&c.length?(c=Ns(c,P),r=null):c=[P]}return o===null&&u===1&&r&&(c=sr(c,r)),u===1&&(o=u,r=null),c.length?c:(r=null,[])});return er(h)}const ir=(i,t)=>{const e=qe(i,t,{firstInside:"remove",secondInside:"remove"});return Array.isArray(e)?e:e.identical?[i]:e.firstCurveInSecond?[t]:e.secondCurveInFirst?[i]:[i,t]},ce=(i,t)=>{const e=qe(i,t,{firstInside:"remove",secondInside:"keep"});return Array.isArray(e)?e:e.identical?[]:e.firstCurveInSecond?[]:e.secondCurveInFirst?[i,t]:[i]},Ve=(i,t)=>{const e=qe(i,t,{firstInside:"keep",secondInside:"keep"});return Array.isArray(e)?e:e.identical?[i]:e.firstCurveInSecond?[i]:e.secondCurveInFirst?[t]:[]};function nr(i){const t=new Map,e=[];return i.forEach((s,n)=>{let r;t.has(n)?r=t.get(n):(r={current:[s],fusedWith:new Set([n])},e.push(r)),i.slice(n+1).forEach((o,h)=>{const l=r.current,a=n+h+1;if(r.fusedWith.has(a))return;let c=[o],u=!1;if(t.has(a)&&(c=t.get(a).current,u=!0),!l.some(p=>c.some(g=>p.intersects(g))))return;let f;l.length>1||c.length>1?f=Zt(l,c):f=zs(l[0],c[0]),r.fusedWith.add(a),r.current=f,u||t.set(a,r)})}),e.flatMap(({current:s})=>s)}function zs(i,t){const e=ir(i.contour,t.contour),s=t.holes.flatMap(o=>ce(o,i.contour)),n=i.holes.flatMap(o=>ce(o,t.contour)),r=Qn(i.holes,t.holes).flatMap(([o,h])=>Ve(o,h));return Wt([...e,...s,...n,...r])}function fe(i,t){if(i.isFull&&t.isFull)return Wt(ce(i.contour,t.contour));if(i.isFull){const s=ce(i.contour,t.contour),n=t.holes.flatMap(r=>Ve(r,i.contour));return Wt([...s,...n])}else if(t.isFull&&!i.contour.intersects(t.contour))if(i.contour.contains(t.contour.firstPoint)){const s=Zt(i.holes.map(n=>new ut(n)),[t]);return Wt([i.contour,...s.flatMap(n=>n.allLoops)])}else return[i];let e=fe(new ut(i.contour),t);return i.holes.forEach(s=>{e=e.flatMap(n=>fe(n,new ut(s)))}),e}function rr(i,t){const e=Ve(i.contour,t.contour);if(!e.length)return[];let s=Wt(e);return s=qt(s,i.holes.map(n=>new ut(n))),qt(s,t.holes.map(n=>new ut(n)))}function Zt(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 nr([...i,...t]);if(i.length>1&&t.length>1){let e=Zt([i[0]],t);return i.slice(1).forEach(s=>{e=Zt([s],e)}),e}return i.length===1&&t.length===1?zs(i[0],t[0]):[]}function qt(i,t){if(!i.length)return[];if(!t.length)return i;if(i.length===1&&t.length===1)return fe(i[0],t[0]);if(i.length>1)return i.flatMap(s=>qt([s],t));let e=fe(i[0],t[0]);return t.slice(1).forEach(s=>{e=qt(e,[s])}),e}function Oe(i,t){return!i.length||!t.length?[]:i.length===1&&t.length===1?rr(i[0],t[0]):i.length>1?i.flatMap(e=>Oe([e],t)):t.flatMap(e=>Oe(i,[e]))}class ft extends $t{constructor(t=[],{ignoreChecks:e=!1}={}){super(),st(this,"figures"),st(this,"_boundingBox",null),e||or(t),this.figures=t}get isEmpty(){return this.figures.length===0}get boundingBox(){if(this.isEmpty)return new Yt;if(this._boundingBox===null){let t=this.figures[0].boundingBox;for(const e of this.figures.slice(1))t=t.merge(e.boundingBox);this._boundingBox=t}return this._boundingBox}clone(){return new ft(this.figures.map(t=>t.clone()))}transform(t){return new ft(this.figures.map(e=>e.transform(t)))}contains(t){return this.figures.some(e=>e.contains(t))}intersects(t){return this.figures.some(e=>t.figures.some(s=>e.intersects(s)))}overlappingStrands(t){return this.figures.flatMap(e=>t instanceof ft?t.figures.flatMap(s=>e.overlappingStrands(s)):e.overlappingStrands(t))}fuse(t){return new ft(Zt(this.figures,t.figures))}cut(t){return new ft(qt(this.figures,t.figures))}intersect(t){return new ft(Oe(this.figures,t.figures))}}function or(i){for(const[t,e]of Re(i))if(t.intersects(e))throw new Error("Diagram figures must not intersect")}var hr=Object.defineProperty,ar=(i,t,e)=>t in i?hr(i,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):i[t]=e,me=(i,t,e)=>(ar(i,typeof t!="symbol"?t+"":t,e),e);class Ne{constructor(t,e){this.firstPoint=t,this.lastPoint=e}}function Us(i,t){if(i instanceof B)return lr(i,t);if(i instanceof L)return ur(i,t);throw new Error("Not implemented")}function lr(i,t){const{firstPoint:e,lastPoint:s}=i,n=i.normalVector;return new B(O(e,G(n,t)),O(s,G(n,t)))}function ur(i,t){const e=O(i.firstPoint,G(tt(i.tangentAtFirstPoint),t)),s=O(i.lastPoint,G(tt(i.tangentAtLastPoint),t)),n=t*(i.clockwise?1:-1);return i.radius+n<i.precision?new Ne(e,s):new L(e,s,i.center,i.clockwise)}function Ys(i,t,e){const s=bt(i.tangentAtLastPoint,t.tangentAtFirstPoint);if(Math.abs(s)<1e-10)return null;const n=s>0?1:-1,r=Math.abs(e)*n,o=Us(i,r),h=Us(t,r);if(o instanceof Ne||h instanceof Ne)return null;let l;try{l=_n(o,h,1e-9).at(-1)}catch{return null}if(!l)return null;const a=l,c=(p,g)=>{const w=g.tangentAt(a),P=Dt(w),m=O(a,G(P,r));return p.splitAt(m)},[u]=c(i,o),[,f]=c(t,h);return{first:u,second:f,center:a}}function Ds(i,t,e){const s=Ys(i,t,e);if(!s)return console.warn("Cannot fillet between segments",i.repr,t.repr),[i,t];const{first:n,second:r}=s;return[n,Ke(n.lastPoint,r.firstPoint,n.tangentAtLastPoint),r]}function Xs(i,t,e){const s=Ys(i,t,e);if(!s)return console.warn("Cannot chamfer between segments",i.repr,t.repr),[i,t];const{first:n,second:r}=s;return[n,new B(n.lastPoint,r.firstPoint),r]}const cr=i=>{let t;i?typeof i=="number"||Array.isArray(i)&&i.length===2?t={endTangent:i}:t={endTangent:0,...i}:t={endTangent:[1,0]};const{endTangent:e,startFactor:s=1,endFactor:n=1,startTangent:r}=t;let o;typeof e=="number"?o=Mt(1,e*St):o=e;let h;return typeof r=="number"?h=Mt(1,r*St):h=r,{endTangent:o,startFactor:s,endFactor:n,startTangent:h}};function Hs(i,{ignoreChecks:t=!1}={}){return new ft([new ut(new ct([...i],{ignoreChecks:t}))])}class fr{constructor(t=[0,0]){me(this,"pointer"),me(this,"firstPoint"),me(this,"pendingSegments"),me(this,"_nextCorner"),this.pointer=t,this.firstPoint=t,this.pendingSegments=[],this._nextCorner=null}movePointerTo(t){if(this.pendingSegments.length)throw new Error("You can only move the pointer if there is no segment defined");return this.pointer=t,this.firstPoint=t,this}saveSegment(t){if(y(t.firstPoint,t.lastPoint))throw new Error(`Segment has no length, ${t.repr}`);if(!this._nextCorner)return this.pendingSegments.push(t),this;const e=this.pendingSegments.pop();if(!e)throw new Error("bug in the custom corner algorithm");const s=this._nextCorner.mode==="chamfer"?Xs:Ds;return this.pendingSegments.push(...s(e,t,this._nextCorner.radius)),this._nextCorner=null,this}lineTo(t){const e=new B(this.pointer,t);return this.pointer=t,this.saveSegment(e)}line(t,e){return this.lineTo([this.pointer[0]+t,this.pointer[1]+e])}vLine(t){return this.line(0,t)}hLine(t){return this.line(t,0)}vLineTo(t){return this.lineTo([this.pointer[0],t])}hLineTo(t){return this.lineTo([t,this.pointer[1]])}polarLineTo([t,e]){const s=e*St,n=Mt(t,s);return this.lineTo(n)}polarLine(t,e){const s=e*St,[n,r]=Mt(t,s);return this.line(n,r)}tangentLine(t){const e=this.pendingSegments.at(-1);if(!e)throw new Error("You need a previous segment to sketch a tangent line");const[s,n]=e.tangentAtLastPoint;return this.line(s*t,n*t)}threePointsArcTo(t,e){return this.saveSegment(Pi(this.pointer,e,t)),this.pointer=t,this}threePointsArc(t,e,s,n){const[r,o]=this.pointer;return this.threePointsArcTo([r+t,o+e],[r+s,o+n])}sagittaArcTo(t,e){if(!e)return this.lineTo(t);const s=new B(this.pointer,t),n=tt(s.tangentAtFirstPoint),r=O(s.midPoint,G(n,e));return this.threePointsArcTo(t,r)}sagittaArc(t,e,s){return this.sagittaArcTo([t+this.pointer[0],e+this.pointer[1]],s)}vSagittaArc(t,e){return this.sagittaArc(0,t,e)}hSagittaArc(t,e){return this.sagittaArc(t,0,e)}bulgeArcTo(t,e){if(!e)return this.lineTo(t);const s=N(this.pointer,t)/2,n=-e*s;return this.sagittaArcTo(t,n)}bulgeArc(t,e,s){return this.bulgeArcTo([t+this.pointer[0],e+this.pointer[1]],s)}vBulgeArc(t,e){return this.bulgeArc(0,t,e)}hBulgeArc(t,e){return this.bulgeArc(t,0,e)}tangentArcTo(t,e){const s=this.pendingSegments.at(-1);if(!s)throw new Error("You need a previous curve to sketch a tangent arc");return this.saveSegment(Ke(this.pointer,t,e??s.tangentAtLastPoint)),this.pointer=t,this}tangentArc(t,e,s){const[n,r]=this.pointer;return this.tangentArcTo([t+n,e+r],s)}ellipseTo(t,e,s,n,r,o){return this.saveSegment(hn(this.pointer,t,e,s,n,r,o)),this.pointer=t,this}ellipse(t,e,s,n,r,o,h){return this.ellipseTo([t+this.pointer[0],e+this.pointer[1]],s,n,r,o,h)}halfEllipseTo(t,e){const[s,n]=Qe(q(t,this.pointer));return this.ellipseTo(t,s/2,Math.abs(e),n*ve,!0,e>0)}halfEllipse(t,e,s){return this.halfEllipseTo([t+this.pointer[0],e+this.pointer[1]],s)}cubicBezierCurveTo(t,e,s){return this.saveSegment(new et(this.pointer,t,e,s)),this.pointer=t,this}quadraticBezierCurveTo(t,e){return this.saveSegment(new K(this.pointer,t,e)),this.pointer=t,this}smoothCurveTo(t,e){const{endTangent:s,startTangent:n,startFactor:r,endFactor:o}=cr(e),h=this.pendingSegments.length?this.pendingSegments[this.pendingSegments.length-1]:null,l=N(this.pointer,t)/3;let a;n?a=n:h?a=h.tangentAtLastPoint:a=[1,0],a=W(a);const c=[this.pointer[0]+a[0]*r*l,this.pointer[1]+a[1]*r*l];let u=s;u=W(u);const f=[t[0]-u[0]*o*l,t[1]-u[1]*o*l];return this.cubicBezierCurveTo(t,c,f)}smoothCurve(t,e,s){return this.smoothCurveTo([t+this.pointer[0],e+this.pointer[1]],s)}customCorner(t,e="fillet"){if(!this.pendingSegments.length)throw new Error("You need a segment defined to fillet the angle");return t?(this._nextCorner={mode:e,radius:t},this):this}_customCornerLastWithFirst(t,e="fillet"){if(!t)return;const s=this.pendingSegments.pop(),n=this.pendingSegments.shift();if(!s||!n)throw new Error("Not enough curves to close and fillet");const r=e==="chamfer"?Xs:Ds;this.pendingSegments.push(...r(s,n,t))}close(t=!1){if(!this.pendingSegments.length)throw new Error("No segments to close");const e=this.pendingSegments[0],s=this.pendingSegments.at(-1);return y(e.firstPoint,s.lastPoint)||this.lineTo(e.firstPoint),this._nextCorner!==null&&(this._customCornerLastWithFirst(this._nextCorner.radius,this._nextCorner.mode),this._nextCorner=null),Hs(this.pendingSegments,{ignoreChecks:t})}closeWithMirror(t=!1){if(!this.pendingSegments.length)throw new Error("No segments to close");const e=this.pendingSegments[0],s=this.pendingSegments.at(-1),n=q(s.lastPoint,e.firstPoint),r=new at().mirrorLine(n,e.firstPoint),o=this.pendingSegments.map(h=>h.transform(r).reverse());return o.reverse(),Hs([...this.pendingSegments,...o],{ignoreChecks:t})}asStrand(){return new ot([...this.pendingSegments])}get isClosed(){var t;return y(this.pointer,(t=this.pendingSegments[0])==null?void 0:t.firstPoint)}}function Gs(i=[0,0]){return new fr(i)}function xt([i,t]){return`${i} ${t}`}function ze(i){if(i instanceof B)return`L ${xt(i.lastPoint)}`;if(i instanceof L)return`A ${i.radius} ${i.radius} 0 ${i.angularLength>Math.PI?"1":"0"} ${i.clockwise?"0":"1"} ${xt(i.lastPoint)}`;if(i instanceof J)return`A ${i.majorRadius} ${i.minorRadius} ${i.tiltAngle*ve} ${i.deltaAngle>Math.PI?"1":"0"} ${i.clockwise?"0":"1"} ${xt(i.lastPoint)}`;if(i instanceof K)return`Q ${[xt(i.controlPoint),xt(i.lastPoint)].join(" ")}`;if(i instanceof et)return`C ${[xt(i.firstControlPoint),xt(i.lastControlPoint),xt(i.lastPoint)].join(" ")}`;throw new Error("Unknown segment type")}function Ws(i){const t=`M ${i.firstPoint.join(" ")}`,e=i.segments.map(ze).join(" ");return`${t} ${e} Z`}function Zs(i){return`<path d="${i.allLoops.map(Ws).join(" ")}" />`}function mr(i){return`<g>
|
|
11
|
+
${i.figures.map(Zs).join(`
|
|
12
12
|
`)}
|
|
13
|
-
</g>`}function
|
|
14
|
-
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="${
|
|
15
|
-
${
|
|
16
|
-
</svg>`}function
|
|
17
|
-
`),h=
|
|
13
|
+
</g>`}function gr(i){const t=`M ${i.firstPoint.join(" ")}`,e=i.segments.map(ze).join(" ");return`${t} ${e}`}function pr(i,t=1){const e=i.xMin-t,s=i.yMin-t;return`${e} ${s} ${i.width+2*t} ${i.height+2*t}`}function Qs(i,t,e=1,s){const n=pr(t,e),r=s?`width="${t.width+2*e}${s}" height="${t.height+2*e}${s}"`:"";return`<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
|
14
|
+
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="${n}" fill="none" stroke="black" stroke-width="0.2%" vector-effect="non-scaling-stroke" ${r}>
|
|
15
|
+
${i}
|
|
16
|
+
</svg>`}function Js(i){if(i instanceof ut)return[i];if(i instanceof ct)return[new ut(i)];if(i instanceof ft)return i.figures;throw new Error("Unknown shape")}function Ks(i,t,e=1e-9){let s=[];const n=[],r=new Array(t.segments.length).fill(0).map(()=>[]);t.segments.forEach((h,l)=>{i.segments.forEach(a=>{const{intersections:c,overlaps:u}=Gt(h,a,e);s.push(...c),r[l].push(...c),n.push(...u);const f=u.flatMap(p=>[p.firstPoint,p.lastPoint]);s.push(...f),r[l].push(...f)})}),s=Pt(s,e);const o=mt([t.segments,r]).flatMap(([h,l])=>l.length?h.splitAt(l):[h]);return Array.from(je(o,s,n))}function ti(i,t,e=!1){return Ks(t,i).filter(s=>{const n=s.segments[0].midPoint;return t.onStroke(n)?!e:!t.contains(n)})}function wr(i,t,e=!1){return Ks(t,i).filter(s=>{const n=s.segments[0].midPoint;return t.onStroke(n)?!e:t.contains(n)})}function ei(i,t,e=!1){const s=ti(i,t.contour,e),n=t.holes.flatMap(r=>wr(i,r,e));return[...s,...n]}function dr(i,t){return new ft(qt(Js(i),Js(t)))}function Ue(i,t,e=!0){if(t instanceof ct)return ti(i,t,e);if(t instanceof ut)return ei(i,t,e);let s=[new ot([...i.segments])];return t.figures.forEach(n=>{s=s.flatMap(r=>ei(r,n,e))}),s}function si(i){if(i instanceof ft)return mr(i);if(i instanceof ut)return Zs(i);if(i instanceof ct)return`<path d="${Ws(i)}" />`;if(i instanceof ot)return`<path d="${gr(i)}" />`;if(Cs(i))return`<path d="${`M ${i.firstPoint.join(" ")}`} ${ze(i)}" />`;throw new Error("Unknown shape type")}const ii=i=>"shape"in i?i.shape:i,ni=(i,t)=>{if(!("shape"in i))return t;const{color:e}=i;return e?`<g stroke="${e}">${t}</g>`:t},ri=i=>new Yt(i.xMin,-i.yMax,i.xMax,-i.yMin);function Pr(i,{margin:t=1,unit:e=null,viewBox:s}={}){if(Array.isArray(i)){const r=i.map(l=>ii(l).mirror()),o=r.map((l,a)=>ni(i[a],si(l))).join(`
|
|
17
|
+
`),h=r.slice(1).reduce((l,a)=>l.merge(a.boundingBox),r[0].boundingBox);return Qs(o,s?ri(s):h,t,e)}const n=ii(i).mirror();return Qs(ni(i,si(n)),s?ri(s):n.boundingBox,t,e)}class Vt extends $t{constructor(e,{cutLines:s=[],foldLines:n=[],foldLinesForwards:r=[],foldLinesBackwards:o=[]}={}){super();Et(this,"body");Et(this,"cutLines");Et(this,"foldLinesBackwards");Et(this,"foldLinesForwards");this.body=e,this.cutLines=[...s],this.foldLinesBackwards=[...n,...o],this.foldLinesForwards=[...r]}get foldLines(){return[...this.foldLinesForwards,...this.foldLinesBackwards]}clone(){return new Vt(this.body,{cutLines:[...this.cutLines],foldLinesForwards:[...this.foldLinesForwards],foldLinesBackwards:[...this.foldLinesBackwards]})}addCutLine(e){return this.cutLines.push(e),this}addFoldLine(e,s="backwards"){s==="forwards"?this.foldLinesForwards.push(e):this.foldLinesBackwards.push(e)}fuseFold(e,s="backwards"){const n=e instanceof Vt?e.body:e,r=this.body.overlappingStrands(n);return s==="forwards"?this.foldLinesForwards.push(...r):this.foldLinesBackwards.push(...r),this.fuseBody(e),this}fuseBody(e){let s;return e instanceof Vt?(s=e.body,this.cutLines.push(...e.cutLines),this.foldLinesForwards.push(...e.foldLinesForwards),this.foldLinesBackwards.push(...e.foldLinesBackwards)):s=e,this.body=this.body.fuse(s),this}cutShape(e){return this.body=dr(this.body,e),this.cutLines=this.cutLines.flatMap(s=>Ue(s,e,!0)),this.eraseFolds(e),this}eraseFolds(e){this.foldLinesBackwards=this.foldLinesBackwards.flatMap(s=>Ue(s,e,!0)),this.foldLinesForwards=this.foldLinesForwards.flatMap(s=>Ue(s,e,!0))}transform(e){return new Vt(this.body.transform(e),{cutLines:this.cutLines.map(n=>n.transform(e)),foldLinesForwards:this.foldLinesForwards.map(n=>n.transform(e)),foldLinesBackwards:this.foldLinesBackwards.map(n=>n.transform(e))})}asSVG(){const e=[];return this.body&&e.push({shape:this.body,color:"red"}),this.cutLines.length&&this.cutLines.map(s=>e.push({shape:s,color:"red"})),this.foldLinesBackwards.length&&this.foldLinesBackwards.map(s=>e.push({shape:s,color:"green"})),this.foldLinesForwards.length&&this.foldLinesForwards.map(s=>e.push({shape:s,color:"blue"})),Pr(e,{unit:"mm"})}}function Ye(i,t,{fillet:e=1,contraction:s=1,contractionMode:n="rounded",contractionLeft:r,contractionRight:o,leftContractionMode:h,rightContractionMode:l}={}){const a=r??s,c=o??s,u=Gs([-i/2,0]).hLine(i);return c?(l??n)==="rounded"?(u.tangentArc(-c,c,[-1,0]),u.vLine(t-c)):u.line(-c,t):u.vLine(t),u.customCorner(e),u.hLine(-i+a+c),u.customCorner(e),a?(h??n)==="rounded"?(u.vLine(-t+a),u.tangentArcTo([-i/2,0])):u.lineTo([-i/2,0]):u.vLine(-t),u.close()}function oi(i,t,e={}){return Ye(i,t,e)}function hi(i,t,e={}){return Ye(i,t,e).mirror("x")}function De(i,t,{contractionTop:e,contractionBottom:s,topContractionMode:n,bottomContractionMode:r,...o}={}){return Ye(i,t,{contractionLeft:e,contractionRight:s,leftContractionMode:n,rightContractionMode:r,...o}).rotate(90)}function ai(i,t,e={}){return De(i,t,e).mirror("y")}const Mr={right:ai,left:De,top:oi,bottom:hi},li=(i,t)=>Gs([-i/2,0]).line(t,-t).hLine(i-2*t).line(t,t);class ge extends $t{constructor(e,s=.2){super();Et(this,"cut");Et(this,"unfold");Et(this,"bump");if(typeof e!="number"){this.cut=e.cut,this.unfold=e.unfold,this.bump=e.bump;return}const n=li(e,s*2.5);this.cut=n.asStrand(),this.unfold=n.close(),this.bump=li(e-5*s,5*s).close().mirror("x")}clone(){return new ge(this)}transform(e){return new ge({cut:this.cut.transform(e),unfold:this.unfold.transform(e),bump:this.bump.transform(e)})}makeCut(e){e.eraseFolds(this.unfold),e.addCutLine(this.cut)}fuseBump(e){e.fuseBody(this.bump)}}class pe extends $t{constructor(t){super(),this.points=t,this.points=t}transform(t){return new pe(this.points.map(t.transform))}distribute(t){return this.points.map(e=>t.translateTo(e))}cloneDistribute(t){return this.points.map(e=>t.clone().translateTo(e))}}function yr(i,t,e=3,s=0){if(i<=t*2)throw new Error("totalWidth must be greater than twice the objectWidth");const n=Math.max(Math.floor((i-2*s)/(t*(e+1))),2),o=(i-2*s-n*t)/(n-1),h=[];for(let l=0;l<n;l++)h.push(s+l*(t+o)-i/2+t/2);return new pe(h.map(l=>[l,0]))}function br(i,t,e="x"){const s=i*(t-1),n=[];for(let r=0;r<t;r++)n.push(r*i-s/2);return new pe(n.map(r=>e.toLowerCase()==="x"?[r,0]:[0,r]))}X.Dieline=Vt,X.FoldLockBump=ge,X.bottomFlap=hi,X.drawFlaps=Mr,X.leftFlap=De,X.linearDistribution=yr,X.linearSpread=br,X.rightFlap=ai,X.topFlap=oi,Object.defineProperty(X,Symbol.toStringTag,{value:"Module"})});
|
|
18
18
|
//# sourceMappingURL=dielines.umd.cjs.map
|