Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function customRadial(radius, x, y) {
- var constant = function(x) {
- return function() {
- return x;
- };
- };
- var nodes,
- strength = constant(0.1),
- strengths,
- radiuses,
- xs,
- ys;
- if (typeof radius !== "function") radius = constant(+radius);
- if (typeof x !== "function") x = constant(x == null ? 0 : +x);
- if (typeof y !== "function") y = constant(y == null ? 0 : +y);
- function force(alpha) {
- for (var i = 0, n = nodes.length; i < n; ++i) {
- var node = nodes[i],
- dx = node.x - xs[i] || 1e-6,
- dy = node.y - ys[i] || 1e-6,
- r = Math.sqrt(dx * dx + dy * dy),
- k = (radiuses[i] - r) * strengths[i] * alpha / r;
- node.vx += dx * k;
- node.vy += dy * k;
- }
- }
- function initialize() {
- if (!nodes) return;
- var i, n = nodes.length;
- strengths = new Array(n);
- radiuses = new Array(n);
- xs = new Array(n);
- ys = new Array(n);
- for (i = 0; i < n; ++i) {
- radiuses[i] = +radius(nodes[i], i, nodes);
- xs[i] = +x(nodes[i], i, nodes);
- ys[i] = +y(nodes[i], i, nodes);
- strengths[i] = isNaN(radiuses[i]) ? 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.radius = function(_) {
- return arguments.length ? (radius = typeof _ === "function" ? _ : constant(+_), initialize(), force) : radius;
- };
- force.x = function(_) {
- return arguments.length ? (x = typeof _ === "function" ? _ : constant(+_), initialize(), force) : x;
- };
- force.y = function(_) {
- return arguments.length ? (y = typeof _ === "function" ? _ : constant(+_), initialize(), force) : y;
- };
- return force;
- }
Advertisement
Add Comment
Please, Sign In to add comment