(nodes)
| 9090 | initialAngle = Math.PI * (3 - Math.sqrt(5)); |
| 9091 | |
| 9092 | function simulation(nodes) { |
| 9093 | var simulation, |
| 9094 | alpha = 1, |
| 9095 | alphaMin = 0.001, |
| 9096 | alphaDecay = 1 - Math.pow(alphaMin, 1 / 300), |
| 9097 | alphaTarget = 0, |
| 9098 | velocityDecay = 0.6, |
| 9099 | forces = new Map(), |
| 9100 | stepper = timer(step), |
| 9101 | event = dispatch("tick", "end"), |
| 9102 | random = lcg$2(); |
| 9103 | |
| 9104 | if (nodes == null) nodes = []; |
| 9105 | |
| 9106 | function step() { |
| 9107 | tick(); |
| 9108 | event.call("tick", simulation); |
| 9109 | if (alpha < alphaMin) { |
| 9110 | stepper.stop(); |
| 9111 | event.call("end", simulation); |
| 9112 | } |
| 9113 | } |
| 9114 | |
| 9115 | function tick(iterations) { |
| 9116 | var i, n = nodes.length, node; |
| 9117 | |
| 9118 | if (iterations === undefined) iterations = 1; |
| 9119 | |
| 9120 | for (var k = 0; k < iterations; ++k) { |
| 9121 | alpha += (alphaTarget - alpha) * alphaDecay; |
| 9122 | |
| 9123 | forces.forEach(function(force) { |
| 9124 | force(alpha); |
| 9125 | }); |
| 9126 | |
| 9127 | for (i = 0; i < n; ++i) { |
| 9128 | node = nodes[i]; |
| 9129 | if (node.fx == null) node.x += node.vx *= velocityDecay; |
| 9130 | else node.x = node.fx, node.vx = 0; |
| 9131 | if (node.fy == null) node.y += node.vy *= velocityDecay; |
| 9132 | else node.y = node.fy, node.vy = 0; |
| 9133 | } |
| 9134 | } |
| 9135 | |
| 9136 | return simulation; |
| 9137 | } |
| 9138 | |
| 9139 | function initializeNodes() { |
| 9140 | for (var i = 0, n = nodes.length, node; i < n; ++i) { |
| 9141 | node = nodes[i], node.index = i; |
| 9142 | if (node.fx != null) node.x = node.fx; |
| 9143 | if (node.fy != null) node.y = node.fy; |
| 9144 | if (isNaN(node.x) || isNaN(node.y)) { |
| 9145 | var radius = initialRadius * Math.sqrt(0.5 + i), angle = i * initialAngle; |
| 9146 | node.x = radius * Math.cos(angle); |
| 9147 | node.y = radius * Math.sin(angle); |
| 9148 | } |
| 9149 | if (isNaN(node.vx) || isNaN(node.vy)) { |
nothing calls this directly
no test coverage detected