diff options
author | Peter Wu <peter@lekensteyn.nl> | 2014-05-21 15:13:16 +0200 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2014-05-21 15:13:16 +0200 |
commit | 0334aa5e0051b59ce4050fd306b26119466e2991 (patch) | |
tree | 656de6f8ab686b3340abdd4dd86d196048d47ff3 /js/collision.js | |
parent | 0d2a9c9fd6c16ced9c684354690cc62dfd3d58f0 (diff) | |
download | d3viz-0334aa5e0051b59ce4050fd306b26119466e2991.tar.gz |
Move scripts to subdir
Our scripts end up in js/, third-party libraries end up in lib/.
Diffstat (limited to 'js/collision.js')
-rw-r--r-- | js/collision.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/js/collision.js b/js/collision.js new file mode 100644 index 0000000..acaf728 --- /dev/null +++ b/js/collision.js @@ -0,0 +1,36 @@ +/* globals d3 */ +'use strict'; +// source: http://mbostock.github.io/d3/talk/20111018/collision.html +function collisionTick(nodes) { + var q = d3.geom.quadtree(nodes), + i = 0, + n = nodes.length; + + while (++i < n) { + q.visit(collide(nodes[i])); + } +} + +function collide(node) { + var r = node.radius + 16, + nx1 = node.x - r, + nx2 = node.x + r, + ny1 = node.y - r, + ny2 = node.y + r; + return function(quad, x1, y1, x2, y2) { + if (quad.point && (quad.point !== node)) { + var x = node.x - quad.point.x, + y = node.y - quad.point.y, + l = Math.sqrt(x * x + y * y), + r = node.radius + quad.point.radius; + if (l < r) { + l = (l - r) / l * 0.5; + node.x -= x *= l; + node.y -= y *= l; + quad.point.x += x; + quad.point.y += y; + } + } + return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1; + }; +} |