forked from d3/d3.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd3-contour.v0.0.js
More file actions
77 lines (66 loc) · 1.78 KB
/
Copy pathd3-contour.v0.0.js
File metadata and controls
77 lines (66 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// https://d3js.org/d3-contour/ Version 0.0.1. Copyright 2017 Mike Bostock.
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(factory((global.d3 = global.d3 || {})));
}(this, (function (exports) { 'use strict';
var DX = [1,0,1,1,-1,0,-1,1,0,0,0,0,-1,0,-1,NaN];
var DY = [0,-1,0,0,0,-1,0,0,1,-1,1,1,0,-1,0,NaN];
var contour = function(test, start) {
var s = start || findStart(test),
c = [], // contour polygon
x = s[0], // current x position
y = s[1], // current y position
dx = 0, // next x direction
dy = 0, // next y direction
pdx = NaN, // previous x direction
pdy = NaN, // previous y direction
i = 0;
do {
// determine marching squares index
i = 0;
if (test(x - 1, y - 1)) i += 1;
if (test(x, y - 1)) i += 2;
if (test(x - 1, y)) i += 4;
if (test(x, y)) i += 8;
// determine next direction
if (i === 6) {
dx = pdy === -1 ? -1 : 1;
dy = 0;
} else if (i === 9) {
dx = 0;
dy = pdx === 1 ? -1 : 1;
} else {
dx = DX[i];
dy = DY[i];
}
// update contour polygon
if (dx != pdx && dy != pdy) {
c.push([x, y]);
pdx = dx;
pdy = dy;
}
x += dx;
y += dy;
} while (s[0] != x || s[1] != y);
return c;
};
// search for a starting point; begin at origin
// and proceed along outward-expanding diagonals
function findStart(test) {
var x = 0,
y = 0;
while (!test(x, y)) {
if (x === 0) {
x = y + 1;
y = 0;
} else {
x = x - 1;
y = y + 1;
}
}
return [x, y];
}
exports.contour = contour;
Object.defineProperty(exports, '__esModule', { value: true });
})));