Add simulation.randomSource.
This commit is contained in:
parent
f02c557cce
commit
523a02d963
|
|
@ -13,6 +13,7 @@ function y(d) {
|
|||
export default function(radius) {
|
||||
var nodes,
|
||||
radii,
|
||||
random,
|
||||
strength = 1,
|
||||
iterations = 1;
|
||||
|
||||
|
|
@ -46,8 +47,8 @@ export default function(radius) {
|
|||
y = yi - data.y - data.vy,
|
||||
l = x * x + y * y;
|
||||
if (l < r * r) {
|
||||
if (x === 0) x = jiggle(), l += x * x;
|
||||
if (y === 0) y = jiggle(), l += y * y;
|
||||
if (x === 0) x = jiggle(random), l += x * x;
|
||||
if (y === 0) y = jiggle(random), l += y * y;
|
||||
l = (r - (l = Math.sqrt(l))) / l * strength;
|
||||
node.vx += (x *= l) * (r = (rj *= rj) / (ri2 + rj));
|
||||
node.vy += (y *= l) * r;
|
||||
|
|
@ -77,8 +78,9 @@ export default function(radius) {
|
|||
for (i = 0; i < n; ++i) node = nodes[i], radii[node.index] = +radius(node, i, nodes);
|
||||
}
|
||||
|
||||
force.initialize = function(_) {
|
||||
nodes = _;
|
||||
force.initialize = function(_nodes, _random) {
|
||||
nodes = _nodes;
|
||||
random = _random;
|
||||
initialize();
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
export default function() {
|
||||
return (Math.random() - 0.5) * 1e-6;
|
||||
export default function(random) {
|
||||
return (random() - 0.5) * 1e-6;
|
||||
}
|
||||
|
|
|
|||
10
src/link.js
10
src/link.js
|
|
@ -20,6 +20,7 @@ export default function(links) {
|
|||
nodes,
|
||||
count,
|
||||
bias,
|
||||
random,
|
||||
iterations = 1;
|
||||
|
||||
if (links == null) links = [];
|
||||
|
|
@ -32,8 +33,8 @@ export default function(links) {
|
|||
for (var k = 0, n = links.length; k < iterations; ++k) {
|
||||
for (var i = 0, link, source, target, x, y, l, b; i < n; ++i) {
|
||||
link = links[i], source = link.source, target = link.target;
|
||||
x = target.x + target.vx - source.x - source.vx || jiggle();
|
||||
y = target.y + target.vy - source.y - source.vy || jiggle();
|
||||
x = target.x + target.vx - source.x - source.vx || jiggle(random);
|
||||
y = target.y + target.vy - source.y - source.vy || jiggle(random);
|
||||
l = Math.sqrt(x * x + y * y);
|
||||
l = (l - distances[i]) / l * alpha * strengths[i];
|
||||
x *= l, y *= l;
|
||||
|
|
@ -86,8 +87,9 @@ export default function(links) {
|
|||
}
|
||||
}
|
||||
|
||||
force.initialize = function(_) {
|
||||
nodes = _;
|
||||
force.initialize = function(_nodes, _random) {
|
||||
nodes = _nodes;
|
||||
random = _random;
|
||||
initialize();
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import {x, y} from "./simulation.js";
|
|||
export default function() {
|
||||
var nodes,
|
||||
node,
|
||||
random,
|
||||
alpha,
|
||||
strength = constant(-30),
|
||||
strengths,
|
||||
|
|
@ -63,8 +64,8 @@ export default function() {
|
|||
// Limit forces for very close nodes; randomize direction if coincident.
|
||||
if (w * w / theta2 < l) {
|
||||
if (l < distanceMax2) {
|
||||
if (x === 0) x = jiggle(), l += x * x;
|
||||
if (y === 0) y = jiggle(), l += y * y;
|
||||
if (x === 0) x = jiggle(random), l += x * x;
|
||||
if (y === 0) y = jiggle(random), l += y * y;
|
||||
if (l < distanceMin2) l = Math.sqrt(distanceMin2 * l);
|
||||
node.vx += x * quad.value * alpha / l;
|
||||
node.vy += y * quad.value * alpha / l;
|
||||
|
|
@ -77,8 +78,8 @@ export default function() {
|
|||
|
||||
// Limit forces for very close nodes; randomize direction if coincident.
|
||||
if (quad.data !== node || quad.next) {
|
||||
if (x === 0) x = jiggle(), l += x * x;
|
||||
if (y === 0) y = jiggle(), l += y * y;
|
||||
if (x === 0) x = jiggle(random), l += x * x;
|
||||
if (y === 0) y = jiggle(random), l += y * y;
|
||||
if (l < distanceMin2) l = Math.sqrt(distanceMin2 * l);
|
||||
}
|
||||
|
||||
|
|
@ -89,8 +90,9 @@ export default function() {
|
|||
} while (quad = quad.next);
|
||||
}
|
||||
|
||||
force.initialize = function(_) {
|
||||
nodes = _;
|
||||
force.initialize = function(_nodes, _random) {
|
||||
nodes = _nodes;
|
||||
random = _random;
|
||||
initialize();
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,8 @@ export default function(nodes) {
|
|||
velocityDecay = 0.6,
|
||||
forces = new Map(),
|
||||
stepper = timer(step),
|
||||
event = dispatch("tick", "end");
|
||||
event = dispatch("tick", "end"),
|
||||
random = Math.random;
|
||||
|
||||
if (nodes == null) nodes = [];
|
||||
|
||||
|
|
@ -75,7 +76,7 @@ export default function(nodes) {
|
|||
}
|
||||
|
||||
function initializeForce(force) {
|
||||
if (force.initialize) force.initialize(nodes);
|
||||
if (force.initialize) force.initialize(nodes, random);
|
||||
return force;
|
||||
}
|
||||
|
||||
|
|
@ -116,6 +117,10 @@ export default function(nodes) {
|
|||
return arguments.length ? (velocityDecay = 1 - _, simulation) : 1 - velocityDecay;
|
||||
},
|
||||
|
||||
randomSource: function(_) {
|
||||
return arguments.length ? (random = _, simulation) : random;
|
||||
},
|
||||
|
||||
force: function(name, _) {
|
||||
return arguments.length > 1 ? ((_ == null ? forces.delete(name) : forces.set(name, initializeForce(_))), simulation) : forces.get(name);
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user