oompa_l
Full Member
Offline
Posts: 212
Re: problem with VD ch8 network graph example
Reply #2 - Sep 1st , 2008, 2:30am
sure: int nodeCount; Node[] nodes = new Node[100]; HashMap nodeTable = new HashMap(); int edgeCount; Edge[] edges = new Edge[500]; static final color nodeColor = #F0C070; static final color selectColor = #FF3030; static final color fixedColor = #FF8080; static final color edgeColor = #000000; PFont font; Node selection; void setup () { size (600,600); font = createFont("SansSerif", 10); textFont(font); loadData(); smooth(); } void loadData () { /*addEdge("joe", "food"); addEdge("joe", "dog"); addEdge("joe", "tea"); addEdge("joe", "cat"); addEdge("joe", "table"); addEdge("table", "plate"); addEdge("plate", "food"); addEdge("mouse", "food"); addEdge("dog", "food"); addEdge("mouse", "cat"); addEdge("table", "cup"); addEdge("cup", "tea"); addEdge("dog", "cat"); addEdge("cup", "spoon"); addEdge("plate", "fork"); addEdge("dog", "flea1"); addEdge("dog", "flea2"); addEdge("flea1", "flea2"); addEdge("plate", "knife");*/ String[] lines = loadStrings("huckfinn.txt"); //Make the text into a single String object. String line = join(lines, " "); //Replace -- with and actual em dash line = line.replaceAll("--", "\u2014"); //Split into phrases using any of the provided tokens. String[] phrases = splitTokens(line, ".,;:?!\u2014\""); println(phrases); for (int i = 0; i <phrases.length; i++) { //Make this phrase lowercase/ String phrase = phrases[i].toLowerCase(); //Split each phrase into individual words at one or more spaces String[] words = splitTokens(phrase, " "); for (int w=0; w<words.length-1; w++) { addEdge(words[w], words[w+1]); } } } void addEdge (String fromLabel, String toLabel) { Node from = findNode(fromLabel); Node to = findNode(toLabel); Edge e = new Edge(from, to); if (edgeCount == edges.length) { edges = (Edge[]) expand (edges); } edges [edgeCount++] = e; } Node findNode(String label) { label = label.toLowerCase(); Node n = (Node) nodeTable.get (label); if(n==null){ return addNode(label); } return n; } Node addNode (String label) { Node n = new Node(label); if (nodeCount == nodes.length){ nodes = (Node[]) expand (nodes); } nodeTable.put(label, n); nodes[nodeCount++] = n; return n; } void draw () { background (255); for (int i=0; i < edgeCount; i++) { edges[i].relax(); } for (int i=0; i < nodeCount; i++) { edges[i].relax(); } for (int i=0; i < nodeCount; i++) { nodes[i].update(); } for (int i=0; i < edgeCount; i++) { edges[i].draw(); } for (int i=0; i < nodeCount; i++) { edges[i].draw(); } } void mousePressed() { //Ignore anything greater than this distance. float closest = 20; for (int i = 0; i<nodeCount; i++) { Node n = nodes[i]; float d = dist(mouseX, mouseY, n.x, n.y); if (d < closest) { selection = n; closest = d; } } if (selection !=null) { if (mouseButton == LEFT) { selection.fixed = true; } else if (mouseButton == RIGHT) { selection.fixed = false; } } } void mouseDragged() { if (selection !=null) { selection.x = mouseX; selection.y = mouseY; } } void mouseReleased() { selection = null; }