6 /****************************
8 ***************************/
10 // Free the memory for a graph and substructures
11 void grFree(grGraph *g)
24 // Allocate memory for a new graph, return NULL on any error
25 grGraph* grAlloc(int numnodes, int numedges)
29 g = malloc(sizeof(grGraph));
33 g->nodes = malloc(sizeof(grNode[numnodes]));
34 g->edges = malloc(sizeof(grEdge[numedges]));
35 if (!g->nodes || !g->edges)
38 g->numnodes = numnodes;
39 g->numedges = numedges;
50 /****************************
52 ***************************/
55 void grGenRandom(grGraph *g)
61 for (i = 0; i < ns; i++)
63 grNode *n = &g->nodes[i];
65 n->x = (float)rand() / (RAND_MAX/4);
66 n->y = (float)rand() / (RAND_MAX/4);
67 n->z = (float)rand() / (RAND_MAX/4);
71 for (i = 0; i < es; i++)
73 grEdge *e = &g->edges[i];
75 e->n1 = rand() / (RAND_MAX/ns);
76 e->n2 = rand() / (RAND_MAX/ns);
82 /****************************
83 * Graph drawing functions
84 ***************************/
86 // Draw the graph on the current OpenGL context
87 void graphDrawGL(grGraph *g)
94 glColor4f(0.0, 0.0, 1.0, 0.5);
95 for (i = 0; i < es; i++)
97 grNode *n1 = &g->nodes[g->edges[i].n1];
98 grNode *n2 = &g->nodes[g->edges[i].n2];
100 glBegin(GL_LINE_STRIP);
101 glVertex3f(n1->x, n1->y, n1->z);
102 glVertex3f(n2->x, n2->y, n2->z);
109 for (i = 0; i < ns; i++)
111 grNode *n = &g->nodes[i];
112 //glLoadName(i); // Load point number into depth buffer for selection
113 glColor4f(n->colour, 1.0, 0.3, 0.7);
114 glVertex3f(n->x, n->y, n->z);