summaryrefslogtreecommitdiff
path: root/src/x.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/x.js')
-rw-r--r--src/x.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/x.js b/src/x.js
new file mode 100644
index 0000000..894e748
--- /dev/null
+++ b/src/x.js
@@ -0,0 +1,41 @@
+import constant from "./constant";
+
+export default function(x) {
+ var strength = constant(0.1),
+ nodes,
+ strengths,
+ xz;
+
+ if (typeof x !== "function") x = constant(x == null ? 0 : +x);
+
+ function force(alpha) {
+ for (var i = 0, n = nodes.length, node; i < n; ++i) {
+ node = nodes[i], node.vx += (xz[i] - node.x) * strengths[i] * alpha;
+ }
+ }
+
+ function initialize() {
+ if (!nodes) return;
+ var i, n = nodes.length;
+ strengths = new Array(n);
+ xz = new Array(n);
+ for (i = 0; i < n; ++i) {
+ strengths[i] = isNaN(xz[i] = +x(nodes[i], i, nodes)) ? 0 : +strength(nodes[i], i, nodes);
+ }
+ }
+
+ force.initialize = function(_) {
+ nodes = _;
+ initialize();
+ };
+
+ force.strength = function(_) {
+ return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength;
+ };
+
+ force.x = function(_) {
+ return arguments.length ? (x = typeof _ === "function" ? _ : constant(+_), initialize(), force) : x;
+ };
+
+ return force;
+}