| 6397 | ]; |
| 6398 | |
| 6399 | function Contours() { |
| 6400 | var dx = 1, |
| 6401 | dy = 1, |
| 6402 | threshold = thresholdSturges, |
| 6403 | smooth = smoothLinear; |
| 6404 | |
| 6405 | function contours(values) { |
| 6406 | var tz = threshold(values); |
| 6407 | |
| 6408 | // Convert number of thresholds into uniform thresholds. |
| 6409 | if (!Array.isArray(tz)) { |
| 6410 | const e = extent$1(values, finite); |
| 6411 | tz = ticks(...nice$1(e[0], e[1], tz), tz); |
| 6412 | while (tz[tz.length - 1] >= e[1]) tz.pop(); |
| 6413 | while (tz[1] < e[0]) tz.shift(); |
| 6414 | } else { |
| 6415 | tz = tz.slice().sort(ascending$1); |
| 6416 | } |
| 6417 | |
| 6418 | return tz.map(value => contour(values, value)); |
| 6419 | } |
| 6420 | |
| 6421 | // Accumulate, smooth contour rings, assign holes to exterior rings. |
| 6422 | // Based on https://github.com/mbostock/shapefile/blob/v0.6.2/shp/polygon.js |
| 6423 | function contour(values, value) { |
| 6424 | const v = value == null ? NaN : +value; |
| 6425 | if (isNaN(v)) throw new Error(`invalid value: ${value}`); |
| 6426 | |
| 6427 | var polygons = [], |
| 6428 | holes = []; |
| 6429 | |
| 6430 | isorings(values, v, function(ring) { |
| 6431 | smooth(ring, values, v); |
| 6432 | if (area$3(ring) > 0) polygons.push([ring]); |
| 6433 | else holes.push(ring); |
| 6434 | }); |
| 6435 | |
| 6436 | holes.forEach(function(hole) { |
| 6437 | for (var i = 0, n = polygons.length, polygon; i < n; ++i) { |
| 6438 | if (contains$2((polygon = polygons[i])[0], hole) !== -1) { |
| 6439 | polygon.push(hole); |
| 6440 | return; |
| 6441 | } |
| 6442 | } |
| 6443 | }); |
| 6444 | |
| 6445 | return { |
| 6446 | type: "MultiPolygon", |
| 6447 | value: value, |
| 6448 | coordinates: polygons |
| 6449 | }; |
| 6450 | } |
| 6451 | |
| 6452 | // Marching squares with isolines stitched into rings. |
| 6453 | // Based on https://github.com/topojson/topojson-client/blob/v3.0.0/src/stitch.js |
| 6454 | function isorings(values, value, callback) { |
| 6455 | var fragmentByStart = new Array, |
| 6456 | fragmentByEnd = new Array, |