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.
Page Index Toggle Pages: 1
Graph help? (Read 201 times)
Graph help?
Feb 27th, 2009, 8:29am
 
Hi all,

I'm super new at using Processing, and have a quick question about generating graphs...I want to import a .mat file that contains an adjacency matrix from which I can generate a graph where the vertices are equally spaced along a circle. Does anyone know how to do this/know of any relevant commands?

Thanks so much!!
Re: Graph help?
Reply #1 - Feb 27th, 2009, 6:47pm
 
First, spend some time looking at the examples on this website to get a feel for how to work with Processing; what the setup() and draw() functions do, how they relate, et cetera.  If you've ever worked with computer graphics before, it shouldn't take you long to get oriented to Processing's way of doing things.

I assume you can handle working with the matrix and constructing a useful in-memory representation of the graph.  It sounds like you know what you're doing there.  The problem, then, is in drawing the results in the way you describe.

For this, I will assume that you have turned your matrix into an ArrayList of graph edges, each edge represented by a pair of integers representing the ID numbers of the graph nodes, and that the ID numbers run contiguously from 0 up to N-1.  For each edge, then, all you need to do is get the indexes of the nodes, use each index to generate coordinates around a circle, and connect them with lines:

void draw()
{
 // the drawing is easier if (0,0) is in the center of the screen:
 translate(width/2, height/2);  

 ArrayList edges; // I leave out the part where you fill this with data.

 float radius = width/2; // we'll assume the drawing window is square.
 for(int edge = 0; edge < edges.size(); edge++)
 {
// I presume that edges holds objects that have ".node1" and ".node2" properties
   int v1 = edges.get(edge).node1;
   int v2 = edges.get(edge).node2;

   float x1, y1, x2, y2; // coordinates for each node
   float angl1, angle2;  // each node's angle around the circle.

   // you were awake in high school trig class, right?
   angle1 = v1 * (2*PI / edges.size());
   x1 = radius * cos(angle1);
   y1 = radius * sin(angle1);

   angle2 = v2 * (2*PI / edges.size());
   x2 = radius * cos(angle2);
   y2 = radius * sin(angle2);
 
   line(x1, y1, x2, y2);
 }
}
Re: Graph help?
Reply #2 - Mar 3rd, 2009, 5:58am
 
Ahhhh thanks SO much, cloister! This is super useful, and makes my life so much easier. I really appreciate it.
Page Index Toggle Pages: 1