diff options
author | Peter Wu <peter@lekensteyn.nl> | 2014-05-20 23:40:49 +0200 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2014-05-20 23:40:49 +0200 |
commit | 7561200fd09d4e741f20ceb4dc6068fbb36e9ff7 (patch) | |
tree | 7af72ae4c86b036832828b0091e4bfc392811049 /collision.js | |
parent | 8c871b799aa4b32dbc64dee994e15ed9a670f602 (diff) | |
download | d3viz-7561200fd09d4e741f20ceb4dc6068fbb36e9ff7.tar.gz |
Initial commit of users/tweets D3 demo
Diffstat (limited to 'collision.js')
-rw-r--r-- | collision.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/collision.js b/collision.js new file mode 100644 index 0000000..acaf728 --- /dev/null +++ b/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; + }; +} |