11 /****************************
13 ***************************/
15 // Free the memory for a graph and substructures
16 void grFree(grGraph *g)
29 // Allocate memory for a new graph, return NULL on any error
30 grGraph* grAlloc(int numnodes, int numedges)
34 g = malloc(sizeof(grGraph));
38 g->nodes = malloc(sizeof(grNode[numnodes]));
39 g->edges = malloc(sizeof(grEdge[numedges]));
40 if (!g->nodes || !g->edges)
43 g->numnodes = numnodes;
44 g->numedges = numedges;
55 /****************************
57 ***************************/
60 void grGenRandom(grGraph *g)
66 for (i = 0; i < ns; i++)
68 grNode *n = &g->nodes[i];
70 n->x = (float)rand() / (RAND_MAX/4);
71 n->y = (float)rand() / (RAND_MAX/4);
72 n->z = (float)rand() / (RAND_MAX/4);
76 for (i = 0; i < es; i++)
78 grEdge *e = &g->edges[i];
80 e->n1 = rand() / (RAND_MAX/ns);
81 e->n2 = rand() / (RAND_MAX/ns);
87 /****************************
88 * Graph drawing functions
89 ***************************/
91 // Draw the graph on the current OpenGL context
92 void graphDrawGL(grGraph *g)
99 glColor4f(0.0, 0.0, 1.0, 0.5);
100 for (i = 0; i < es; i++)
102 grNode *n1 = &g->nodes[g->edges[i].n1];
103 grNode *n2 = &g->nodes[g->edges[i].n2];
105 glBegin(GL_LINE_STRIP);
106 glVertex3f(n1->x, n1->y, n1->z);
107 glVertex3f(n2->x, n2->y, n2->z);
114 for (i = 0; i < ns; i++)
116 grNode *n = &g->nodes[i];
117 //glLoadName(i); // Load point number into depth buffer for selection
118 glColor4f(n->colour, 1.0, 0.3, 0.7);
119 glVertex3f(n->x, n->y, n->z);