From 6e3d4a9fca95716289510d804433c061ef2adb17 Mon Sep 17 00:00:00 2001 From: Nils Schneider Date: Sat, 9 Jun 2012 02:26:19 +0200 Subject: [PATCH] pacman foo --- html/force.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/html/force.js b/html/force.js index f7e856a..daec041 100644 --- a/html/force.js +++ b/html/force.js @@ -176,6 +176,12 @@ vis.append("g").attr("class", "links") vis.append("g").attr("class", "nodes") +var pm = vis.append("path") + .style("fill", "#ff0") + .style("stroke", "#000") + .style("stroke-width", 2.5) + .style("stroke-linejoin", "round") + var linkedByIndex var force = d3.layout.force() @@ -199,6 +205,13 @@ var force = d3.layout.force() } }) +var angle = d3.scale.linear() + .domain([0, 1, 2, 3]) + .range([0.01, Math.PI/4, 0.01, Math.PI/4]) +var a = 0 + +var p = {x: 0, y: 0} + force.on("tick", function() { var size = force.size() var nodes = force.nodes() @@ -225,7 +238,45 @@ force.on("tick", function() { vis.selectAll(".node").attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; - }) + }) + + a = (a + 0.10)%2 + + pm.attr("d", d3.svg.arc().innerRadius(0) + .outerRadius(24).endAngle(-angle(a) + Math.PI/2 + 2*Math.PI).startAngle(angle(a) + Math.PI/2)) + + var closest = null + var dd = Infinity; + for (i = 0; i < n; i++) { + var o = nodes[i] + if (o.dead) + continue + + var d = Math.pow((o.x - p.x),2) + Math.pow( o.y - p.y, 2) + if (d < dd) { + dd = d + closest = o + } + } + + var dx = closest.x - p.x + var dy = closest.y - p.y + + var d = Math.sqrt(Math.pow(dx,2) + Math.pow(dy,2)) + + dx = dx/d + dy = dy/d + + if (d>1) { + + p.x += dx + p.y += dy + } else { + closest.dead = true + } + + pm.attr("transform", "matrix(" + [dx, dy, -dy, dx, p.x, p.y].join(",") + ")") + }) var data