2 lines
20 KiB
JavaScript
Raw Normal View History

2026-05-31 13:21:13 +02:00
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).venn={})}(this,(function(t){"use strict";const e=1e-10;function n(t,n){const o=function(t){const e=[];for(let n=0;n<t.length;++n)for(let s=n+1;s<t.length;++s){const r=i(t[n],t[s]);for(const t of r)t.parentIndex=[n,s],e.push(t)}return e}(t),l=o.filter((n=>function(t,n){return n.every((n=>r(t,n)<n.radius+e))}(n,t)));let c=0,u=0;const f=[];if(l.length>1){const e=a(l);for(let t=0;t<l.length;++t){const n=l[t];n.angle=Math.atan2(n.x-e.x,n.y-e.y)}l.sort(((t,e)=>e.angle-t.angle));let n=l[l.length-1];for(let e=0;e<l.length;++e){const o=l[e];u+=(n.x+o.x)*(o.y-n.y);const i={x:(o.x+n.x)/2,y:(o.y+n.y)/2};let a=null;for(let e=0;e<o.parentIndex.length;++e)if(n.parentIndex.includes(o.parentIndex[e])){const s=t[o.parentIndex[e]],l=Math.atan2(o.x-s.x,o.y-s.y),c=Math.atan2(n.x-s.x,n.y-s.y);let u=c-l;u<0&&(u+=2*Math.PI);const f=c-u/2;let h=r(i,{x:s.x+s.radius*Math.sin(f),y:s.y+s.radius*Math.cos(f)});h>2*s.radius&&(h=2*s.radius),(null==a||a.width>h)&&(a={circle:s,width:h,p1:o,p2:n,large:h>s.radius,sweep:!0})}null!=a&&(f.push(a),c+=s(a.circle.radius,a.width),n=o)}}else{let n=t[0];for(let e=1;e<t.length;++e)t[e].radius<n.radius&&(n=t[e]);let s=!1;for(let e=0;e<t.length;++e)if(r(t[e],n)>Math.abs(n.radius-t[e].radius)){s=!0;break}s?c=u=0:(c=n.radius*n.radius*Math.PI,f.push({circle:n,p1:{x:n.x,y:n.y+n.radius},p2:{x:n.x-e,y:n.y+n.radius},width:2*n.radius,large:!0,sweep:!0}))}return u/=2,n&&(n.area=c+u,n.arcArea=c,n.polygonArea=u,n.arcs=f,n.innerPoints=l,n.intersectionPoints=o),c+u}function s(t,e){return t*t*Math.acos(1-e/t)-(t-e)*Math.sqrt(e*(2*t-e))}function r(t,e){return Math.sqrt((t.x-e.x)*(t.x-e.x)+(t.y-e.y)*(t.y-e.y))}function o(t,e,n){if(n>=t+e)return 0;if(n<=Math.abs(t-e))return Math.PI*Math.min(t,e)*Math.min(t,e);const r=e-(n*n-t*t+e*e)/(2*n);return s(t,t-(n*n-e*e+t*t)/(2*n))+s(e,r)}function i(t,e){const n=r(t,e),s=t.radius,o=e.radius;if(n>=s+o||n<=Math.abs(s-o))return[];const i=(s*s-o*o+n*n)/(2*n),a=Math.sqrt(s*s-i*i),l=t.x+i*(e.x-t.x)/n,c=t.y+i*(e.y-t.y)/n,u=-(e.y-t.y)*(a/n),f=-(e.x-t.x)*(a/n);return[{x:l+u,y:c-f},{x:l-u,y:c+f}]}function a(t){const e={x:0,y:0};for(const n of t)e.x+=n.x,e.y+=n.y;return e.x/=t.length,e.y/=t.length,e}function l(t){const e=new Array(t);for(let n=0;n<t;++n)e[n]=0;return e}function c(t,e){return l(t).map((()=>l(e)))}function u(t,e){let n=0;for(let s=0;s<t.length;++s)n+=t[s]*e[s];return n}function f(t){return Math.sqrt(u(t,t))}function h(t,e,n){for(let s=0;s<e.length;++s)t[s]=e[s]*n}function x(t,e,n,s,r){for(let o=0;o<t.length;++o)t[o]=e*n[o]+s*r[o]}function g(t,e,n){const s=(n=n||{}).maxIterations||200*e.length,r=n.nonZeroDelta||1.05,o=n.zeroDelta||.001,i=n.minErrorDelta||1e-6,a=n.minErrorDelta||1e-5,l=void 0!==n.rho?n.rho:1,c=void 0!==n.chi?n.chi:2,u=void 0!==n.psi?n.psi:-.5,f=void 0!==n.sigma?n.sigma:.5;let h;const g=e.length,d=new Array(g+1);d[0]=e,d[0].fx=t(e),d[0].id=0;for(let n=0;n<g;++n){const s=e.slice();s[n]=s[n]?s[n]*r:o,d[n+1]=s,d[n+1].fx=t(s),d[n+1].id=n+1}function p(t){for(let e=0;e<t.length;e++)d[g][e]=t[e];d[g].fx=t.fx}const y=(t,e)=>t.fx-e.fx,m=e.slice(),M=e.slice(),b=e.slice(),z=e.slice();for(let e=0;e<s;++e){if(d.sort(y),n.history){const t=d.map((t=>{const e=t.slice();return e.fx=t.fx,e.id=t.id,e}));t.sort(((t,e)=>t.id-e.id)),n.history.push({x:d[0].slice(),fx:d[0].fx,simplex:t})}h=0;for(let t=0;t<g;++t)h=Math.max(h,Math.abs(d[0][t]-d[1][t]));if(Math.abs(d[0].fx-d[g].fx)<i&&h<a)break;for(let t=0;t<g;++t){m[t]=0;for(let e=0;e<g;++e)m[t]+=d[e][t];m[t]/=g}const e=d[g];if(x(M,1+l,m,-l,e),M.fx=t(M),M.fx<d[0].fx)x(z,1+c,m,-c,e),z.fx=t(z),z.fx<M.fx?p(z):p(M);else if(M.fx>=d[g-1].fx){let n=!1;if(M.fx>e.fx?(x(b,1+u,m,-u,e),b.fx=t(b),b.fx<e.fx?p(b):n=!0):(x(b,1-u*l,m,u*l,e),b.fx=t(b),b.fx<M.fx?p(b):n=!0),n){if(f>=1)break;for(let e=1;e<d.length;++e)x(d[e],1-f,d[0],f,d[e]),d[e].fx=t(d[e])}}else p(M)}return d.sort(y),{fx:d[0].fx,x:d[0]}}function d(t,e,n,s,r,o,i){const a=n.fx,l=u(n.fxprime,e);let c=a,f=a,h=l,g=0;fu