cloister
Full Member
Offline
Posts: 138
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); } }