forked from d3/d3-force
-
Notifications
You must be signed in to change notification settings - Fork 54
/
radial.js
67 lines (56 loc) · 1.76 KB
/
radial.js
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
import constant from "./constant.js";
export default function(radius, x, y, z) {
var nodes,
nDim,
strength = constant(0.1),
strengths,
radiuses;
if (typeof radius !== "function") radius = constant(+radius);
if (x == null) x = 0;
if (y == null) y = 0;
if (z == null) z = 0;
function force(alpha) {
for (var i = 0, n = nodes.length; i < n; ++i) {
var node = nodes[i],
dx = node.x - x || 1e-6,
dy = (node.y || 0) - y || 1e-6,
dz = (node.z || 0) - z || 1e-6,
r = Math.sqrt(dx * dx + dy * dy + dz * dz),
k = (radiuses[i] - r) * strengths[i] * alpha / r;
node.vx += dx * k;
if (nDim>1) { node.vy += dy * k; }
if (nDim>2) { node.vz += dz * k; }
}
}
function initialize() {
if (!nodes) return;
var i, n = nodes.length;
strengths = new Array(n);
radiuses = new Array(n);
for (i = 0; i < n; ++i) {
radiuses[i] = +radius(nodes[i], i, nodes);
strengths[i] = isNaN(radiuses[i]) ? 0 : +strength(nodes[i], i, nodes);
}
}
force.initialize = function(initNodes, ...args) {
nodes = initNodes;
nDim = args.find(arg => [1, 2, 3].includes(arg)) || 2;
initialize();
};
force.strength = function(_) {
return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength;
};
force.radius = function(_) {
return arguments.length ? (radius = typeof _ === "function" ? _ : constant(+_), initialize(), force) : radius;
};
force.x = function(_) {
return arguments.length ? (x = +_, force) : x;
};
force.y = function(_) {
return arguments.length ? (y = +_, force) : y;
};
force.z = function(_) {
return arguments.length ? (z = +_, force) : z;
};
return force;
}