@excalidraw/math
Version:
Excalidraw math functions
2 lines (1 loc) • 12.2 kB
JavaScript
var y=1e-4,ft=(t,n,o)=>Math.min(Math.max(t,n),o),dt=(t,n,o="round")=>{let e=Math.pow(10,n);return Math[o]((t+Number.EPSILON)*e)/e},gt=(t,n,o="round")=>{let e=1/n;return Math[o](t*e)/e},ht=(t,n)=>(t+n)/2,Lt=t=>typeof t=="number"&&Number.isFinite(t),yt=(t,n,o=1e-4)=>Math.abs(t-n)<o;var C=t=>t<0?t%(2*Math.PI)+2*Math.PI:t%(2*Math.PI),St=([t,n])=>[Math.hypot(t,n),C(Math.atan2(n,t))];function K(t){return t*Math.PI/180}function vt(t){return t*180/Math.PI}function It(t){return Math.abs(Math.sin(2*t))<1e-4}function Vt(t,n,o){return t=C(t),n=C(n),o=C(o),n<o?t>=n&&t<=o:t>=n||t<=o}function Ct(t,n){t=C(t),n=C(n);let o=t-n;return o<-Math.PI?o=o+2*Math.PI:o>Math.PI&&(o=o-2*Math.PI),Math.abs(o)}function L(t,n,o=0,e=0){return[t-o,n-e]}function h(t,n=[0,0],o,e=[0,1]){let r=L(t[0]-n[0],t[1]-n[1]);return o&&U(r)<o*o?e:r}function N(t,n){return t[0]*n[1]-n[0]*t[1]}function E(t,n){return t[0]*n[0]+t[1]*n[1]}function Et(t){return Array.isArray(t)&&t.length===2&&typeof t[0]=="number"&&!isNaN(t[0])&&typeof t[1]=="number"&&!isNaN(t[1])}function F(t,n){return[t[0]+n[0],t[1]+n[1]]}function D(t,n){return[t[0]-n[0],t[1]-n[1]]}function M(t,n){return L(t[0]*n,t[1]*n)}function U(t){return t[0]*t[0]+t[1]*t[1]}function ct(t){return Math.sqrt(U(t))}var T=t=>{let n=ct(t);return n===0?L(0,0):L(t[0]/n,t[1]/n)},O=t=>L(t[1],-t[0]);function p(t,n){return[t,n]}function Tt(t){return t.length===2?p(t[0],t[1]):void 0}function Ot(t){return t}function I(t,n=p(0,0)){return p(n[0]+t[0],n[1]+t[1])}function S(t){return Array.isArray(t)&&t.length===2&&typeof t[0]=="number"&&!isNaN(t[0])&&typeof t[1]=="number"&&!isNaN(t[1])}function A(t,n,o=1e-4){let e=Math.abs;return e(t[0]-n[0])<o&&e(t[1]-n[1])<o}function w([t,n],[o,e],r){return p((t-o)*Math.cos(r)-(n-e)*Math.sin(r)+o,(t-o)*Math.sin(r)+(n-e)*Math.cos(r)+e)}function zt(t,n,o){return w(t,n,K(o))}function lt(t,n=[0,0]){return p(t[0]+n[0],t[1]+n[1])}function z(t,n){return p((t[0]+n[0])/2,(t[1]+n[1])/2)}function _(t,n){return Math.hypot(n[0]-t[0],n[1]-t[1])}function Xt(t,n){let o=n[0]-t[0],e=n[1]-t[1];return o*o+e*e}var Yt=(t,n,o)=>lt(n,M(h(t,n),o)),jt=(t,n,o)=>n[0]<=Math.max(t[0],o[0])&&n[0]>=Math.min(t[0],o[0])&&n[1]<=Math.max(t[1],o[1])&&n[1]>=Math.min(t[1],o[1]);var X=[-.06405689286260563,.06405689286260563,-.1911188674736163,.1911188674736163,-.3150426796961634,.3150426796961634,-.4337935076260451,.4337935076260451,-.5454214713888396,.5454214713888396,-.6480936519369755,.6480936519369755,-.7401241915785544,.7401241915785544,-.820001985973903,.820001985973903,-.8864155270044011,.8864155270044011,-.9382745520027328,.9382745520027328,-.9747285559713095,.9747285559713095,-.9951872199970213,.9951872199970213],Y=[.12793819534675216,.12793819534675216,.1258374563468283,.1258374563468283,.12167047292780339,.12167047292780339,.1155056680537256,.1155056680537256,.10744427011596563,.10744427011596563,.09761865210411388,.09761865210411388,.08619016153195327,.08619016153195327,.0733464814110803,.0733464814110803,.05929858491543678,.05929858491543678,.04427743881741981,.04427743881741981,.028531388628933663,.028531388628933663,.0123412297999872,.0123412297999872];function tt(t,n,o,e){return[t,n,o,e]}function $(t,n,o,e=1e-6){return[(t(n+e,o)-t(n-e,o))/(2*e),(t(n,o+e)-t(n,o-e))/(2*e)]}function st(t,n,o,e=.001,r=10){let i=1/0,a=0;for(;i>=e;){if(a>=r)return null;let c=t(n,o),l=[$((f,d)=>t(f,d)[0],n,o),$((f,d)=>t(f,d)[1],n,o)],P=[[-c[0]],[-c[1]]],s=l[0][0]*l[1][1]-l[0][1]*l[1][0];if(s===0)return null;let u=[[l[1][1]/s,-l[0][1]/s],[-l[1][0]/s,l[0][0]/s]],m=[[u[0][0]*P[0][0]+u[0][1]*P[1][0]],[u[1][0]*P[0][0]+u[1][1]*P[1][0]]];n=n+m[0][0],o=o+m[1][0];let[x,b]=t(n,o);i=Math.max(Math.abs(x),Math.abs(b)),a+=1}return[n,o]}var R=(t,n)=>p((1-n)**3*t[0][0]+3*(1-n)**2*n*t[1][0]+3*(1-n)*n**2*t[2][0]+n**3*t[3][0],(1-n)**3*t[0][1]+3*(1-n)**2*n*t[1][1]+3*(1-n)*n**2*t[2][1]+n**3*t[3][1]);function Kt(t,n){let o=a=>p(n[0][0]+a*(n[1][0]-n[0][0]),n[0][1]+a*(n[1][1]-n[0][1])),e=[[.5,0],[.2,0],[.8,0]],r=([a,c])=>{let l=st((u,m)=>{let x=R(t,u),b=o(m);return[x[0]-b[0],x[1]-b[1]]},a,c);if(!l)return null;let[P,s]=l;return P<0||P>1||s<0||s>1?null:R(t,P)},i=r(e[0]);return i?[i]:(i=r(e[1]),i?[i]:(i=r(e[2]),i?[i]:[]))}function Pt(t,n,o=.001){let e=(P,s,u,m=o)=>{let x=P,b=s,f;for(;b-x>m;)f=(b+x)/2,u(f-m)<u(f+m)?b=f:x=f;return f},i=0;for(let P=1/0,s=0;s<30;s++){let u=_(n,R(t,s/30));u<P&&(P=u,i=s)}let a=Math.max((i-1)/30,0),c=Math.min((i+1)/30,1),l=e(a,c,P=>_(n,R(t,P)));return l?R(t,l):null}function Ut(t,n){let o=Pt(t,n);return o?_(n,o):0}function $t(t){return Array.isArray(t)&&t.length===4&&S(t[0])&&S(t[1])&&S(t[2])&&S(t[3])}function j([t,n,o,e],r){return L(-3*(1-r)*(1-r)*t[0]+3*(1-r)*(1-r)*n[0]-6*r*(1-r)*n[0]-3*r*r*o[0]+6*r*(1-r)*o[0]+3*r*r*e[0],-3*(1-r)*(1-r)*t[1]+3*(1-r)*(1-r)*n[1]-6*r*(1-r)*n[1]-3*r*r*o[1]+6*r*(1-r)*o[1]+3*r*r*e[1])}function tn(t,n=.5){if(t.length<2)return;let o=[];for(let e=0;e<t.length-1;e++){let r=t[e-1<0?0:e-1],i=t[e],a=t[e+1>=t.length?t.length-1:e+1],c=i[0]+(a[0]-r[0])*n/2,l=i[1]+(a[1]-r[1])*n/2;o.push([p(c,l),p(a[0],a[1])])}return o}function nn(t,n=.5){if(t.length<2)return;let o=[];for(let e=0;e<t.length-1;e++){let r=t[e-1<0?0:e-1],i=t[e],a=t[e+1>=t.length?t.length-1:e+1],c=t[e+2>=t.length?t.length-1:e+2],l=[(a[0]-r[0])*n,(a[1]-r[1])*n],P=[(c[0]-i[0])*n,(c[1]-i[1])*n],s=i[0]+l[0]/3,u=i[1]+l[1]/3,m=a[0]-P[0]/3,x=a[1]-P[1]/3;o.push(tt(p(i[0],i[1]),p(s,u),p(m,x),p(a[0],a[1])))}return o}function on([t,n,o,e],r,i=50){let a=[];for(let c=0;c<=i;c++){let l=c/i,P=tt(t,n,o,e),s=R(P,l),u=T(j(P,l)),m=O(u);a.push(I(M(m,r),s))}return a}function en(t,n,o,e,r=50){let i=[];for(let a=0;a<=r;a++){let c=a/r,l=1-c,P=p(l*l*t[0]+2*l*c*n[0]+c*c*o[0],l*l*t[1]+2*l*c*n[1]+c*c*o[1]),s=2*(1-c)*(n[0]-t[0])+2*c*(o[0]-n[0]),u=2*(1-c)*(n[1]-t[1])+2*c*(o[1]-n[1]),m=T(L(s,u)),x=O(m);i.push(I(M(x,e),P))}return i}function nt(t){let o=0;for(let e=0;e<24;e++){let r=.5*X[e]+.5,i=j(t,r),a=Math.sqrt(i[0]*i[0]+i[1]*i[1]);o+=Y[e]*a}return .5*o}function ut(t,n){if(n<=0)return 0;if(n>=1)return nt(t);let o=n/2,e=n/2,r=0;for(let i=0;i<24;i++){let a=o*X[i]+e,c=j(t,a),l=Math.sqrt(c[0]*c[0]+c[1]*c[1]);r+=Y[i]*l}return o*r}function rn(t,n){if(n<=0)return R(t,0);if(n>=1)return R(t,1);let o=nt(t),e=o*n,r=0,i=1,a=n,c=0,l=o*1e-4,P=20;for(let s=0;s<P&&(c=ut(t,a),!(Math.abs(c-e)<l));s++)c<e?r=a:i=a,a=(r+i)/2;return R(t,a)}function un(t,n,o){return{center:t,halfWidth:n,halfHeight:o}}var mn=(t,n)=>{let{center:o,halfWidth:e,halfHeight:r}=n,i=(t[0]-o[0])/e,a=(t[1]-o[1])/r;return i*i+a*a<=1},pn=(t,n,o=1e-4)=>mt(t,n)<=o,mt=(t,n)=>{let{halfWidth:o,halfHeight:e,center:r}=n,i=o,a=e,c=F(h(t),M(h(r),-1)),l=Math.abs(c[0]),P=Math.abs(c[1]),s=.707,u=.707;for(let b=0;b<3;b++){let f=i*s,d=a*u,G=(i*i-a*a)*s**3/i,V=(a*a-i*i)*u**3/a,it=f-G,at=d-V,W=l-G,H=P-V,Q=Math.hypot(at,it),J=Math.hypot(H,W);s=Math.min(1,Math.max(0,(W*Q/J+G)/i)),u=Math.min(1,Math.max(0,(H*Q/J+V)/a));let Z=Math.hypot(u,s);s/=Z,u/=Z}let[m,x]=[i*s*Math.sign(c[0]),a*u*Math.sign(c[1])];return _(I(c),p(m,x))};function xn(t,n){let o=t.halfWidth,e=t.halfHeight,r=h(n[1],n[0]),i=L(n[0][0]-t.center[0],n[0][1]-t.center[1]),a=L(r[0]/(o*o),r[1]/(e*e)),c=L(i[0]/(o*o),i[1]/(e*e)),l=E(r,a),P=E(r,c),s=E(i,c)-1,u=P*P-l*s,m=[];if(u>0){let x=(-P-Math.sqrt(u))/l,b=(-P+Math.sqrt(u))/l;0<=x&&x<=1&&m.push(p(n[0][0]+(n[1][0]-n[0][0])*x,n[0][1]+(n[1][1]-n[0][1])*x)),0<=b&&b<=1&&m.push(p(n[0][0]+(n[1][0]-n[0][0])*b,n[0][1]+(n[1][1]-n[0][1])*b))}else if(u===0){let x=-P/l;0<=x&&x<=1&&m.push(p(n[0][0]+(n[1][0]-n[0][0])*x,n[0][1]+(n[1][1]-n[0][1])*x))}return m}function bn({center:t,halfWidth:n,halfHeight:o},[e,r]){let[i,a]=t,c=e[0]-i,l=e[1]-a,P=r[0]-i,s=r[1]-a,u=Math.pow(P-c,2)/Math.pow(n,2)+Math.pow(s-l,2)/Math.pow(o,2),m=2*(c*(P-c)/Math.pow(n,2)+l*(s-l)/Math.pow(o,2)),x=Math.pow(c,2)/Math.pow(n,2)+Math.pow(l,2)/Math.pow(o,2)-1,b=(-m+Math.sqrt(Math.pow(m,2)-4*u*x))/(2*u),f=(-m-Math.sqrt(Math.pow(m,2)-4*u*x))/(2*u),d=[p(c+b*(P-c)+i,l+b*(s-l)+a),p(c+f*(P-c)+i,l+f*(s-l)+a)].filter(G=>!isNaN(G[0])&&!isNaN(G[1]));return d.length===2&&A(d[0],d[1])?[d[0]]:d}function k(t,n){return[t,n]}function ot(t,n){let o=t[1][1]-t[0][1],e=t[0][0]-t[1][0],r=n[1][1]-n[0][1],i=n[0][0]-n[1][0],a=o*i-r*e;if(a!==0){let c=o*t[0][0]+e*t[0][1],l=r*n[0][0]+i*n[0][1];return p((c*i-l*e)/a,(o*l-r*c)/a)}return null}function v(t,n){return[t,n]}var Sn=t=>Array.isArray(t)&&t.length===2&&S(t[0])&&S(t[0]),vn=(t,n,o)=>v(w(t[0],o||z(t[0],t[1]),n),w(t[1],o||z(t[0],t[1]),n)),In=(t,n)=>{let o=h(t[0]),e=h(t[1]),r=h(n[0]),i=h(n[1]),a=D(e,o),c=D(i,r),l=N(a,c);if(l===0)return null;let P=D(h(n[0]),h(t[0])),s=N(P,a)/l,u=N(P,c)/l;if(s===0)return null;let m=F(o,M(a,u));return u>=0&&u<1&&s>=0&&s<1?I(m):null},q=(t,n,o=1e-4)=>{let e=pt(t,n);return e===0?!0:e<o},pt=(t,n)=>{let[o,e]=t,[[r,i],[a,c]]=n,l=o-r,P=e-i,s=a-r,u=c-i,m=l*s+P*u,x=s*s+u*u,b=-1;x!==0&&(b=m/x);let f,d;b<0?(f=r,d=i):b>1?(f=a,d=c):(f=r+b*s,d=i+b*u);let G=o-f,V=e-d;return Math.sqrt(G*G+V*V)};function et(t,n,o){let e=ot(k(t[0],t[1]),k(n[0],n[1]));return!e||!q(e,n,o)||!q(e,t,o)?null:e}function Fn(...t){return rt(t)}function Dn(t){return rt(t)}var An=(t,n)=>{let o=t[0],e=t[1],r=!1;for(let i=0,a=n.length-1;i<n.length;a=i++){let c=n[i][0],l=n[i][1],P=n[a][0],s=n[a][1];(l>e&&s<=e||l<=e&&s>e)&&o<(P-c)*(e-l)/(s-l)+c&&(r=!r)}return r},wn=(t,n)=>{let[o,e]=t,r=0;for(let i=0;i<n.length;i++){let a=(i+1)%n.length,[c,l]=n[i],[P,s]=n[a];l<=e?s>e&&(P-c)*(e-l)-(o-c)*(s-l)>0&&r++:s<=e&&(P-c)*(e-l)-(o-c)*(s-l)<0&&r--}return r!==0},qn=(t,n,o=1e-4)=>{let e=!1;for(let r=0,i=n.length-1;r<i;r++)if(q(t,v(n[r],n[r+1]),o)){e=!0;break}return e};function rt(t){return xt(t)?t:[...t,t[0]]}function xt(t){return A(t[0],t[t.length-1])}import{toBrandedType as B}from"@excalidraw/common";function Xn(t,n){return B([t,n])}function Yn(t){return B(t)}var jn=([t,n],[o,e])=>t<=o?n>=o:t>=o?e>=t:!1,kn=([t,n],[o,e])=>{let r=Math.max(t,o),i=Math.min(n,e);return r<=i?B([r,i]):null},Bn=(t,[n,o])=>t>=n&&t<=o;function bt(t,n){return[t,n]}function Zn(t,n,o,e){return bt(p(t,n),p(o,e))}function Kn(t,n){return[v(t[0],p(t[1][0],t[0][1])),v(p(t[1][0],t[0][1]),t[1]),v(t[1],p(t[0][0],t[1][1])),v(p(t[0][0],t[1][1]),t[0])].map(o=>et(n,o)).filter(o=>!!o)}function Un(t,n){let[[o,e],[r,i]]=t,[[a,c],[l,P]]=n;return o<l&&r>a&&e<P&&i>c}function no([t,n,o],e){let r=(s,u,m)=>(s[0]-m[0])*(u[1]-m[1])-(u[0]-m[0])*(s[1]-m[1]),i=r(e,t,n),a=r(e,n,o),c=r(e,o,t),l=i<0||a<0||c<0,P=i>0||a>0||c>0;return!(l&&P)}export{y as PRECISION,ht as average,R as bezierEquation,St as cartesian2Polar,ft as clamp,tt as curve,nn as curveCatmullRomCubicApproxPoints,tn as curveCatmullRomQuadraticApproxPoints,Pt as curveClosestPoint,Kt as curveIntersectLineSegment,nt as curveLength,ut as curveLengthAtParameter,on as curveOffsetPoints,rn as curvePointAtLength,Ut as curvePointDistance,j as curveTangent,K as degreesToRadians,pt as distanceToLineSegment,un as ellipse,mt as ellipseDistanceFromPoint,mn as ellipseIncludesPoint,bn as ellipseLineIntersectionPoints,xn as ellipseSegmentInterceptPoints,pn as ellipseTouchesPoint,yt as isCloseTo,$t as isCurve,Lt as isFiniteNumber,Sn as isLineSegment,S as isPoint,jt as isPointWithinBounds,It as isRightAngleRads,Et as isVector,k as line,v as lineSegment,et as lineSegmentIntersectionPoints,vn as lineSegmentRotate,ot as linesIntersectAt,C as normalizeRadians,en as offsetPointsForQuadraticBezier,z as pointCenter,_ as pointDistance,Xt as pointDistanceSq,p as pointFrom,Tt as pointFromArray,Ot as pointFromPair,I as pointFromVector,q as pointOnLineSegment,qn as pointOnPolygon,zt as pointRotateDegs,w as pointRotateRads,Yt as pointScaleFromOrigin,lt as pointTranslate,A as pointsEqual,Fn as polygon,Dn as polygonFromPoints,An as polygonIncludesPoint,wn as polygonIncludesPointNonZero,Vt as radiansBetweenAngles,Ct as radiansDifference,vt as radiansToDegrees,Bn as rangeIncludesValue,Xn as rangeInclusive,Yn as rangeInclusiveFromPair,kn as rangeIntersection,jn as rangesOverlap,bt as rectangle,Zn as rectangleFromNumberSequence,Kn as rectangleIntersectLineSegment,Un as rectangleIntersectRectangle,dt as round,gt as roundToStep,In as segmentsIntersectAt,no as triangleIncludesPoint,L as vector,F as vectorAdd,N as vectorCross,E as vectorDot,h as vectorFromPoint,ct as vectorMagnitude,U as vectorMagnitudeSq,O as vectorNormal,T as vectorNormalize,M as vectorScale,D as vectorSubtract};