Skip to content

Instantly share code, notes, and snippets.

@leonsas
Created July 19, 2012 15:51
Show Gist options
  • Save leonsas/3144862 to your computer and use it in GitHub Desktop.
Save leonsas/3144862 to your computer and use it in GitHub Desktop.
collision from dset
<html>
<head></head>
<body>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.5.0"></script>
<script type="text/javascript">
var dataset = {"nodes": [{"statuses_count": 905, "name": "ThinkBlue Initiative", "friends_count": 1247, "followers_count": 120, "id": 171099799, "screen_name": "ThinkBlueInit"}, {"statuses_count": 218, "name": "Be Liquid", "friends_count": 261, "followers_count": 61, "id": 293498084, "screen_name": "MyWayToDelphi"}, {"statuses_count": 284, "name": "discover5oceans", "friends_count": 1952, "followers_count": 1075, "id": 105296106, "screen_name": "discover5oceans"}, {"statuses_count": 948, "name": "Katie Vogel", "friends_count": 1256, "followers_count": 327, "id": 18323274, "screen_name": "vogelian"}, {"statuses_count": 85, "name": "Nicole McLachlan", "friends_count": 96, "followers_count": 319, "id": 245773605, "screen_name": "pathtoprotect"}, {"statuses_count": 0, "name": "Valerio Pandolfi", "friends_count": 271, "followers_count": 43, "id": 561857821, "screen_name": "ValerioPandolfi"}, {"statuses_count": 23, "name": "Save Our Water Today", "friends_count": 170, "followers_count": 29, "id": 581210698, "screen_name": "SaveOurWater11"}, {"statuses_count": 84, "name": "OceansRockOrg", "friends_count": 373, "followers_count": 178, "id": 575492947, "screen_name": "OceansRockOrg"}, {"statuses_count": 936, "name": "Hands Across Sand", "friends_count": 790, "followers_count": 1799, "id": 98924487, "screen_name": "HandsAcrossSand"}, {"statuses_count": 12, "name": "Happy Peixe", "friends_count": 521, "followers_count": 123, "id": 575435404, "screen_name": "HappyPeixe"}, {"statuses_count": 482, "name": "For Love Of Water", "friends_count": 203, "followers_count": 505, "id": 121707889, "screen_name": "ForLoveOfWater"}, {"statuses_count": 463, "name": "The Ocean Lover", "friends_count": 262, "followers_count": 662, "id": 162550176, "screen_name": "The_Ocean_Lover"}, {"statuses_count": 2184, "name": "CBD-Cntr for Bio Div", "friends_count": 130, "followers_count": 9714, "id": 24457744, "screen_name": "CenterForBioDiv"}, {"statuses_count": 242, "name": "Maine Sea Grant", "friends_count": 244, "followers_count": 1434, "id": 17620786, "screen_name": "Maine_Sea_Grant"}, {"statuses_count": 14888, "name": "EcoInteractive", "friends_count": 107530, "followers_count": 166560, "id": 18932935, "screen_name": "EcoInteractive"}, {"statuses_count": 6908, "name": "Ways 2 Go Green .com", "friends_count": 27037, "followers_count": 28083, "id": 23834147, "screen_name": "Ways2GoGreen"}, {"statuses_count": 961, "name": "SeaTurtleFoundation", "friends_count": 17948, "followers_count": 18456, "id": 70902048, "screen_name": "turtlenews"}, {"statuses_count": 554, "name": "CAISE", "friends_count": 527, "followers_count": 1529, "id": 55656003, "screen_name": "informalscience"}, {"statuses_count": 1525, "name": "Jonas Thing", "friends_count": 76, "followers_count": 437, "id": 149241923, "screen_name": "oceanthing"}, {"statuses_count": 915, "name": "Carl Safina", "friends_count": 72, "followers_count": 5523, "id": 65987618, "screen_name": "carlsafina"}, {"statuses_count": 4466, "name": "US EPA News", "friends_count": 9, "followers_count": 22179, "id": 16567955, "screen_name": "EPAnews"}, {"statuses_count": 11753, "name": "HuffPost Green", "friends_count": 444, "followers_count": 104253, "id": 45577446, "screen_name": "HuffPostGreen"}, {"statuses_count": 16056, "name": "Kate Sheppard", "friends_count": 2190, "followers_count": 30599, "id": 15458181, "screen_name": "kate_sheppard"}, {"statuses_count": 1691, "name": "Water 1st", "friends_count": 8020, "followers_count": 8503, "id": 31683177, "screen_name": "Water1st"}, {"statuses_count": 5042, "name": "Georgia Aquarium", "friends_count": 15639, "followers_count": 26583, "id": 15762845, "screen_name": "GeorgiaAquarium"}, {"statuses_count": 702, "name": "KillTheLionfish", "friends_count": 6265, "followers_count": 7581, "id": 123977178, "screen_name": "KillTheLionfish"}, {"statuses_count": 2339, "name": "David Suzuki FDN", "friends_count": 11558, "followers_count": 44524, "id": 14079041, "screen_name": "DavidSuzukiFDN"}, {"statuses_count": 24, "name": "OceanTalk", "friends_count": 6, "followers_count": 21, "id": 44257681, "screen_name": "OceanTalk"}, {"statuses_count": 93, "name": "Ocean Aware Hawaii", "friends_count": 434, "followers_count": 892, "id": 84245010, "screen_name": "OceanAwareHI"}, {"statuses_count": 1997, "name": "Seafish Authority", "friends_count": 447, "followers_count": 1205, "id": 69290382, "screen_name": "Seafood2aweek"}, {"statuses_count": 28, "name": "Aquaculture Talk", "friends_count": 82, "followers_count": 1187, "id": 18414452, "screen_name": "AquacultureTalk"}, {"statuses_count": 402, "name": "Brian O'Hanlon", "friends_count": 133, "followers_count": 431, "id": 24118076, "screen_name": "BrianOHanlon"}, {"statuses_count": 57, "name": "SEACR", "friends_count": 635, "followers_count": 838, "id": 123011619, "screen_name": "WhaleLab"}, {"statuses_count": 3954, "name": "NOVA", "friends_count": 686, "followers_count": 25017, "id": 17296792, "screen_name": "novapbs"}, {"statuses_count": 1604, "name": "Carol Grant", "friends_count": 242, "followers_count": 851, "id": 15423571, "screen_name": "oceangrant"}, {"statuses_count": 110, "name": "Whale Trackers", "friends_count": 179, "followers_count": 1561, "id": 52914984, "screen_name": "WhaleTrackers"}, {"statuses_count": 8, "name": "Whales & Dolphins", "friends_count": 12, "followers_count": 682, "id": 22699945, "screen_name": "EarthWhales"}, {"statuses_count": 9972, "name": "Christie Wilcox", "friends_count": 1178, "followers_count": 5250, "id": 19808176, "screen_name": "NerdyChristie"}, {"statuses_count": 4469, "name": "Ken Peterson", "friends_count": 310, "followers_count": 1949, "id": 18816614, "screen_name": "aquaken"}, {"statuses_count": 1447, "name": "Rachel Leedom", "friends_count": 270, "followers_count": 334, "id": 29026337, "screen_name": "whalewiz"}, {"statuses_count": 2495, "name": "Stephen Ryan", "friends_count": 504, "followers_count": 876, "id": 31075306, "screen_name": "StephenJRyan"}, {"statuses_count": 4967, "name": "KJ ", "friends_count": 2279, "followers_count": 2579, "id": 66090593, "screen_name": "dolphincode"}, {"statuses_count": 3167, "name": "EcoloBlue", "friends_count": 4607, "followers_count": 4212, "id": 34229272, "screen_name": "EcoloBlue"}, {"statuses_count": 568, "name": "john logioco", "friends_count": 227, "followers_count": 1510, "id": 23371512, "screen_name": "SaveTheBluefin"}, {"statuses_count": 333, "name": "NOAA Teacher at Sea", "friends_count": 402, "followers_count": 3249, "id": 33578432, "screen_name": "TeacherAtSea"}, {"statuses_count": 11713, "name": "National Geographic ", "friends_count": 4964, "followers_count": 241418, "id": 18244358, "screen_name": "NatGeoChannel"}, {"statuses_count": 3992, "name": "DonorsChoose", "friends_count": 7615, "followers_count": 120535, "id": 17232815, "screen_name": "DonorsChoose"}, {"statuses_count": 1265, "name": "Nonprofit Excellence", "friends_count": 787, "followers_count": 2302, "id": 28584305, "screen_name": "BVUVolunteers"}, {"statuses_count": 12148, "name": "CouncilonFoundations", "friends_count": 872, "followers_count": 11649, "id": 32070022, "screen_name": "COF_"}, {"statuses_count": 523, "name": "Lifewater Int'l", "friends_count": 181, "followers_count": 1239, "id": 23011895, "screen_name": "Lifewater"}, {"statuses_count": 149, "name": "Rayne Water", "friends_count": 1987, "followers_count": 682, "id": 108990962, "screen_name": "Rayne_Water"}, {"statuses_count": 4879, "name": "Greenpeace USA", "friends_count": 9150, "followers_count": 58438, "id": 5360012, "screen_name": "greenpeaceusa"}, {"statuses_count": 7304, "name": "A Sea Change", "friends_count": 2168, "followers_count": 4290, "id": 22443383, "screen_name": "aseachange"}, {"statuses_count": 1375, "name": "The Ocean Registry", "friends_count": 1153, "followers_count": 1264, "id": 71859896, "screen_name": "oceanregistry"}, {"statuses_count": 8486, "name": "Amer Cetacean Soc", "friends_count": 22, "followers_count": 7321, "id": 85095636, "screen_name": "CetaceanSociety"}, {"statuses_count": 1987, "name": "Philippe Cousteau", "friends_count": 86, "followers_count": 22779, "id": 32515106, "screen_name": "pcousteau"}, {"statuses_count": 417, "name": "Ocean Institute", "friends_count": 873, "followers_count": 2378, "id": 27664009, "screen_name": "OceanInstitute"}, {"statuses_count": 415, "name": "Fabien Cousteau", "friends_count": 30, "followers_count": 2807, "id": 139881024, "screen_name": "PlantAFish"}, {"statuses_count": 33, "name": "Jonty Laycock", "friends_count": 1865, "followers_count": 665, "id": 163877684, "screen_name": "REDSEARESEARCH"}, {"statuses_count": 5795, "name": "Mark Copeland", "friends_count": 15650, "followers_count": 16863, "id": 93827358, "screen_name": "Eco_Boyz"}, {"statuses_count": 163, "name": "Green Living", "friends_count": 10680, "followers_count": 18448, "id": 31388299, "screen_name": "greenlivingblog"}, {"statuses_count": 407, "name": "WaterGoodness", "friends_count": 641, "followers_count": 417, "id": 54014120, "screen_name": "watergoodness"}, {"statuses_count": 316, "name": "Bill Nye", "friends_count": 30, "followers_count": 330457, "id": 37710752, "screen_name": "TheScienceGuy"}, {"statuses_count": 2312, "name": "WILDfoundation", "friends_count": 2658, "followers_count": 5177, "id": 15838767, "screen_name": "WILDfoundation"}, {"statuses_count": 1043, "name": "Toxipedia", "friends_count": 820, "followers_count": 663, "id": 41751357, "screen_name": "toxipedia"}, {"statuses_count": 4181, "name": "Aquatic Habitat", "friends_count": 39603, "followers_count": 38498, "id": 93666150, "screen_name": "aquatic_habitat"}, {"statuses_count": 1559, "name": "Steff", "friends_count": 173, "followers_count": 247, "id": 112496231, "screen_name": "stefelli"}, {"statuses_count": 82, "name": "Eliza Patrick", "friends_count": 408, "followers_count": 249, "id": 51871115, "screen_name": "Elizylizard"}, {"statuses_count": 2257, "name": "Michelle Young", "friends_count": 1467, "followers_count": 1652, "id": 47189110, "screen_name": "Wild4Salmon"}, {"statuses_count": 7274, "name": "Jen B.", "friends_count": 2055, "followers_count": 1886, "id": 43112612, "screen_name": "jlojackaboo"}, {"statuses_count": 2082, "name": "Margi Prideaux", "friends_count": 657, "followers_count": 910, "id": 111239070, "screen_name": "WildPolitics"}, {"statuses_count": 323, "name": "Social Media Club", "friends_count": 521, "followers_count": 1403, "id": 39977646, "screen_name": "smcswfl"}, {"statuses_count": 4910, "name": "Cloe ", "friends_count": 2207, "followers_count": 2159, "id": 30369308, "screen_name": "cloe2050"}, {"statuses_count": 93, "name": "Shark Safe", "friends_count": 177, "followers_count": 458, "id": 40428837, "screen_name": "sharksafe"}, {"statuses_count": 593, "name": "MarineBio Society", "friends_count": 154, "followers_count": 1292, "id": 137924400, "screen_name": "MBSociety"}, {"statuses_count": 10558, "name": "Dan Beachman", "friends_count": 51, "followers_count": 578, "id": 128271264, "screen_name": "danbeachman"}, {"statuses_count": 1378, "name": "Michigan Sea Grant", "friends_count": 1051, "followers_count": 1920, "id": 14857382, "screen_name": "miseagrant"}, {"statuses_count": 606, "name": "New York Sea Grant", "friends_count": 399, "followers_count": 1087, "id": 59216679, "screen_name": "nyseagrant"}, {"statuses_count": 404, "name": "sweet water", "friends_count": 62, "followers_count": 761, "id": 163863231, "screen_name": "swourbanfarm"}, {"statuses_count": 28116, "name": "CleanDurt", "friends_count": 1071, "followers_count": 1470, "id": 51335573, "screen_name": "cleandurt"}, {"statuses_count": 1249, "name": "OZEANEUM Stralsund", "friends_count": 1279, "followers_count": 1157, "id": 63752426, "screen_name": "OzeaneumPress"}, {"statuses_count": 573, "name": "Reef Check Germany", "friends_count": 97, "followers_count": 164, "id": 40200148, "screen_name": "ReefCheck_de"}, {"statuses_count": 5607, "name": "Bob Timmons", "friends_count": 753, "followers_count": 800, "id": 72911723, "screen_name": "ArtistforOcean"}, {"statuses_count": 3, "name": "Sea Shepherd MyP", "friends_count": 107, "followers_count": 248, "id": 124314524, "screen_name": "SeaShepherd_MyP"}, {"statuses_count": 2703, "name": "Ocean Sentry", "friends_count": 202, "followers_count": 1814, "id": 15015421, "screen_name": "oceansentry"}, {"statuses_count": 979, "name": "WorldAgainstWhaling", "friends_count": 29, "followers_count": 370, "id": 73776804, "screen_name": "WAW_News"}, {"statuses_count": 0, "name": "Anti Whaling", "friends_count": 14, "followers_count": 26, "id": 129486377, "screen_name": "antiwhaling"}, {"statuses_count": 34083, "name": "Leslie SayimaDreamer", "friends_count": 1287, "followers_count": 2028, "id": 38700230, "screen_name": "earthspeakorg"}, {"statuses_count": 1442, "name": "_sea_no_evil_", "friends_count": 364, "followers_count": 1001, "id": 104838548, "screen_name": "_Sea_No_Evil_"}, {"statuses_count": 809, "name": "Annie Crawley", "friends_count": 457, "followers_count": 931, "id": 30004021, "screen_name": "AnnieCrawley"}, {"statuses_count": 891, "name": "DiveIntoImagination", "friends_count": 584, "followers_count": 826, "id": 55397465, "screen_name": "DiveImagination"}, {"statuses_count": 41, "name": "5IMDC", "friends_count": 0, "followers_count": 113, "id": 143948563, "screen_name": "5imdc"}, {"statuses_count": 124, "name": "Carnival of the Blue", "friends_count": 123, "followers_count": 323, "id": 21715258, "screen_name": "carnivalblue"}, {"statuses_count": 7660, "name": "Miriam Goldstein", "friends_count": 582, "followers_count": 2817, "id": 21917716, "screen_name": "MiriamGoldste"}, {"statuses_count": 989, "name": "SEAPLEX Expedition", "friends_count": 258, "followers_count": 1576, "id": 54641525, "screen_name": "seaplexscience"}, {"statuses_count": 631, "name": "Deborah Bassett", "friends_count": 705, "followers_count": 781, "id": 19410691, "screen_name": "debstact"}, {"statuses_count": 2083, "name": "Save The Waves", "friends_count": 284, "followers_count": 4560, "id": 38057076, "screen_name": "SaveTheWaves"}, {"statuses_count": 1630, "name": "The Ocean Voice", "friends_count": 607, "followers_count": 2607, "id": 37727287, "screen_name": "Ocean_Voice_"}, {"statuses_count": 166, "name": "Oceanic Defense", "friends_count": 931, "followers_count": 4144, "id": 32631259, "screen_name": "oceanicdefense"}, {"statuses_count": 1506, "name": "Minnei Soume", "friends_count": 1927, "followers_count": 1728, "id": 39775523, "screen_name": "LadySharkTagger"}, {"statuses_count": 4014, "name": "cbGoGreen", "friends_count": 19653, "followers_count": 18217, "id": 124558745, "screen_name": "cbGoGreen"}]};
var w = 1280,
h = 800;
var nodes = d3.selectAll("circle.node")
.data(dataset.nodes)
color = d3.scale.category10();
var force = d3.layout.force()
.gravity(0.05)
.charge(function(d, i) { return i ? 0 : -2000; })
.nodes(nodes)
.size([w, h]);
var root = nodes[0];
root.radius = 0;
root.fixed = true;
force.start();
var svg = d3.select("#body").append("svg:svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("circle")
.data(nodes.slice(1))
.enter().append("svg:circle")
.attr("r", function(d) { return d.radius - 2; })
.style("fill", function(d, i) { return color(i % 3); });
force.on("tick", function(e) {
var q = d3.geom.quadtree(nodes),
i = 0,
n = nodes.length;
while (++i < n) {
q.visit(collide(nodes[i]));
}
svg.selectAll("circle")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
svg.on("mousemove", function() {
var p1 = d3.svg.mouse(this);
root.px = p1[0];
root.py = p1[1];
force.resume();
});
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 * .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;
};
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment