import{thresholdSturges as n,extent as t,ticks as r,nice as o,blur2 as i,max as e}from"d3-array";var a=Array.prototype;var s=a.slice;function ascending(n,t){return n-t}function area(n){var t=0,r=n.length,o=n[r-1][1]*n[0][0]-n[r-1][0]*n[0][1];while(++t()=>n;function contains(n,t){var r,o=-1,i=t.length;while(++oo!==d>o&&r<(l-f)*(o-c)/(d-c)+f&&(i=-i)}return i}function segmentContains(n,t,r){var o;return collinear(n,t,r)&&within(n[o=+(n[0]===t[0])],r[o],t[o])}function collinear(n,t,r){return(t[0]-n[0])*(r[1]-n[1])===(r[0]-n[0])*(t[1]-n[1])}function within(n,t,r){return n<=t&&t<=r||r<=t&&t<=n}function noop(){}var u=[[],[[[1,1.5],[.5,1]]],[[[1.5,1],[1,1.5]]],[[[1.5,1],[.5,1]]],[[[1,.5],[1.5,1]]],[[[1,1.5],[.5,1]],[[1,.5],[1.5,1]]],[[[1,.5],[1,1.5]]],[[[1,.5],[.5,1]]],[[[.5,1],[1,.5]]],[[[1,1.5],[1,.5]]],[[[.5,1],[1,.5]],[[1.5,1],[1,1.5]]],[[[1.5,1],[1,.5]]],[[[.5,1],[1.5,1]]],[[[1,1.5],[1.5,1]]],[[[.5,1],[1,1.5]]],[]];function Contours(){var i=1,e=1,a=n,f=smoothLinear;function contours(n){var i=a(n);if(Array.isArray(i))i=i.slice().sort(ascending);else{const e=t(n,finite);i=r(...o(e[0],e[1],i),i);while(i[i.length-1]>=e[1])i.pop();while(i[1]contour(n,t)))}function contour(n,t){const r=null==t?NaN:+t;if(isNaN(r))throw new Error(`invalid value: ${t}`);var o=[],i=[];isorings(n,r,(function(t){f(t,n,r);area(t)>0?o.push([t]):i.push(t)}));i.forEach((function(n){for(var t,r=0,i=o.length;r=t;u[c<<2].forEach(stitch);while(++o0&&o0&&a=0&&r>=0))throw new Error("invalid size");return i=t,e=r,contours};contours.thresholds=function(n){return arguments.length?(a="function"===typeof n?n:Array.isArray(n)?constant(s.call(n)):constant(n),contours):a};contours.smooth=function(n){return arguments.length?(f=n?smoothLinear:noop,contours):f===smoothLinear};return contours}function finite(n){return isFinite(n)?n:NaN}function above(n,t){return null!=n&&+n>=t}function valid(n){return null==n||isNaN(n=+n)?-Infinity:n}function smooth1(n,t,r,o){const i=o-t;const e=r-t;const a=isFinite(i)||isFinite(e)?i/e:Math.sign(i)/Math.sign(e);return isNaN(a)?n:n+a-.5}function defaultX(n){return n[0]}function defaultY(n){return n[1]}function defaultWeight(){return 1}function density(){var n=defaultX,t=defaultY,o=defaultWeight,a=960,u=500,f=20,c=2,h=3*f,l=a+2*h>>c,d=u+2*h>>c,g=constant(20);function grid(r){var e=new Float32Array(l*d),a=Math.pow(2,-c),s=-1;for(const i of r){var u=(n(i,++s,r)+h)*a,g=(t(i,s,r)+h)*a,v=+o(i,s,r);if(v&&u>=0&&u=0&&gn*i)))(t).map(((n,t)=>(n.value=+o[t],transform(n))))}density.contours=function(n){var t=grid(n),r=Contours().size([l,d]),o=Math.pow(2,2*c),contour=n=>{n=+n;var i=transform(r.contour(t,n*o));i.value=n;return i};Object.defineProperty(contour,"max",{get:()=>e(t)/o});return contour};function transform(n){n.coordinates.forEach(transformPolygon);return n}function transformPolygon(n){n.forEach(transformRing)}function transformRing(n){n.forEach(transformPoint)}function transformPoint(n){n[0]=n[0]*Math.pow(2,c)-h;n[1]=n[1]*Math.pow(2,c)-h}function resize(){h=3*f;l=a+2*h>>c;d=u+2*h>>c;return density}density.x=function(t){return arguments.length?(n="function"===typeof t?t:constant(+t),density):n};density.y=function(n){return arguments.length?(t="function"===typeof n?n:constant(+n),density):t};density.weight=function(n){return arguments.length?(o="function"===typeof n?n:constant(+n),density):o};density.size=function(n){if(!arguments.length)return[a,u];var t=+n[0],r=+n[1];if(!(t>=0&&r>=0))throw new Error("invalid size");return a=t,u=r,resize()};density.cellSize=function(n){if(!arguments.length)return 1<=1))throw new Error("invalid cell size");return c=Math.floor(Math.log(n)/Math.LN2),resize()};density.thresholds=function(n){return arguments.length?(g="function"===typeof n?n:Array.isArray(n)?constant(s.call(n)):constant(n),density):g};density.bandwidth=function(n){if(!arguments.length)return Math.sqrt(f*(f+1));if(!((n=+n)>=0))throw new Error("invalid bandwidth");return f=(Math.sqrt(4*n*n+1)-1)/2,resize()};return density}export{density as contourDensity,Contours as contours};