UNPKG

cubic-beziers-through-points

Version:

A function to fit fair (bending energy minimizing) cubic bezier curves through a set of given ordered points in the plane.

1 lines 23.6 kB
var n={d:(t,e)=>{for(var r in e)n.o(e,r)&&!n.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},o:(n,t)=>Object.prototype.hasOwnProperty.call(n,t)},t={};function e(n){return n[n.length-1]}function r(n){var t=new ArrayBuffer(8);return new DataView(t).setFloat64(0,n,!1),Array.from(new Uint8Array(t))}function o(n){return r(n).map(u).join("")}function u(n){let t=n.toString(2);for(;t.length<8;t="0"+t);return t}function i(n){let t=r(n),e=t[0],o=t[1],u=e>>7,i=((127&e)<<4)+((240&o)>>4),c=0===i?0:16,s=0===i?i-1022:i-1023,l=t.slice(1);return l[0]=(15&o)+c,{sign:u,exponent:s,significand:l}}function c(n){return i(n).significand}function s(n){return Math.log2(n&-n)}function l(n){if(0===n||!Number.isFinite(n))return NaN;let t=c(n),e=t.length;for(let n=e-1;n>=0;n--){if(0===t[n])continue;let r=s(t[n]);if(Number.isFinite(r))return 8*(e-n-1)+r}return NaN}function f(n){return n>=128?7:n>=64?6:n>=32?5:n>=16?4:n>=8?3:n>=4?2:n>=2?1:n>=1?0:NaN}function a(n){if(0===n||!Number.isFinite(n))return NaN;let t=c(n),e=t.length;for(let n=0;n<e;n++){let r=f(t[n]);if(Number.isFinite(r))return 8*(e-n-1)+r}return NaN}function h(n){return i(n).exponent}function g(n){if(0===n||!Number.isFinite(n))return NaN;let t=h(n);return a(n)-52+t}function d(n){const t=n.slice(),e=t.length;if(1===e)return t;let r=t[e-1],o=e;for(let n=e-2;n>=0;--n){const e=r,u=t[n];r=e+u;const i=u-(r-e);i&&(t[--o]=r,r=i)}let u=0;for(let n=o;n<e;++n){const e=t[n],o=r;r=e+o;const i=o-(r-e);i&&(t[u++]=i)}return t[u++]=r,t.length=u,t}function p(n,t){const e=(2**(53-t)+1)*n;return e-(e-n)}n.d(t,{j:()=>be});const b=e,m=d;function N(n,t){if(n=m(n),0===b(n))return[0];let e=g(n[n.length-1]),r=e,o=n.length-1;for(;o>0;){let u=g(n[o-1]);if(e-u>t)break;r=u,o--}let u=Math.min(t-(e-r),53),i=n[o];i=p(i,u);let c=n.slice(o);return c[0]=i,c}function T(n){let t=n[0];for(let e=1;e<n.length;e++)t+=n[e];return t}function O(n,t){const e=n.length,r=t.length;let o=0,u=0;const i=[];for(;o<e&&u<r;)0!==n[o]?0!==t[u]?Math.abs(n[o])<=Math.abs(t[u])?(i.push(n[o]),o++):(i.push(t[u]),u++):u++:o++;for(;o<e;)i.push(n[o]),o++;for(;u<r;)i.push(t[u]),u++;if(0===i.length)return[0];const c=i.length;if(1===c)return i;const s=[],l=i[1],f=i[0];let a=l+f;const h=f-(a-l);0!==h&&s.push(h),u=0;for(let n=2;n<c;n++){const t=i[n],e=a+t,r=e-a,o=a-(e-r)+(t-r);0!==o&&s.push(o),a=e}return 0===a&&0!==s.length||s.push(a),s}const v=134217729;function w(n,t){const e=n.length;let r;const o=n[0];let u=o*t;const i=v*o,c=i-(i-o),s=o-c,l=v*t,f=l-(l-t),a=t-f,h=[],g=s*a-(u-c*f-s*f-c*a);0!==g&&h.push(g);for(let o=1;o<e;o++){const e=n[o],i=e*t,c=v*e,s=c-(c-e),l=e-s,f=v*t,a=f-(f-t),g=t-a,d=l*g-(i-s*a-l*a-s*g),p=u+d,b=p-u,m=u-(p-b)+(d-b);0!==m&&h.push(m),r=p;const N=i+r,T=r-(N-i);0!==T&&h.push(T),u=N}return 0===u&&0!==h.length||h.push(u),h}function M(n,t){const e=t.length;let r;const o=t[0];let u=o*n;const i=v*o,c=i-(i-o),s=o-c,l=v*n,f=l-(l-n),a=n-f,h=[],g=s*a-(u-c*f-s*f-c*a);0!==g&&h.push(g);for(let o=1;o<e;o++){const e=t[o],i=e*n,c=v*e,s=c-(c-e),l=e-s,f=v*n,a=f-(f-n),g=n-a,d=l*g-(i-s*a-l*a-s*g),p=u+d,b=p-u,m=u-(p-b)+(d-b);0!==m&&h.push(m),r=p;const N=i+r,T=r-(N-i);0!==T&&h.push(T),u=N}return 0===u&&0!==h.length||h.push(u),h}const y=w,I=O;function x(n,t){let e=[0];for(let r=0;r<n.length;r++)e=I(e,y(t,n[r]));return e}function E(n){const t=n.length,e=new Array(t);for(let r=0;r<t;r++)e[r]=-n[r];return e}const S=E,P=O;function B(n,t){const e=S(t);return P(n,e)}function D(n){let t=d(n);if(0===e(t))return 0;let r=t[t.length-1],o=t[0];return h(r)-h(o)+(53-l(o))}const L=x,A=N,F=D,_=B,j=T;function z(n,t){const e=n.length;let r=t;const o=[];for(let t=0;t<e;t++){const e=n[t],u=r+e,i=u-r;let c=r-(u-i)+(e-i);0!==c&&o.push(c),r=u}return 0===r&&0!==o.length||o.push(r),o}function $(n,t){const e=n+t,r=e-n;return[n-(e-r)+(t-r),e]}const k=$,C=z,V=O;function q(n){let t=[0];for(let e=0;e<n.length;e++){const r=n[e];t=1===r.length?1===t.length?k(t[0],r[0]):C(t,r[0]):1===t.length?C(r,t[0]):V(t,r)}return t}const Y=E,H=O,R=d,U=z,G=q,J=w,K=B,Q=Math.sign;function W(n,t){n=R(n);let e=(t=R(t))[t.length-1];if(1===t.length){if(0===e)throw new Error("division by zero");if(1===e)return{div:n,rem:[0]};if(-1===e)return{div:Y(n),rem:[0]}}const r=Q(n[n.length-1]);if(0===r)return{div:[0],rem:[0]};let o=Q(e),u=[],i=0;for(;;){let r=[];for(let o=n.length-1;o>=0;o--){const i=n[o];let c=Math.round((i-i%e)/e);if(r.push(J(t,c)),0===c)break;u.push(c)}if(n=R(K(n,G(r))),i===u.length)break;i=u.length}let c=n,s=[0];for(let n=0;n<u.length;n++)s=U(s,u[n]);s=R(s);const l=Q(c[c.length-1]);return 0!==l&&l!==r&&(r>0?o>0?(s=U(s,-1),c=H(c,t)):(s=U(s,1),c=H(c,Y(t))):r<0&&(o>0?(s=U(s,1),c=H(c,Y(t))):(s=U(s,-1),c=H(c,t)))),{div:s,rem:c}}const X=W,Z=W,nn=E;function tn(n,t){const e=n-t,r=n-e;return[n-(e+r)+(r-t),e]}const en=134217729;function rn(n,t){const e=n*t,r=en*n,o=r-(r-n),u=n-o,i=en*t,c=i-(i-t),s=t-c;return[u*s-(e-o*c-u*c-o*s),e]}const on=x,un=rn,cn=w,sn=$,ln=z,fn=O,an=x,hn=rn,gn=w,dn=d;function pn(n){let t=n[0];for(let e=1;e<n.length;e++){const r=n[e];t=1==r.length?1===t.length?hn(t[0],r[0]):gn(t,r[0]):1===t.length?gn(r,t[0]):an(r,t)}return dn(t)}const bn=x,mn=pn,Nn=d;function Tn(n,t){return!function(n,t){if(0===n||0===t)return!0;Math.abs(t)>Math.abs(n)&&([n,t]=[t,n]);let e=l(n),r=a(t);return e+(h(n)-h(t))>r}(n,t)}const On=O,vn={fastTwoDiff:function(n,t){const e=n-t;return[n-e-t,e]},fastTwoSum:function(n,t){const e=n+t;return[t-(e-n),e]},split:function(n){const t=134217729*n,e=t-(t-n);return[e,n-e]},twoDiff:tn,twoProduct:rn,twoSum:$,reduceSignificand:p,fastExpansionSum:O,eAdd:On,growExpansion:z,eAddDouble:z,expansionProduct:x,eMult:x,scaleExpansion:w,eMultDouble1:w,scaleExpansion2:M,eMultDouble2:M,eDiv:function(n,t,e){let r=t,o=n,u=!1,i=0;e||(i=F(o)-F(r)+1,e=i/53+1,u=!0);let c=[1/j(r)],s=1;for(;;){if(o=L(o,c),s>e)return u?(o=A(o,i),o):o.slice(o.length-e,o.length);r=L(r,c),c=_([2],r),s*=2}},eLongDivide:W,eIntDiv:function(n,t){return X(n,t).div},eRem:function(n,t){return Z(n,t).rem},eCompress:d,eEstimate:T,eDiff:B,eNegativeOf:E,eMultBy2:function(n){const t=[];for(let e=0;e<n.length;e++)t.push(2*n[e]);return t},eMultByNeg2:function(n){const t=[];for(let e=0;e<n.length;e++)t.push(-2*n[e]);return t},eDivBy2:function(n){const t=[];for(let e=0;e<n.length;e++)t.push(.5*n[e]);return t},eSign:e,eCompare:function(n,t){return e(B(n,t))},eAbs:function(n){return n[n.length-1]<0?nn(n):n},eToBitlength:N,eIntPow:function(n,t){if(0===t)return[1];if(1===t)return n;if(2===t)return bn(n,n);const e=[];for(let r=0;r<t;r++)e.push(n);return mn(e)},eCalculate:function(n){let t=[0];for(let e=0;e<n.length;e++){const r=n[e];let o=r[0];for(let n=1;n<r.length;n++){const t=r[n];o=1==t.length?1===o.length?un(o[0],t[0]):cn(o,t[0]):1===o.length?cn(t,o[0]):on(t,o)}t=1===o.length?1===t.length?sn(t[0],o[0]):ln(t,o[0]):1===t.length?ln(o,t[0]):fn(t,o)}return t},eSum:q,eProduct:pn,eToDd:function(n){const t=(n=Nn(n)).length;return 2===t?n:1===t?[0,n[0]]:[n[t-2],n[t-1]]},parseDouble:i,parseDoubleDetailed:function(n){let t=o(n),[,e,r,u]=t.match(/^(.)(.{11})(.{52})$/),i=0===parseInt(r,2)?"0":"1";return{full:e+r+i+u,sign:e,exponent:r,hidden:i,significand:u}},isBitAligned:function(n,t,e){if(0===n)return!0;let r=h(n),o=a(n)-52+r;return l(n)-52+r>=e&&o<=t-1+e},msbExponent:g,lsbExponent:function(n){if(0===n||!Number.isFinite(n))return NaN;let t=h(n);return l(n)-52+t},bitLength:function(n){return 0===n?0:a(n)-l(n)+1},expBitLength:D,doubleToBinaryString:o,doubleToOctets:r,getHighestSetBit:a,getLowestSetBit:l,exponent:h,significand:c,orient2d:function(n,t,e){let r,o=(n[0]-e[0])*(t[1]-e[1]),u=(n[1]-e[1])*(t[0]-e[0]),i=o-u;if(o>0){if(u<=0)return i;r=o+u}else{if(!(o<0))return i;if(u>=0)return i;r=-o-u}return Math.abs(i)>=3330669073875472e-31*r?i:function(n,t,e,r){let o=n[0]-e[0],u=t[0]-e[0],i=n[1]-e[1],c=t[1]-e[1],s=B(rn(o,c),rn(i,u)),l=T(s);if(Math.abs(l)>=2220446049250315e-31*r)return l;let f=tn(n[0],e[0])[0],a=tn(t[0],e[0])[0],h=tn(n[1],e[1])[0],g=tn(t[1],e[1])[0];if(0===f&&0===h&&0===a&&0===g)return l;let p=1109335647967049e-46*r+3330669073875471e-31*Math.abs(l);if(l+=o*g+c*f-(i*a+u*h),Math.abs(l)>=p)return l;let b=O(s,B(rn(f,c),rn(h,u))),m=O(b,B(rn(o,g),rn(i,a))),N=O(m,B(rn(f,g),rn(h,a)));return N=d(N),N[N.length-1]}(n,t,e,r)},isAdjacent:function(n,t){return Tn(n,t)||Tn(n,2*t)||Tn(2*n,t)},isNonOverlappingAll:function(n){for(let t=1;t<n.length;t++)if(Tn(n[t-1],n[t]))return!1;return!0},eIsInteger:function(n){n=d(n);for(let t=0;t<n.length;t++)if(n[t]%1!=0)return!1;return!0}};function wn(n,t){function e(t){return[n[0]+t[0],n[1]+t[1]]}return void 0===t?e:e(t)}function Mn(n,t,e){function r(e){return[e[0]*t-e[1]*n,e[0]*n+e[1]*t]}return void 0===e?r:r(e)}function yn(n){return[-n[0],-n[1]]}function In(n){return Math.sqrt(n[0]*n[0]+n[1]*n[1])}function xn(n,t){return[t[0]-n[0],t[1]-n[1]]}const{cos:En,sin:Sn,atan2:Pn}=Math;function Bn(n){const t=n[0];n=n.map(wn(yn(t)));const[e,r]=[n[3][0],n[3][1]],o=Pn(r,e),u=(n=n.map(Mn(Sn(-o),En(-o))))[3][0];n=n.map((n=>function(n,t){return[t*n[0],t*n[1]]}(n,1/u)));const i=n[1],c=[1-n[2][0],-n[2][1]];return{α:Pn(i[1],i[0]),β:Pn(c[1],c[0]),s0:3*In(i),s1:3*In(c),L:u,rot:o,p:t}}function Dn(n,t,e=16){if(t[0]===t[1])return 0;let{weights:r,abscissas:o}=Ln[e],[u,i]=t,c=0,s=(i-u)/2,l=(i+u)/2;for(let t=0;t<=e-1;t++)c+=r[t]*n(s*o[t]+l);return s*c}const Ln={2:{weights:[1,1],abscissas:[-.5773502691896257,.5773502691896257]},4:{weights:[.6521451548625461,.6521451548625461,.3478548451374538,.3478548451374538],abscissas:[-.3399810435848563,.3399810435848563,-.8611363115940526,.8611363115940526]},8:{weights:[.362683783378362,.362683783378362,.3137066458778873,.3137066458778873,.2223810344533745,.2223810344533745,.1012285362903763,.1012285362903763],abscissas:[-.1834346424956498,.1834346424956498,-.525532409916329,.525532409916329,-.7966664774136267,.7966664774136267,-.9602898564975363,.9602898564975363]},16:{weights:[.027152459411754096,.062253523938647894,.09515851168249279,.12462897125553388,.14959598881657674,.16915651939500254,.18260341504492358,.1894506104550685,.1894506104550685,.18260341504492358,.16915651939500254,.14959598881657674,.12462897125553388,.09515851168249279,.062253523938647894,.027152459411754096],abscissas:[-.9894009349916499,-.9445750230732326,-.8656312023878318,-.755404408355003,-.6178762444026438,-.45801677765722737,-.2816035507792589,-.09501250983763744,.09501250983763744,.2816035507792589,.45801677765722737,.6178762444026438,.755404408355003,.8656312023878318,.9445750230732326,.9894009349916499]},64:{weights:[.048690957009139724,.048690957009139724,.04857546744150343,.04857546744150343,.048344762234802954,.048344762234802954,.04799938859645831,.04799938859645831,.04754016571483031,.04754016571483031,.04696818281621002,.04696818281621002,.046284796581314416,.046284796581314416,.04549162792741814,.04549162792741814,.044590558163756566,.044590558163756566,.04358372452932345,.04358372452932345,.04247351512365359,.04247351512365359,.04126256324262353,.04126256324262353,.03995374113272034,.03995374113272034,.038550153178615626,.038550153178615626,.03705512854024005,.03705512854024005,.035472213256882386,.035472213256882386,.033805161837141606,.033805161837141606,.03205792835485155,.03205792835485155,.030234657072402478,.030234657072402478,.028339672614259483,.028339672614259483,.02637746971505466,.02637746971505466,.024352702568710874,.024352702568710874,.022270173808383253,.022270173808383253,.02013482315353021,.02013482315353021,.017951715775697343,.017951715775697343,.015726030476024718,.015726030476024718,.013463047896718643,.013463047896718643,.011168139460131128,.011168139460131128,.008846759826363947,.008846759826363947,.006504457968978363,.006504457968978363,.004147033260562468,.004147033260562468,.001783280721696433,.001783280721696433],abscissas:[-.024350292663424433,.024350292663424433,-.07299312178779904,.07299312178779904,-.12146281929612056,.12146281929612056,-.16964442042399283,.16964442042399283,-.21742364374000708,.21742364374000708,-.2646871622087674,.2646871622087674,-.31132287199021097,.31132287199021097,-.3572201583376681,.3572201583376681,-.4022701579639916,.4022701579639916,-.4463660172534641,.4463660172534641,-.48940314570705296,.48940314570705296,-.5312794640198946,.5312794640198946,-.571895646202634,.571895646202634,-.6111553551723933,.6111553551723933,-.6489654712546573,.6489654712546573,-.6852363130542333,.6852363130542333,-.7198818501716109,.7198818501716109,-.7528199072605319,.7528199072605319,-.7839723589433414,.7839723589433414,-.8132653151227975,.8132653151227975,-.8406292962525803,.8406292962525803,-.8659993981540928,.8659993981540928,-.8893154459951141,.8893154459951141,-.9105221370785028,.9105221370785028,-.9295691721319396,.9295691721319396,-.9464113748584028,.9464113748584028,-.9610087996520538,.9610087996520538,-.973326827789911,.973326827789911,-.983336253884626,.983336253884626,-.9910133714767443,.9910133714767443,-.9963401167719553,.9963401167719553,-.9993050417357722,.9993050417357722]}};Number.EPSILON;const An=134217729;Number.EPSILON,Number.EPSILON;const Fn=function(n,t){const e=n[0],r=n[1],o=t[0],u=t[1],i=r+u,c=i-r,s=e+o,l=s-e,f=r-(i-c)+(u-c)+s,a=i+f,h=e-(s-l)+(o-l)+(f-(a-i)),g=a+h;return[h-(g-a),g]},_n=function(n,t){const e=n[0],r=n[1],o=t[0],u=t[1],i=r-u,c=i-r,s=e-o,l=s-e,f=r-(i-c)+(-u-c)+s,a=i+f,h=e-(s-l)+(-o-l)+(f-(a-i)),g=a+h;return[h-(g-a),g]},jn=Number.EPSILON/2;function zn(n){const t=n*jn;return t/(1-t)}function $n(n){const t=n[0][0],e=n[0][1];for(let r=1;r<n.length;r++)if(t!==n[r][0]||e!==n[r][1])return!1;return!0}zn(1);const kn=B,Cn=e,Vn=$,{abs:qn}=Math,Yn=rn,Hn=O,Rn=e,Un=B,Gn=Number.EPSILON/2,Jn=Math.abs,{orient2d:Kn}=vn;function Qn(n){if(4===n.length)return 0===Kn(n[0],n[1],n[2])&&0===Kn(n[1],n[2],n[3])&&0===Kn(n[0],n[2],n[3]);if(3===n.length)return 0===Kn(n[0],n[1],n[2]);if(n.length<=2)return!0;throw new Error("The given bezier curve must be of order <= 3.")}const Wn=w,Xn=B,Zn=$,nt=e,tt=Math.abs;const et=Math.abs,rt=zn(1),ot=tn,ut=$,it=M,ct=z,st=On;const lt=x,ft=O,at=B,{eSign:ht,eCompare:gt}=vn,dt=B,pt=x,bt=M,mt=function(n,t){const e=n-t,r=n-e;return[n-(e+r)+(r-t),e]},Nt=function(n,t){const e=n+t,r=e-n;return[n-(e-r)+(t-r),e]},Tt=O,Ot=z,vt=Math.abs,wt=zn(1),Mt={order:0,realOrder:0,collinear:!0,nodeType:"n/a"},yt={order:1,realOrder:1,collinear:!0,nodeType:"n/a"},It={order:1,realOrder:0,collinear:!0,nodeType:"n/a"},xt={order:2,realOrder:2,collinear:!1,nodeType:"n/a"},Et={order:2,realOrder:2,collinear:!0,nodeType:"n/a"},St={order:2,realOrder:1,collinear:!0,nodeType:"n/a"},Pt={order:2,realOrder:0,collinear:!0,nodeType:"n/a"},Bt={order:3,realOrder:3,collinear:!1,nodeType:"crunode"},Dt={order:3,realOrder:3,collinear:!1,nodeType:"acnode"},Lt={order:3,realOrder:3,collinear:!1,nodeType:"cusp"},At={order:3,realOrder:3,collinear:!1,nodeType:"explicit"},Ft={order:3,realOrder:3,collinear:!0,nodeType:"n/a"},_t={order:3,realOrder:2,collinear:!1,nodeType:"n/a"},jt={order:3,realOrder:2,collinear:!0,nodeType:"n/a"},zt={order:3,realOrder:1,collinear:!0,nodeType:"n/a"},$t={order:3,realOrder:0,collinear:!0,nodeType:"n/a"};function kt(n){if(4===n.length)return function(n){const[[t,e],[r,o],[u,i],[c,s]]=n,l=c+3*r,f=t+3*u,a=l-f,h=Jn(3*r)+Jn(l)+(Jn(3*u)+Jn(f))+Jn(a);if(Jn(a)-Gn*h>0)return!1;const g=s+3*o,d=e+3*i,p=g-d,b=Jn(3*o)+Jn(g)+(Jn(3*i)+Jn(d))+Jn(p);return!(Jn(p)-Gn*b>0)&&0===Rn(Un(Hn([c],Yn(3,r)),Hn([t],Yn(3,u))))&&0===Rn(Un(Hn([s],Yn(3,o)),Hn([e],Yn(3,i))))}(n)?function(n){if(!Qn(n))return!1;const[t,e,r,o]=n,[u,i]=t,[c,s]=e,[l,f]=r,[a,h]=o,g=Xn(Wn(Zn(c/4,l/4),3),Zn(u/4,a/4)),d=Xn(Wn(Zn(s/4,f/4),3),Zn(i/4,h/4));return 0===nt(Xn(Zn(u/2,a/2),g))&&0===nt(Xn(Zn(i/2,h/2),d))}(n)?$n(n)?$t:zt:Qn(n)?jt:_t:Qn(n)?Ft:function(n){const{coeffs:[t,e,r],errBound:[o,u,i]}=function(n){const{coeffs:[[t,e,r],[o,u,i]],errorBound:[[c,s],[l,f]]}=function(n){const[[t,e],[r,o],[u,i],[c,s]]=n,l=c-t,f=r-u,a=l+3*f,h=u+t,g=h-2*r,d=r-t,p=s-e,b=o-i,m=p+3*b,N=i+e,T=N-2*o,O=o-e;return{coeffs:[[a,3*g,3*d,t],[m,3*T,3*O,e]],errorBound:[[tt(l)+6*tt(f)+tt(a),6*(tt(h)+tt(g)),6*tt(d),0],[tt(p)+6*tt(b)+tt(m),6*(tt(N)+tt(T)),6*tt(O),0]]}}(n),a=et(t),h=et(e),g=et(r),d=et(o),p=et(u),b=et(i),m=e*o,N=t*u,T=t*i,O=r*o,v=e*i,w=r*u,M=s*d+h*l+et(m),y=c*p+a*f+et(N),I=c*b+et(T),x=g*l+et(O),E=s*b+et(v),S=g*f+et(w),P=m-N,B=et(P),D=M+y+B,L=O-T,A=et(L),F=x+I+A,_=v-w,j=et(_),z=E+S+j,$=P*P,k=P*L,C=_*P,V=L*L,q=C+V;return{coeffs:[$,k,q],errBound:[2*D*B+et($),D*A+B*F+et(k),z*B+j*D+et(C)+(2*A*F+et(V))+et(q)].map((n=>rt*n))}}(n);if(vt(t)<=o){const[[t,e],[r,o],[u,i],[c,s]]=n,l=Tt(mt(c,t),bt(3,mt(r,u))),f=Ot(Nt(u,t),-2*r),a=Tt(mt(s,e),bt(3,mt(o,i))),h=Ot(Nt(i,e),-2*o),g=pt(f,a),d=pt(l,h);if(0===gt(g,d))return At}const c=e*e,s=4*t*r,l=c-s,f=2*u*vt(e)+wt*c+(4*(o*vt(r)+vt(t)*i)+wt*vt(s))+wt*vt(l);if(l<-f)return Dt;if(l>f)return Bt;let[a,h,g]=function(n){const[[t,e,r],[o,u,i]]=function(n){const[[t,e],[r,o],[u,i],[c,s]]=n;return[[st(ot(c,t),it(3,ot(r,u))),it(3,ct(ut(u,t),-2*r)),it(3,ot(r,t)),[t]],[st(ot(s,e),it(3,ot(o,i))),it(3,ct(ut(i,e),-2*o)),it(3,ot(o,e)),[e]]]}(n),c=lt(e,o),s=lt(t,u),l=lt(t,i),f=lt(r,o),a=lt(e,i),h=lt(r,u),g=at(c,s),d=at(f,l),p=at(a,h),b=lt(g,g),m=lt(g,d),N=lt(g,p),T=lt(d,d);return[b,m,ft(N,T)]}(n);const d=dt(pt(h,h),bt(4,pt(a,g))),p=ht(d);return p<0?Dt:p>0?Bt:Lt}(n);if(3===n.length)return Qn(n)?function(n){const[[t,e],[r,o],[u,i]]=n,c=t+u,s=c-2*r,l=qn(c)+qn(s);if(qn(s)-l>0)return!1;const f=e+i,a=f-2*o,h=qn(f)+qn(a);return!(qn(a)-h>0)&&0===Cn(kn(Vn(t,u),[2*r]))&&0===Cn(kn(Vn(e,i),[2*o]))}(n)?$n(n)?Pt:St:Et:xt;if(2===n.length)return $n(n)?It:yt;if(1===n.length)return Mt;throw new Error("The given bezier curve must be of order <= 3")}const Ct=function(n,t){const e=n*t,r=An*n,o=r-(r-n),u=n-o,i=An*t,c=i-(i-t),s=t-c;return[u*s-(e-o*c-u*c-o*s),e]},Vt=Fn,qt=_n,{atan2:Yt}=Math;function Ht(n,t){const e=n[0],r=n[1],o=t[0],u=t[1];let i=qt(Ct(u,e),Ct(o,r))[1],c=Vt(Ct(o,e),Ct(u,r))[1];return Yt(i,c)}const{abs:Rt}=Math;function Ut(n){const t=[];for(let e=0;e<n.length-1;e++){const r=[n[e+1][0]-n[e][0],n[e+1][1]-n[e][1]];0!==(0!==r[0]||r[1])&&t.push(r)}const e=t.length;let r=0;for(let n=0;n<e-1;n++)r+=Rt(Ht(t[n],t[n+1]));return r}function Gt(n,t,e){if(4===n.length)return function(n,t,e){return 0===t?1===e?n:function(n,t){const e=n[0],r=n[1],o=n[2],u=n[3],i=e[0],c=e[1],s=r[0],l=r[1],f=o[0],a=o[1],h=i-t*(i-s),g=s-t*(s-f),d=h-t*(h-g),p=c-t*(c-l),b=l-t*(l-a),m=p-t*(p-b);return[[i,c],[h,p],[d,m],[d-t*(d-(g-t*(g-(f-t*(f-u[0]))))),m-t*(m-(b-t*(b-(a-t*(a-u[1])))))]]}(n,e):1===e?function(n,t){const e=n[0],r=n[1],o=n[2],u=n[3],i=e[0],c=e[1],s=r[0],l=r[1],f=o[0],a=o[1],h=u[0],g=u[1],d=i-t*(i-s),p=s-t*(s-f),b=f-t*(f-h),m=d-t*(d-p),N=p-t*(p-b),T=c-t*(c-l),O=l-t*(l-a),v=a-t*(a-g),w=T-t*(T-O),M=O-t*(O-v);return[[m-t*(m-N),w-t*(w-M)],[N,M],[b,v],[h,g]]}(n,t):function(n,t,e){const r=n[0],o=n[1],u=n[2],i=n[3],c=r[0],s=r[1],l=o[0],f=o[1],a=u[0],h=u[1],g=t*t,d=t*g,p=e*e,b=e*p,m=t*e,N=c-l,T=a-l,O=N+T,v=t*N,w=e*N,M=i[0]-c-3*T,y=s-f,I=h-f,x=y+I,E=t*y,S=e*y,P=i[1]-s-3*I;return[[d*M+(3*t*(t*O-N)+c),d*P+(3*t*(t*x-y)+s)],[m*(t*M+2*O)+(g*O+c-(w+2*v)),m*(t*P+2*x)+(g*x+s-(S+2*E))],[m*(e*M+2*O)+(p*O+c-(2*w+v)),m*(e*P+2*x)+(p*x+s-(2*S+E))],[b*M+(3*e*(e*O-N)+c),b*P+(3*e*(e*x-y)+s)]]}(n,t,e)}(n,t,e);if(3===n.length)return function(n,t,e){return 0===t?1===e?n:function(n,t){const e=n[0],r=n[1],o=n[2],u=e[0],i=e[1],c=r[0],s=r[1],l=t*t,f=u-c,a=i-s;return[[u,i],[-t*f+u,-t*a+i],[l*(f+(o[0]-c))-(2*t*f-u),l*(a+(o[1]-s))-(2*t*a-i)]]}(n,e):1===e?function(n,t){const e=n[0],r=n[1],o=n[2],u=e[0],i=e[1],c=r[0],s=r[1],l=o[0],f=o[1],a=t*t,h=u-c,g=l-c,d=i-s,p=f-s;return[[a*(h+g)-(2*t*h-u),a*(d+p)-(2*t*d-i)],[t*g+c,t*p+s],[l,f]]}(n,t):function(n,t,e){const r=n[0],o=n[1],u=n[2],i=r[0],c=r[1],s=o[0],l=o[1],f=t*t,a=e*e,h=t*e,g=i-s,d=g+(u[0]-s),p=c-l,b=p+(u[1]-l);return[[f*d-(2*t*g-i),f*b-(2*t*p-c)],[h*d-(g*(e+t)-i),h*b-(p*(e+t)-c)],[a*d-(2*e*g-i),a*b-(2*e*p-c)]]}(n,t,e)}(n,t,e);if(2===n.length)return function(n,t,e){return 0===t?1===e?n:function(n,t){const e=n[0],r=n[1],o=e[0],u=e[1];return[[o,u],[t*(r[0]-o)+o,t*(r[1]-u)+u]]}(n,e):1===e?function(n,t){const e=n[0],r=n[1],o=e[0],u=e[1],i=r[0],c=r[1];return[[t*(i-o)+o,t*(c-u)+u],[i,c]]}(n,t):function(n,t,e){const r=n[0],o=n[1],u=r[0],i=r[1],c=o[0],s=o[1];return[[t*(c-u)+u,t*(s-i)+i],[e*(c-u)+u,e*(s-i)+i]]}(n,t,e)}(n,t,e);if(1===n.length)return n;throw new Error("The given bezier curve must be of order <= 3.")}function Jt(n){return function(n){for(let t=1;t<n.length;t++)if(n[t-1]>n[t])return!1;return!0}(n)||function(n){for(let t=1;t<n.length;t++)if(n[t-1]<n[t])return!1;return!0}(n)}const{sqrt:Kt}=Math;function Qt(n,t=1.125,e=4){const r=kt(n);return r.collinear?function(n){if(!Qn(n))return!1;const t=n.map((n=>n[0])),e=n.map((n=>n[1]));return!(Jt(t)&&Jt(e))}(n)?Number.POSITIVE_INFINITY:0:3===r.realOrder?"cusp"===r.nodeType?Number.POSITIVE_INFINITY:Wt(Xt,n,t,e):2===r.realOrder?Wt(Zt,n,t,e):0}function Wt(n,t,e,r){const o=function(n,t=.4,e=2**-16){let r={r:[0,1]},o=r;for(;void 0!==o;){const r=o.r;if(Ut(Gt(n,r[0],r[1]))<=t||r[1]-r[0]<=e){o=o.next;continue}const u=(r[0]+r[1])/2,i=[r[0],u],c=[u,r[1]];o.r=i,o.next={r:c,next:o.next}}o=r;const u=[];for(;void 0!==o;)u.push(o.r[0]),void 0===o.next&&u.push(o.r[1]),o=o.next;return u}(t,e);let u=0;for(let e=0;e<o.length-1;e++)u+=Dn(n(Gt(t,o[e],o[e+1])),[0,1],r);return u}function Xt(n){const t=n[0],e=n[1],r=n[2],o=n[3],u=t[0],i=t[1],c=e[0],s=e[1],l=r[0],f=r[1],a=o[0],h=o[1],g=a-u+3*(c-l),d=h-i+3*(s-f),p=l+u-2*c,b=f+i-2*s;return n=>{const t=n*n,e=t*g+2*n*p+(c-u),r=t*d+2*n*b+(s-i),o=e*(n*d+b)-r*(n*g+p),l=e*e+r*r;return 4/3*o*o/(l*l*Kt(l))}}function Zt(n){const t=n[0],e=n[1],r=n[2],o=t[0],u=t[1],i=e[0],c=e[1],s=r[0],l=r[1],f=s-2*i+o,a=l-2*c+u;return n=>{const t=f*n+(i-o),e=a*n+(c-u),r=t*a-e*f,s=t*t+e*e;return.5*r*r/(s*s*Kt(s))}}const{cos:ne,sin:te}=Math;function ee(n){const{α:t,β:e,s0:r,s1:o,L:u,rot:i,p:c}=n;return function(n){const t=n[0].length;if(4===t){const[[t,e,r,o],[u,i,c,s]]=n;return[[o,s],[o+r/3,s+c/3],[o+2*r/3+e/3,s+2*c/3+i/3],[o+r+e+t,s+c+i+u]]}if(3===t){const[[t,e,r],[o,u,i]]=n;return[[r,i],[r+e/2,i+u/2],[r+e+t,i+u+o]]}if(2===t){const[[t,e],[r,o]]=n;return[[e,o],[e+t,o+r]]}if(1===t){const[[t],[e]]=n;return[[t,e]]}throw new Error("The given bezier curve must be of order <= 3.")}([[u*(r*ne(t)-2+o*ne(e)),u*(3-2*r*ne(t)-o*ne(e)),u*(r*ne(t)),0*u],[u*(r*te(t)+o*te(e)),u*(-2*r*te(t)-o*te(e)),u*(r*te(t)),0*u]]).map(Mn(te(i),ne(i))).map(wn(c))}function re(n){return t=>{const[e,r]=oe(t,n);return Qt(r)+Qt(e)}}function oe(n,t){const{ϕ:e}=t,r=t._info,o=r.$ps,u=t.$ps,i=ee({...o,β:n-e,s0:1,s1:1}),c=ee({...u,α:n,s0:1,s1:1});{const n=r.ps,e=t.ps;return[[n[0],i[1],i[2],n[3]],[e[0],c[1],c[2],e[3]]]}}const{PI:ue}=Math,ie=ue,ce=.0625,[se,le]=[1,1],{min:fe,max:ae,PI:he,atan2:ge,sin:de,cos:pe}=Math;function be(n){const t=function(n){const t=n.length,e=[];for(let n=0;n<t;n++)e.push({});for(let u=0;u<t;u++){const i=(u-1+t)%t,c=(u+1)%t,s=e[i],l=e[u],f=e[c];l._info=s,l.info_=f;const a=n[i],h=n[u],g=n[c],d=(o=g,-Ht(xn(a,r=h),xn(r,o)));l.ϕ=d;const p=[ae(-ie,d-ie),fe(+ie,d+ie)];l.range=p;const b=wn(yn(h))(g),[m,N]=[b[0],b[1]],T=ge(N,m),O=Mn(de(-T),pe(-T))(b)[0];l.L=O,l.rot=T,l.p=h}var r,o;for(let n=0;n<t;n++){const t=e[n],{ϕ:r,info_:o,p:u,L:i,rot:c}=t,s=r/2,l={α:s,β:-o.ϕ/2,p:u,L:i,rot:c,s0:se,s1:le},f=ee(l);t.ps=[u,f[1],f[2],o.p],t.$ps=l,t.l=s-he*ce,t.m=s,t.r=s+he*ce}return e}(n),e=t.length;for(let n=0;n<4;n++)for(let n=0;n<e;n++){const e=t[n],r=e._info,o=re(e);let u,{l:i,m:c,r:s}=e,l=o(i),f=o(c),a=o(s);for(;;){if(({l:i,m:c,r:s}=e),u=(s-i)/2,l>=f&&f<=a){e.l=c-u/2,e.r=c+u/2;break}l<=f&&f<=a?(e.l=i-u,e.m=i,e.r=c,a=f,f=l,l=o(e.l)):l>=f&&f>=a?(e.l=c,e.m=s,e.r=s+u,l=f,f=a,a=o(e.r)):l<=f&&f>=a&&(e.l=i-u,e.m=i,e.r=c,a=f,f=l,l=o(e.l))}const h=e.m,[g,d]=oe(h,e);e.ps=d,e.$ps=Bn(d),r.ps=g,r.$ps=Bn(g)}return t.map((n=>n.ps))}var me=t.j;export{me as cubicBeziersThroughPoints};