-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.js
203 lines (189 loc) · 6.56 KB
/
graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
var graph = d3.select("#graph");
var nodes = graph.selectAll(".graph-node");
var edges = graph.selectAll(".graph-edge");
var linkForce = d3.forceLink().distance(75);
var centerForce = d3.forceCenter();
var sim = d3.forceSimulation()
.force("charge", d3.forceManyBody().strength(-100))
.force("link", linkForce)
.force("center", centerForce)
.on("tick", updateGraph);
var drag = d3.drag()
.on("start", function() {
var s = d3.event.subject;
s.fx = s.x;
s.fy = s.y;
sim.alphaTarget(1.0).restart();
})
.on("drag", function() {
var s = d3.event.subject;
s.fx = d3.event.x;
s.fy = d3.event.y;
})
.on("end", function(d) {
var s = d3.event.subject;
s.fx = s.fy = null;
sim.alphaTarget(0.0);
});
//currently selected node
var selected = null;
d3.select("svg")
.call(d3.zoom()
.on("zoom", function() {
graph.attr("transform", d3.event.transform);
}))
.on("dblclick.zoom", null);
//generates edges to form a connected graph and sets the initial counts
//for each node. The total count is at least as large as the genus.
function generateNewEdges(nodes, surplus, range) {
//generate the edge data. We want a connected graph, so
//we keep an array of the nodes not in the connected segment,
//removing them when we connect an edge between them.
var ncon = nodes.slice(1); //non-connected nodes
var conn = [nodes[0]]; //connected nodes
var edges = [];
while(ncon.length > 0) {
var node1 = conn[Math.floor(Math.random() * conn.length)];
var node2 = ncon.splice(Math.floor(Math.random() * ncon.length), 1)[0];
node1.adj.push(node2);
node2.adj.push(node1);
conn.push(node2);
edges.push({ source: node1, target: node2 });
}
//add a few cycles in
for(var i = 0; i < nodes.length / 3; i++) {
var node1;
var node2;
do { node1 = nodes[Math.floor(Math.random() * nodes.length)]; }
while(node1.adj.length === nodes.length - 1);
do { node2 = nodes[Math.floor(Math.random() * nodes.length)]; }
while(node1.id === node2.id || node1.adj.find(el => el.id === node2.id));
node1.adj.push(node2);
node2.adj.push(node1);
edges.push({ source: node1, target: node2 });
}
var genus = edges.length - nodes.length + 1;
//assign an initial value
for(var idx in nodes) {
var count = Math.floor(Math.random() * range * 2) - range;
nodes[idx].count = count;
genus -= count;
}
//assign leftovers to guarantee solvability
while(genus-- > 0) {
nodes[Math.floor(Math.random() * nodes.length)].count++;
}
//make the total count exactly equal to the genus for extra challenge
if(!surplus) {
console.log(surplus);
while(++genus < 0) {
nodes[Math.floor(Math.random() * nodes.length)].count--;
}
}
return edges;
}
function generateGraph(num, surplus, range) {
//generate the node data
sim.stop();
resetStats();
var newData = Array.from(Array(num)).map((e, i) => ({ id: i, count: 0, adj: [] }));
var newEdges = generateNewEdges(newData, surplus, range);
//generate edges
edges = graph.selectAll(".graph-edge")
.data(newEdges);
edges.exit().remove();
edges = edges.enter()
.append("line")
.attr("class", "graph-edge")
.merge(edges);
//generate nodes
nodes = graph.selectAll(".graph-node")
.data(newData);
nodes.exit().remove();
nodes = nodes.enter()
.append("g")
.on("click", handleClick)
.on("contextmenu", handleContext)
.on("mouseover", handleHover)
.on("mouseout", handleHoverEnd)
.attr("class", "graph-node")
.call(sel => sel.append("circle").attr("r", 20))
.call(sel => sel.append("text"))
.merge(nodes)
.call(drag)
.call(updateText)
.order();
linkForce.links(newEdges);
var rect = graph.node().parentNode.getBoundingClientRect();
centerForce.x((rect.right - rect.left) / 2);
centerForce.y((rect.bottom - rect.top) / 2);
sim.nodes(nodes.data()).alpha(1.0).restart();
}
function updateGraph() {
nodes.attr("transform", d => "translate(" + d.x + "," + d.y + ")");
edges.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);
}
function handleClick(d) {
//give to adjacent
d.count -= d.adj.length;
d.adj.forEach(el => el.count++);
updateText(nodes);
updateStats();
}
function handleContext(d) {
d3.event.preventDefault();
//take from adjacent
d.count += d.adj.length;
d.adj.forEach(el => el.count--);
updateText(nodes);
updateStats();
}
function updateText(sel) {
sel.select("text")
.text(d => d.count)
.classed("negative", d => d.count < 0);
}
var statMoves = document.getElementById("stat-moves");
function updateStats() {
statMoves.innerHTML++;
}
function resetStats() {
statMoves.innerHTML = 0;
}
function handleHover(d) {
var affected = nodes.filter(d1 => d1 === d || d.adj.indexOf(d1) >= 0);
affected.append("circle")
.attr("class", "outline")
.attr("r", 25);
edges.filter(d1 =>
(d1.source === d || d1.target === d)
&& affected.data().indexOf(d1.source) >= 0
&& affected.data().indexOf(d1.target) >= 0)
.classed("highlighted", true);
}
function handleHoverEnd(d) {
var affected = nodes.filter(d1 => d1 === d || d.adj.indexOf(d1) >= 0);
affected.select(".outline")
.remove();
edges.filter(d1 =>
(d1.source === d || d1.target === d)
&& affected.data().indexOf(d1.source) >= 0
&& affected.data().indexOf(d1.target) >= 0)
.classed("highlighted", false);
}
//input event handlers
var inputNodeCount = document.getElementById("input-node-count");
var inputSurplus = document.getElementById("input-surplus");
var inputRange = document.getElementById("input-range");
var generate = document.getElementById("input-generate").onclick = function() {
var nodeCount = parseInt(inputNodeCount.value, 10);
var allowSurplus = inputSurplus.checked;
var range = parseInt(inputRange.value, 10);
if(nodeCount && nodeCount >= 5 && range && range >= 1) {
generateGraph(nodeCount, allowSurplus, range);
}
};
generate();