We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexSuggestions & BugsWebsite,  Documentation,  Book Bugs › problem with VD ch8 network graph example
Page Index Toggle Pages: 1
problem with VD ch8 network graph example (Read 3468 times)
problem with VD ch8 network graph example
Aug 30th, 2008, 11:52pm
 
Hi

I'm following along and trying to create this example according to the provided code but something is wrong with the nodes...I am getting edges but nothing rendered neither the rectangles nor the text. I tried placing the loadData in the setup position as per another user's post but that didnt seem to work. Any ideas why the thing isn't working?

Re: problem with VD ch8 network graph example
Reply #1 - Aug 31st, 2008, 4:46pm
 
where are you stuck? can you post the code you have so far?
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;
}
Re: problem with VD ch8 network graph example
Reply #3 - Sep 1st, 2008, 2:30am
 
class Edge {
 Node from;
 Node to;
 float len;
 
 Edge(Node from, Node to) {
   this.from = from;
   this.to = to;
   this.len = 50;
 }
 
 void relax () {
   float vx = to.x - from.x;
   float vy = to.y - from.y;
   float d = mag(vx, vy);
   if(d>0) {
     float f = (len-d)/(d*3);
     float dx = f*vx;
     float dy = f*vy;
     to.dx += dx;
     to.dy += dy;
     from.dx -= dx;
     from.dy -= dy;
   }
 }
 
 void draw() {
   stroke(edgeColor);
   strokeWeight(0.3);
   line(from.x,from.y,to.x,to.y);
 }
}
Re: problem with VD ch8 network graph example
Reply #4 - Sep 1st, 2008, 2:30am
 
class Node {
 float x,y;
 float dx, dy;
 boolean fixed;
 String label;
 PFont font;
 
 
 Node(String label) {
   this.label = label;
   x = random(width);
   y = random(height);
 }
 
 void relax () {
   float ddx = 0;
   float ddy = 0;
   
   for (int j = 0; j<nodeCount; j++) {
     Node n = nodes[j];
     if (n !=this) {
       float vx = x - n.x;
       float vy = y - n.y;
       float lensq = vx*vx + vy*vy;
       if (lensq == 0) {
         ddx += random(1);
         ddy += random(1);
       } else if (lensq < 100*100) {
         ddx += vx/ lensq;
         ddy += vy/lensq;
       }
     }
   }
   float dlen = mag(ddx, ddy)/2;
   if (dlen>0) {
     dx += ddx/dlen;
     dy += ddy/dlen;
   }
 }
 
 void update () {
   if (!fixed) {
     x += constrain(dx, -5, 5);
     y += constrain(dy, -5, 5);
     
     x= constrain(x,0,width);
     y= constrain(y, 0, height);
   }
   dx /=2;
   dy /=2;
 }
 /*
 void draw (){
   if(selection == this) {
     fill (selectColor);
   } else if (fixed) {
     fill (fixedColor);
   } else{
     fill (nodeColor);
   }
   
   stroke(0);
   strokeWeight(0.5);
   rectMode(CORNER);
   float w = textWidth(label) + 10;
   float h = textAscent() + textDescent() +4;
   rect(x-w/2, y-h/2, w, h);
   fill(0);
   //font = createFont("SansSerif", 10);
   //textFont(font);
   
   textAlign(CENTER, CENTER);
   text(label, x, y);
 }*/
 
   void draw() {
   fill(nodeColor);
   stroke(0);
   strokeWeight(0.5);
   
   
   float w = textWidth(label) + 10;
   float h = textAscent() + textDescent() +4;
   rect(x-w/2, y-h/2, w, h);
 

  textAlign(CENTER, CENTER);
   text(label, x, y);
 }

}

Re: problem with VD ch8 network graph example
Reply #5 - Sep 1st, 2008, 2:03pm
 
just a typo:
Code:

for (int i=0; i < nodeCount; i++) {
edges[i].draw();
}


that should be:
Code:

for (int i=0; i < nodeCount; i++) {
nodes[i].draw();
}


and in the draw() method within Node, you'll need to change the fill color to something besides nodeColor so that the text shows up a different color than the rectangle it's being drawn upon.
Re: problem with VD ch8 network graph example
Reply #6 - Sep 2nd, 2008, 9:35pm
 
THANKS!!!
Re: problem with VD ch8 network graph example
Reply #7 - Feb 2nd, 2010, 11:48pm
 
I've tried the above, but still have problems...

Embarrassed
Page Index Toggle Pages: 1