You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

2 lines
23 KiB

!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).BubbleSets={})}(this,(function(t){"use strict";function e(t,e,i,s,n,r){const h=i-t,o=s-e;let a=n-t,c=r-e,u=a*h+c*o,l=0;u<=0?l=0:(a=h-a,c=o-c,u=a*h+c*o,l=u<=0?0:u*u/(h*h+o*o));const x=a*a+c*c-l;return x<0?0:x}function i(t,e,i,s){return(t-i)*(t-i)+(e-s)*(e-s)}function s(t,e,s,n,r){return i(t,e,s,n)<r*r}function n(t){const e=Math.min(t.x1,t.x2),i=Math.max(t.x1,t.x2),s=Math.min(t.y1,t.y2),n=Math.max(t.y1,t.y2);return{x:e,y:s,x2:i,y2:n,width:i-e,height:n-s}}class r{constructor(t,e,i,s){this.x1=t,this.y1=e,this.x2=i,this.y2=s}equals(t){return this.x1===t.x1&&this.y1===t.y1&&this.x2===t.x2&&this.y2===t.y2}draw(t){t.moveTo(this.x1,this.y1),t.lineTo(this.x2,this.y2)}toString(){return`Line(from=(${this.x1},${this.y1}),to=(${this.x2},${this.y2}))`}static from(t){return new r(t.x1,t.y1,t.x2,t.y2)}cuts(t,e){if(this.y1===this.y2)return!1;if(e<this.y1&&e<=this.y2||e>this.y1&&e>=this.y2)return!1;if(t>this.x1&&t>=this.x2)return!1;if(t<this.x1&&t<=this.x2)return!0;return t<=this.x1+(e-this.y1)*(this.x2-this.x1)/(this.y2-this.y1)}distSquare(t,i){return e(this.x1,this.y1,this.x2,this.y2,t,i)}ptClose(t,e,i){if(this.x1<this.x2){if(t<this.x1-i||t>this.x2+i)return!1}else if(t<this.x2-i||t>this.x1+i)return!1;if(this.y1<this.y2){if(e<this.y1-i||e>this.y2+i)return!1}else if(e<this.y2-i||e>this.y1+i)return!1;return!0}}var h,o;!function(t){t[t.POINT=1]="POINT",t[t.PARALLEL=2]="PARALLEL",t[t.COINCIDENT=3]="COINCIDENT",t[t.NONE=4]="NONE"}(h||(h={}));class a{constructor(t,e=0,i=0){this.state=t,this.x=e,this.y=i}}function c(t,e){const i=(e.x2-e.x1)*(t.y1-e.y1)-(e.y2-e.y1)*(t.x1-e.x1),s=(t.x2-t.x1)*(t.y1-e.y1)-(t.y2-t.y1)*(t.x1-e.x1),n=(e.y2-e.y1)*(t.x2-t.x1)-(e.x2-e.x1)*(t.y2-t.y1);if(n){const e=i/n,r=s/n;return 0<=e&&e<=1&&0<=r&&r<=1?new a(h.POINT,t.x1+e*(t.x2-t.x1),t.y1+e*(t.y2-t.y1)):new a(h.NONE)}return new a(0===i||0===s?h.COINCIDENT:h.PARALLEL)}function u(t,e){const i=(e.x2-e.x1)*(t.y1-e.y1)-(e.y2-e.y1)*(t.x1-e.x1),s=(t.x2-t.x1)*(t.y1-e.y1)-(t.y2-t.y1)*(t.x1-e.x1),n=(e.y2-e.y1)*(t.x2-t.x1)-(e.x2-e.x1)*(t.y2-t.y1);if(n){const t=i/n,e=s/n;if(0<=t&&t<=1&&0<=e&&e<=1)return t}return Number.POSITIVE_INFINITY}function l(t,e,i){const s=new Set;return t.width<=0?(s.add(o.LEFT),s.add(o.RIGHT)):e<t.x?s.add(o.LEFT):e>t.x+t.width&&s.add(o.RIGHT),t.height<=0?(s.add(o.TOP),s.add(o.BOTTOM)):i<t.y?s.add(o.TOP):i>t.y+t.height&&s.add(o.BOTTOM),s}function x(t,e){let i=e.x1,s=e.y1;const n=e.x2,r=e.y2,h=Array.from(l(t,n,r));if(0===h.length)return!0;let a=l(t,i,s);for(;0!==a.size;){for(const t of h)if(a.has(t))return!1;if(a.has(o.RIGHT)||a.has(o.LEFT)){let e=t.x;a.has(o.RIGHT)&&(e+=t.width),s+=(e-i)*(r-s)/(n-i),i=e}else{let e=t.y;a.has(o.BOTTOM)&&(e+=t.height),i+=(e-s)*(n-i)/(r-s),s=e}a=l(t,i,s)}return!0}function f(t,e){let i=Number.POSITIVE_INFINITY,s=0;function n(t,n,h,o){let a=u(e,new r(t,n,h,o));a=Math.abs(a-.5),a>=0&&a<=1&&(s++,a<i&&(i=a))}return n(t.x,t.y,t.x2,t.y),n(t.x,t.y,t.x,t.y2),s>1?i:(n(t.x,t.y2,t.x2,t.y2),s>1?i:(n(t.x2,t.y,t.x2,t.y2),0===s?-1:i))}function y(t,e){let i=0;const s=c(t,new r(e.x,e.y,e.x2,e.y));i+=s.state===h.POINT?1:0;const n=c(t,new r(e.x,e.y,e.x,e.y2));i+=n.state===h.POINT?1:0;const o=c(t,new r(e.x,e.y2,e.x2,e.y2));i+=o.state===h.POINT?1:0;const a=c(t,new r(e.x2,e.y,e.x2,e.y2));return i+=a.state===h.POINT?1:0,{top:s,left:n,bottom:o,right:a,count:i}}!function(t){t[t.LEFT=0]="LEFT",t[t.TOP=1]="TOP",t[t.RIGHT=2]="RIGHT",t[t.BOTTOM=3]="BOTTOM"}(o||(o={}));class d{constructor(t,e,i,s){this.x=t,this.y=e,this.width=i,this.height=s}get x2(){return this.x+this.width}get y2(){return this.y+this.height}get cx(){return this.x+this.width/2}get cy(){return this.y+this.height/2}get radius(){return Math.max(this.width,this.height)/2}static from(t){return new d(t.x,t.y,t.width,t.height)}equals(t){return this.x===t.x&&this.y===t.y&&this.width===t.width&&this.height===t.height}clone(){return new d(this.x,this.y,this.width,this.height)}add(t){const e=Math.min(this.x,t.x),i=Math.min(this.y,t.y),s=Math.max(this.x2,t.x+t.width),n=Math.max(this.y2,t.y+t.height);this.x=e,this.y=i,this.width=s-e,this.height=n-i}addPoint(t){const e=Math.min(this.x,t.x),i=Math.min(this.y,t.y),s=Math.max(this.x2,t.x),n=Math.max(this.y2,t.y);this.x=e,this.y=i,this.width=s-e,this.height=n-i}toString(){return`Rectangle[x=${this.x}, y=${this.y}, w=${this.width}, h=${this.height}]`}draw(t){t.rect(this.x,this.y,this.width,this.height)}containsPt(t,e){return t>=this.x&&t<=this.x2&&e>=this.y&&e<=this.y2}get area(){return this.width*this.height}intersects(t){return!(this.area<=0||t.width<=0||t.height<=0)&&(t.x+t.width>this.x&&t.y+t.height>this.y&&t.x<this.x2&&t.y<this.y2)}distSquare(t,e){if(this.containsPt(t,e))return 0;const s=l(this,t,e);return s.has(o.TOP)?s.has(o.LEFT)?i(t,e,this.x,this.y):s.has(o.RIGHT)?i(t,e,this.x2,this.y):(this.y-e)*(this.y-e):s.has(o.BOTTOM)?s.has(o.LEFT)?i(t,e,this.x,this.y2):s.has(o.RIGHT)?i(t,e,this.x2,this.y2):(e-this.y2)*(e-this.y2):s.has(o.LEFT)?(this.x-t)*(this.x-t):s.has(o.RIGHT)?(t-this.x2)*(t-this.x2):0}}function g(t){if(0===t.length)return null;const e=t[0],i=new d(e.x,e.y,0,0);for(const e of t)i.addPoint(e);return i}class p{constructor(t,e,i){this.cx=t,this.cy=e,this.radius=i}get x(){return this.cx-this.radius}get x2(){return this.cx+this.radius}get width(){return 2*this.radius}get y(){return this.cy-this.radius}get y2(){return this.cy+this.radius}get height(){return 2*this.radius}static from(t){return new p(t.cx,t.cy,t.radius)}containsPt(t,e){return i(this.cx,this.cy,t,e)<this.radius*this.radius}distSquare(t,e){const s=i(this.cx,this.cy,t,e);if(s<this.radius*this.radius)return 0;const n=Math.sqrt(s)-this.radius;return n*n}draw(t){t.ellipse(this.cx,this.cy,this.radius,this.radius,0,0,2*Math.PI)}}class w{constructor(t,e=0,i=0,s=0,n=0,r=10,h=10,o=new Float32Array(Math.max(0,r*h)).fill(0)){this.pixelGroup=t,this.i=e,this.j=i,this.pixelX=s,this.pixelY=n,this.width=r,this.height=h,this.area=o}createSub(t,e){return new w(this.pixelGroup,t.x,t.y,e.x,e.y,t.width,t.height)}static fromPixelRegion(t,e){return new w(e,0,0,t.x,t.y,Math.ceil(t.width/e),Math.ceil(t.height/e))}copy(t,e){return new w(this.pixelGroup,this.scaleX(e.x),this.scaleY(e.y),e.x,e.y,t.width,t.height,t.area)}boundX(t){return t<this.i?this.i:t>=this.width?this.width-1:t}boundY(t){return t<this.j?this.j:t>=this.height?this.height-1:t}scaleX(t){return this.boundX(Math.floor((t-this.pixelX)/this.pixelGroup))}scaleY(t){return this.boundY(Math.floor((t-this.pixelY)/this.pixelGroup))}scale(t){const e=this.scaleX(t.x),i=this.scaleY(t.y),s=this.boundX(Math.ceil((t.x+t.width-this.pixelX)/this.pixelGroup)),n=this.boundY(Math.ceil((t.y+t.height-this.pixelY)/this.pixelGroup));return new d(e,i,s-e,n-i)}invertScaleX(t){return Math.round(t*this.pixelGroup+this.pixelX)}invertScaleY(t){return Math.round(t*this.pixelGroup+this.pixelY)}addPadding(t,e){const i=Math.ceil(e/this.pixelGroup),s=this.boundX(t.x-i),n=this.boundY(t.y-i),r=this.boundX(t.x2+i),h=this.boundY(t.y2+i);return new d(s,n,r-s,h-n)}get(t,e){return t<0||e<0||t>=this.width||e>=this.height?Number.NaN:this.area[t+e*this.width]}inc(t,e,i){t<0||e<0||t>=this.width||e>=this.height||(this.area[t+e*this.width]+=i)}set(t,e,i){t<0||e<0||t>=this.width||e>=this.height||(this.area[t+e*this.width]=i)}incArea(t,e){if(t.width<=0||t.height<=0||0===e)return;const i=this.width,s=t.width,n=Math.max(0,t.i),r=Math.max(0,t.j),h=Math.min(t.i+t.width,i),o=Math.min(t.j+t.height,this.height);if(!(o<=0||h<=0||n>=i||o>=this.height))for(let a=r;a<o;a++){const r=(a-t.j)*s,o=a*i;for(let i=n;i<h;i++){const s=t.area[i-t.i+r];0!==s&&(this.area[i+o]+=e*s)}}}fill(t){this.area.fill(t)}fillArea(t,e){const i=t.x+t.y*this.width;for(let s=0;s<t.height;s++){const n=i+s*this.width;this.area.fill(e,n,n+t.width)}}fillHorizontalLine(t,e,i,s){const n=t+e*this.width;this.area.fill(s,n,n+i)}fillVerticalLine(t,e,i,s){const n=t+e*this.width;for(let t=0;t<i;t++)this.area[n+t*this.width]=s}clear(){this.area.fill(0)}toString(){let t="";for(let e=0;e<this.height;e++){const i=e*this.width;for(let e=0;e<this.width;e++){t+=this.area[i+e].toFixed(1).padStart(6),t+=" "}t+="\n"}return t}draw(t,e=!0){if(this.width<=0||this.height<=0)return;t.save(),e&&t.translate(this.pixelX,this.pixelY);const i=this.area.reduce(((t,e)=>Math.min(t,e)),Number.POSITIVE_INFINITY),s=this.area.reduce(((t,e)=>Math.max(t,e)),Number.NEGATIVE_INFINITY),n=t=>(t-i)/(s-i);t.scale(this.pixelGroup,this.pixelGroup);for(let e=0;e<this.width;e++)for(let i=0;i<this.height;i++){const s=this.area[e+i*this.width];t.fillStyle=`rgba(0, 0, 0, ${n(s)})`,t.fillRect(e,i,1,1)}t.restore()}drawThreshold(t,e,i=!0){if(!(this.width<=0||this.height<=0)){t.save(),i&&t.translate(this.pixelX,this.pixelY),t.scale(this.pixelGroup,this.pixelGroup);for(let i=0;i<this.width;i++)for(let s=0;s<this.height;s++){const n=this.area[i+s*this.width];t.fillStyle=n>e?"black":"white",t.fillRect(i,s,1,1)}t.restore()}}}function m(t,e){const i=t=>({x:t.x-e,y:t.y-e,width:t.width+2*e,height:t.height+2*e});return Array.isArray(t)?t.map(i):i(t)}function b(t,i,s){return M(Object.assign(n(t),{distSquare:(i,s)=>e(t.x1,t.y1,t.x2,t.y2,i,s)}),i,s)}function M(t,e,i){const s=m(t,i),n=e.scale(s),r=e.createSub(n,s);return function(t,e,i,s){const n=i*i;for(let r=0;r<t.height;r++)for(let h=0;h<t.width;h++){const o=s(e.invertScaleX(t.i+h),e.invertScaleY(t.j+r));if(0!==o){if(o<n){const e=i-Math.sqrt(o);t.set(h,r,e*e)}}else t.set(h,r,n)}}(r,e,i,((e,i)=>t.distSquare(e,i))),r}function E(t,e,i){const s=e.scale(t),n=e.addPadding(s,i),r=e.createSub(n,{x:t.x-i,y:t.y-i}),h=s.x-n.x,o=s.y-n.y,a=n.x2-s.x2,c=n.y2-s.y2,u=n.width-h-a,l=n.height-o-c,x=i*i;r.fillArea({x:h,y:o,width:u+1,height:l+1},x);const f=[0],y=Math.max(o,h,a,c);{const n=e.invertScaleX(s.x+s.width/2);for(let r=1;r<y;r++){const h=e.invertScaleY(s.y-r),o=t.distSquare(n,h);if(!(o<x))break;{const t=i-Math.sqrt(o);f.push(t*t)}}}const d=[],g=Math.max(h,a),p=Math.max(o,a);for(let n=1;n<g;n++){const r=e.invertScaleX(s.x-n),h=[];for(let n=1;n<p;n++){const o=e.invertScaleY(s.y-n),a=t.distSquare(r,o);if(a<x){const t=i-Math.sqrt(a);h.push(t*t)}else h.push(0)}d.push(h)}for(let t=1;t<Math.min(o,f.length);t++){const e=f[t];r.fillHorizontalLine(h,o-t,u+1,e)}for(let t=1;t<Math.min(c,f.length);t++){const e=f[t];r.fillHorizontalLine(h,o+l+t,u+1,e)}for(let t=1;t<Math.min(h,f.length);t++){const e=f[t];r.fillVerticalLine(h-t,o,l+1,e)}for(let t=1;t<Math.min(c,f.length);t++){const e=f[t];r.fillVerticalLine(h+u+t,o,l+1,e)}for(let t=1;t<h;t++){const e=d[t-1],i=h-t;for(let t=1;t<o;t++)r.set(i,o-t,e[t-1]);for(let t=1;t<c;t++)r.set(i,o+l+t,e[t-1])}for(let t=1;t<a;t++){const e=d[t-1],i=h+u+t;for(let t=1;t<o;t++)r.set(i,o-t,e[t-1]);for(let t=1;t<c;t++)r.set(i,o+l+t,e[t-1])}return r}function I(t,e){return{x:t,y:e}}function N(t,e,s,n){if(0===t.length)return[];const h=function(t){if(t.length<2)return t;let e=0,i=0;return t.forEach((t=>{e+=t.cx,i+=t.cy})),e/=t.length,i/=t.length,t.map((t=>{const s=e-t.cx,n=i-t.cy;return[t,s*s+n*n]})).sort(((t,e)=>t[1]-e[1])).map((t=>t[0]))}(t);return h.map(((t,o)=>{const a=h.slice(0,o);return function(t,e,s,n,h){const o=I(e.cx,e.cy),a=function(t,e,s){let n=Number.POSITIVE_INFINITY;return e.reduce(((e,h)=>{const o=i(t.x,t.y,h.cx,h.cy);if(o>n)return e;const a=new r(t.x,t.y,h.cx,h.cy),c=function(t,e){return t.reduce(((t,i)=>x(i,e)&&function(t,e){function i(t,i,s,n){let h=u(e,new r(t,i,s,n));return h=Math.abs(h-.5),h>=0&&h<=1?1:0}let s=i(t.x,t.y,t.x2,t.y);return s+=i(t.x,t.y,t.x,t.y2),s>1||(s+=i(t.x,t.y2,t.x2,t.y2),s>1||(s+=i(t.x2,t.y,t.x2,t.y2),s>0))}(i,e)?t+1:t),0)}(s,a);return o*(c+1)*(c+1)<n&&(e=h,n=o*(c+1)*(c+1)),e}),null)}(o,s,t);if(null==a)return[];const c=function(t,e,i,s){const n=[],h=[];h.push(t);let o=!0;for(let t=0;t<i&&o;t++)for(o=!1;!o&&h.length>0;){const t=h.pop(),i=O(e,t),a=i?y(t,i):null;if(!i||!a||2!==a.count){o||n.push(t);continue}let c=s,u=S(i,c,a,!0),l=R(u,h)||R(u,n),x=T(u,e);for(;!l&&x&&c>=1;)c/=1.5,u=S(i,c,a,!0),l=R(u,h)||R(u,n),x=T(u,e);if(!u||l||x||(h.push(new r(t.x1,t.y1,u.x,u.y)),h.push(new r(u.x,u.y,t.x2,t.y2)),o=!0),o)continue;c=s,u=S(i,c,a,!1);let f=R(u,h)||R(u,n);for(x=T(u,e);!f&&x&&c>=1;)c/=1.5,u=S(i,c,a,!1),f=R(u,h)||R(u,n),x=T(u,e);u&&!f&&(h.push(new r(t.x1,t.y1,u.x,u.y)),h.push(new r(u.x,u.y,t.x2,t.y2)),o=!0),o||n.push(t)}for(;h.length>0;)n.push(h.pop());return n}(new r(o.x,o.y,a.cx,a.cy),t,n,h);return function(t,e){const i=[];for(;t.length>0;){const s=t.pop();if(0===t.length){i.push(s);break}const n=t.pop(),h=new r(s.x1,s.y1,n.x2,n.y2);O(e,h)?(i.push(s),t.push(n)):t.push(h)}return i}(c,t)}(e,t,a,s,n)})).flat()}function T(t,e){return e.some((e=>e.containsPt(t.x,t.y)))}function R(t,e){return e.some((e=>!!s(e.x1,e.y1,t.x,t.y,.001)||!!s(e.x2,e.y2,t.x,t.y,.001)))}function O(t,e){let i=Number.POSITIVE_INFINITY,s=null;for(const n of t){if(!x(n,e))continue;const t=f(n,e);t>=0&&t<i&&(s=n,i=t)}return s}function S(t,e,i,s){const n=i.top,r=i.left,o=i.bottom,a=i.right;if(s){if(r.state===h.POINT){if(n.state===h.POINT)return I(t.x-e,t.y-e);if(o.state===h.POINT)return I(t.x-e,t.y2+e);const i=t.width*t.height;return t.width*(.5*(r.y-t.y+(a.y-t.y)))<.5*i?r.y>a.y?I(t.x-e,t.y-e):I(t.x2+e,t.y-e):r.y<a.y?I(t.x-e,t.y2+e):I(t.x2+e,t.y2+e)}if(a.state===h.POINT){if(n.state===h.POINT)return I(t.x2+e,t.y-e);if(o.state===h.POINT)return I(t.x2+e,t.y2+e)}const i=t.height*t.width;return t.height*(.5*(n.x-t.x+(a.x-t.x)))<.5*i?n.x>o.x?I(t.x-e,t.y-e):I(t.x-e,t.y2+e):n.x<o.x?I(t.x2+e,t.y-e):I(t.x2+e,t.y2+e)}if(r.state===h.POINT){if(n.state===h.POINT)return I(t.x2+e,t.y2+e);if(o.state===h.POINT)return I(t.x2+e,t.y-e);const i=t.height*t.width;return t.width*(.5*(r.y-t.y+(a.y-t.y)))<.5*i?r.y>a.y?I(t.x2+e,t.y2+e):I(t.x-e,t.y2+e):r.y<a.y?I(t.x2+e,t.y-e):I(t.x-e,t.y-e)}if(a.state===h.POINT){if(n.state===h.POINT)return I(t.x-e,t.y2+e);if(o.state===h.POINT)return I(t.x-e,t.y-e)}const c=t.height*t.width;return t.height*(.5*(n.x-t.x+(a.x-t.x)))<.5*c?n.x>o.x?I(t.x2+e,t.y2+e):I(t.x2+e,t.y-e):n.x<o.x?I(t.x-e,t.y2+e):I(t.x-e,t.y-e)}function P(t,i,s,n){if(!(t.closed?s<t.length:s<t.length-1))return!1;const r=t.get(i),h=t.get(s+1);for(let o=i+1;o<=s;o++){const i=t.get(o);if(e(r.x,r.y,h.x,h.y,i.x,i.y)>n)return!1}return!0}function v(t,e){switch(t){case-2:return(((3-e)*e-3)*e+1)/6;case-1:return((3*e-6)*e*e+4)/6;case 0:return(((-3*e+3)*e+3)*e+1)/6;case 1:return e*e*e/6;default:throw new Error("unknown error")}}class j{constructor(t=[],e=!0){this.points=t,this.closed=e}get(t){const e=t,i=this.points.length;return t<0?this.closed?this.get(t+i):this.points[0]:t>=i?this.closed?this.get(t-i):this.points[i-1]:this.points[e]}get length(){return this.points.length}toString(t=1/0){const e=this.points;if(0===e.length)return"";const i="function"==typeof t?t:function(t){if(!Number.isFinite(t))return t=>t;if(0===t)return Math.round;const e=Math.pow(10,t);return t=>Math.round(t*e)/e}(t);let s="M";for(const t of e)s+=`${i(t.x)},${i(t.y)} L`;return s=s.slice(0,-1),this.closed&&(s+=" Z"),s}draw(t){const e=this.points;if(0!==e.length){t.beginPath(),t.moveTo(e[0].x,e[0].y);for(const i of e)t.lineTo(i.x,i.y);this.closed&&t.closePath()}}sample(t){return function(t=8){return e=>{let i=t,s=e.length;if(i>1)for(s=Math.floor(e.length/i);s<3&&i>1;)i-=1,s=Math.floor(e.length/i);const n=[];for(let t=0,r=0;r<s;r++,t+=i)n.push(e.get(t));return new j(n)}}(t)(this)}simplify(t){return function(t=0){return e=>{if(t<0||e.length<3)return e;const i=[];let s=0;const n=t*t;for(;s<e.length;){let t=s+1;for(;P(e,s,t,n);)t++;i.push(e.get(s)),s=t}return new j(i)}}(t)(this)}bSplines(t){return function(t=6){function e(t,e,i){let s=0,n=0;for(let r=-2;r<=1;r++){const h=t.get(e+r),o=v(r,i);s+=o*h.x,n+=o*h.y}return{x:s,y:n}}return i=>{if(i.length<3)return i;const s=[],n=i.closed,r=i.length+3-1+(n?0:2);s.push(e(i,2-(n?0:2),0));for(let h=2-(n?0:2);h<r;h++)for(let n=1;n<=t;n++)s.push(e(i,h,n/t));return new j(s)}}(t)(this)}apply(t){return t(this)}containsElements(t){const e=g(this.points);return!!e&&t.every((t=>e.containsPt(t.cx,t.cy)&&this.withinArea(t.cx,t.cy)))}withinArea(t,e){if(0===this.length)return!1;let i=0;const s=this.points[0],n=new r(s.x,s.y,s.x,s.y);for(let s=1;s<this.points.length;s++){const r=this.points[s];n.x1=n.x2,n.y1=n.y2,n.x2=r.x,n.y2=r.y,n.cuts(t,e)&&i++}return n.x1=n.x2,n.y1=n.y2,n.x2=s.x,n.y2=s.y,n.cuts(t,e)&&i++,i%2==1}}class A{constructor(t=0){this.count=0,this.arr=[],this.set=new Set,this.arr.length=t}add(t){this.set.add(`${t.x}x${t.y}`),this.arr[this.count++]=t}contains(t){return this.set.has(`${t.x}x${t.y}`)}isFirst(t){if(0===this.count)return!1;const e=this.arr[0];return null!=e&&e.x===t.x&&e.y===t.y}path(){return new j(this.arr.slice(0,this.count))}clear(){this.set.clear(),this.count=0}get(t){return this.arr[t]}get length(){return this.count}}function G(t,e){const i=2*(Math.floor(t.width)+Math.floor(t.height)),s=new A(i);function n(i,s,n,r){const h=t.get(i,s);return Number.isNaN(h)?Number.NaN:h>e?n+r:n}function r(t,e){let i=0;return i=n(t,e,i,1),i=n(t+1,e,i,2),i=n(t,e+1,i,4),i=n(t+1,e+1,i,8),Number.isNaN(i)?-1:i}let h=1;function o(e,i){let n=e,o=i,a=t.invertScaleX(n),c=t.invertScaleY(o);for(let e=0;e<t.width*t.height;e++){const e={x:a,y:c};if(s.contains(e)){if(s.isFirst(e))return!0}else s.add(e);const i=r(n,o);switch(i){case-1:return!0;case 0:case 3:case 2:case 7:h=2;break;case 12:case 14:case 4:h=3;break;case 6:h=0===h?3:2;break;case 1:case 13:case 5:h=0;break;case 9:h=2===h?0:1;break;case 10:case 8:case 11:h=1;break;default:return console.warn("Marching squares invalid state: "+i),!0}switch(h){case 0:o--,c-=t.pixelGroup;break;case 1:o++,c+=t.pixelGroup;break;case 3:n--,a-=t.pixelGroup;break;case 2:n++,a+=t.pixelGroup;break;default:return console.warn("Marching squares invalid state: "+i),!0}}return!0}for(let i=0;i<t.width;i++)for(let n=0;n<t.height;n++){if(t.get(i,n)<=e)continue;const h=r(i,n);if(!(h<0||15===h)&&o(i,n))return s.path()}return null}const B={maxRoutingIterations:100,maxMarchingIterations:20,pixelGroup:4,edgeR0:10,edgeR1:20,nodeR0:15,nodeR1:50,morphBuffer:10,threshold:1,memberInfluenceFactor:1,edgeInfluenceFactor:1,nonMemberInfluenceFactor:-.8,virtualEdges:!0};function L(t){return null!=t&&"number"==typeof t.radius}function Y(t,e){if(L(t)!==L(e))return!1;if(L(t)){const i=e;return t.cx===i.cx&&t.cy===i.cy&&t.radius===i.radius}const i=e;return t.x===i.x&&t.y===i.y&&t.width===i.width&&t.height===i.height}var F;!function(t){t[t.MEMBERS=0]="MEMBERS",t[t.NON_MEMBERS=1]="NON_MEMBERS",t[t.EDGES=2]="EDGES"}(F||(F={}));class ${constructor(t={}){this.dirty=new Set,this.members=[],this.nonMembers=[],this.virtualEdges=[],this.edges=[],this.activeRegion=new d(0,0,0,0),this.potentialArea=new w(1,0,0,0,0,0,0),this.o=Object.assign({},B,t)}pushMember(...t){if(0!==t.length){this.dirty.add(F.MEMBERS);for(const e of t)this.members.push({raw:e,obj:L(e)?p.from(e):d.from(e),area:null})}}removeMember(t){const e=this.members.findIndex((e=>Y(e.raw,t)));return!(e<0)&&(this.members.splice(e,1),this.dirty.add(F.MEMBERS),!0)}removeNonMember(t){const e=this.nonMembers.findIndex((e=>Y(e.raw,t)));return!(e<0)&&(this.nonMembers.splice(e,1),this.dirty.add(F.NON_MEMBERS),!0)}removeEdge(t){const e=this.edges.findIndex((e=>e.obj.equals(t)));return!(e<0)&&(this.edges.splice(e,1),this.dirty.add(F.NON_MEMBERS),!0)}pushNonMember(...t){if(0!==t.length){this.dirty.add(F.NON_MEMBERS);for(const e of t)this.nonMembers.push({raw:e,obj:L(e)?p.from(e):d.from(e),area:null})}}pushEdge(...t){if(0!==t.length){this.dirty.add(F.EDGES);for(const e of t)this.edges.push({raw:e,obj:r.from(e),area:null})}}update(){const t=this.dirty.has(F.MEMBERS),e=this.dirty.has(F.NON_MEMBERS);let i=this.dirty.has(F.EDGES);this.dirty.clear();const s=this.members.map((t=>t.obj));if(this.o.virtualEdges&&(t||e)){const t=N(s,this.nonMembers.map((t=>t.obj)),this.o.maxRoutingIterations,this.o.morphBuffer),e=new Map(this.virtualEdges.map((t=>[t.obj.toString(),t.area])));this.virtualEdges=t.map((t=>{var i;return{raw:t,obj:t,area:null!==(i=e.get(t.toString()))&&void 0!==i?i:null}})),i=!0}let n=!1;if(t||i){const t=q(s,this.virtualEdges.concat(this.edges).map((t=>t.obj))),e=Math.max(this.o.edgeR1,this.o.nodeR1)+this.o.morphBuffer,i=d.from(m(t,e));i.equals(this.activeRegion)||(n=!0,this.activeRegion=i)}if(n){const t=Math.ceil(this.activeRegion.width/this.o.pixelGroup),e=Math.ceil(this.activeRegion.height/this.o.pixelGroup);this.activeRegion.x!==this.potentialArea.pixelX||this.activeRegion.y!==this.potentialArea.pixelY?(this.potentialArea=w.fromPixelRegion(this.activeRegion,this.o.pixelGroup),this.members.forEach((t=>t.area=null)),this.nonMembers.forEach((t=>t.area=null)),this.edges.forEach((t=>t.area=null)),this.virtualEdges.forEach((t=>t.area=null))):t===this.potentialArea.width&&e===this.potentialArea.height||(this.potentialArea=w.fromPixelRegion(this.activeRegion,this.o.pixelGroup))}const r=new Map,h=t=>{if(t.area){const e=`${t.obj.width}x${t.obj.height}x${t.obj instanceof d?"R":"C"}`;r.set(e,t.area)}},o=t=>{if(t.area)return;const e=`${t.obj.width}x${t.obj.height}x${t.obj instanceof d?"R":"C"}`;if(r.has(e)){const i=r.get(e);return void(t.area=this.potentialArea.copy(i,{x:t.obj.x-this.o.nodeR1,y:t.obj.y-this.o.nodeR1}))}const i=t.obj instanceof d?E(t.obj,this.potentialArea,this.o.nodeR1):M(t.obj,this.potentialArea,this.o.nodeR1);t.area=i,r.set(e,i)};this.members.forEach(h),this.nonMembers.forEach(h),this.members.forEach(o),this.nonMembers.forEach((t=>{this.activeRegion.intersects(t.obj)?o(t):t.area=null})),this.edges.forEach((t=>{t.area||(t.area=b(t.obj,this.potentialArea,this.o.edgeR1))})),this.virtualEdges.forEach((t=>{t.area||(t.area=b(t.obj,this.potentialArea,this.o.edgeR1))}))}drawMembers(t){for(const e of this.members)e.obj.draw(t)}drawNonMembers(t){for(const e of this.nonMembers)e.obj.draw(t)}drawEdges(t){for(const e of this.edges)e.obj.draw(t)}drawPotentialArea(t,e=!0){this.potentialArea.draw(t,e)}compute(){if(0===this.members.length)return new j([]);this.dirty.size>0&&this.update();const{o:t,potentialArea:e}=this,i=this.members.map((t=>t.area)),s=this.virtualEdges.concat(this.edges).map((t=>t.area)),n=this.nonMembers.filter((t=>null!=t.area)).map((t=>t.area)),r=this.members.map((t=>t.obj));return X(e,i,s,n,(t=>t.containsElements(r)),t)}}function X(t,e,i,s,n,r={}){const h=Object.assign({},B,r);let o=h.threshold,a=h.memberInfluenceFactor,c=h.edgeInfluenceFactor,u=h.nonMemberInfluenceFactor;const l=(h.nodeR0-h.nodeR1)*(h.nodeR0-h.nodeR1),x=(h.edgeR0-h.edgeR1)*(h.edgeR0-h.edgeR1);for(let r=0;r<h.maxMarchingIterations;r++){if(t.clear(),0!==a){const i=a/l;for(const s of e)t.incArea(s,i)}if(0!==c){const e=c/x;for(const s of i)t.incArea(s,e)}if(0!==u){const e=u/l;for(const i of s)t.incArea(i,e)}const f=G(t,o);if(f&&n(f))return f;if(o*=.95,r<=.5*h.maxMarchingIterations)a*=1.2,c*=1.2;else{if(!(0!==u&&s.length>0))break;u*=.8}}return new j([])}function q(t,e){if(0===t.length)return new d(0,0,0,0);const i=d.from(t[0]);for(const e of t)i.add(e);for(const t of e)i.add(n(t));return i}t.Area=w,t.BubbleSets=$,t.Circle=p,t.Line=r,t.PointPath=j,t.Rectangle=d,t.addPadding=m,t.boundingBox=g,t.calculatePotentialOutline=X,t.calculateVirtualEdges=N,t.circle=function(t,e,i){return{cx:t,cy:e,radius:i}},t.createGenericInfluenceArea=M,t.createLineInfluenceArea=b,t.createOutline=function(t,e=[],i=[],s={}){if(0===t.length)return new j([]);const n=new $(s);return n.pushMember(...t),n.pushNonMember(...e),n.pushEdge(...i),n.compute()},t.createRectangleInfluenceArea=E,t.default=$,t.defaultOptions=B,t.line=function(t,e,i,s){return{x1:t,y1:e,x2:i,y2:s}},t.lineBoundingBox=n,t.point=I,t.rect=function(t,e,i,s){return{x:t,y:e,width:i,height:s}},t.unionBoundingBox=q,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=index.umd.min.js.map