source: node_modules/d3-force/src/radial.js@ e4c61dd

Last change on this file since e4c61dd was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[e4c61dd]1import constant from "./constant.js";
2
3export default function(radius, x, y) {
4 var nodes,
5 strength = constant(0.1),
6 strengths,
7 radiuses;
8
9 if (typeof radius !== "function") radius = constant(+radius);
10 if (x == null) x = 0;
11 if (y == null) y = 0;
12
13 function force(alpha) {
14 for (var i = 0, n = nodes.length; i < n; ++i) {
15 var node = nodes[i],
16 dx = node.x - x || 1e-6,
17 dy = node.y - y || 1e-6,
18 r = Math.sqrt(dx * dx + dy * dy),
19 k = (radiuses[i] - r) * strengths[i] * alpha / r;
20 node.vx += dx * k;
21 node.vy += dy * k;
22 }
23 }
24
25 function initialize() {
26 if (!nodes) return;
27 var i, n = nodes.length;
28 strengths = new Array(n);
29 radiuses = new Array(n);
30 for (i = 0; i < n; ++i) {
31 radiuses[i] = +radius(nodes[i], i, nodes);
32 strengths[i] = isNaN(radiuses[i]) ? 0 : +strength(nodes[i], i, nodes);
33 }
34 }
35
36 force.initialize = function(_) {
37 nodes = _, initialize();
38 };
39
40 force.strength = function(_) {
41 return arguments.length ? (strength = typeof _ === "function" ? _ : constant(+_), initialize(), force) : strength;
42 };
43
44 force.radius = function(_) {
45 return arguments.length ? (radius = typeof _ === "function" ? _ : constant(+_), initialize(), force) : radius;
46 };
47
48 force.x = function(_) {
49 return arguments.length ? (x = +_, force) : x;
50 };
51
52 force.y = function(_) {
53 return arguments.length ? (y = +_, force) : y;
54 };
55
56 return force;
57}
Note: See TracBrowser for help on using the repository browser.