Repository navigation
Expand file tree
/
Copy pathmy_graph.c
More file actions
520 lines (422 loc) · 15.9 KB
/
Copy pathmy_graph.c
File metadata and controls
520 lines (422 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include "my_graph.h"
// Define M_PI if it's not already defined (needed for some platforms)
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
int next_graph_id = 1;
struct node *node_create(int nodeCount, int colorIndex, Color nodeColors[],
int nodeDiameter, Vector2 mousePosition) {
if (nodeColors == NULL) {
fprintf(stderr, "Error: NULL nodeColors array passed to node_create\n");
return NULL;
}
if (nodeDiameter <= 0) {
fprintf(stderr, "Error: Invalid node diameter value (%d) passed to node_create\n", nodeDiameter);
return NULL;
}
struct node *node = malloc(sizeof(struct node));
if (node == NULL) {
fprintf(stderr, "Error: Memory allocation failed in node_create\n");
return NULL;
}
node->id = nodeCount;
node->graph_id = 0;
node->color = nodeColors[colorIndex];
node->diameter = nodeDiameter;
node->position = mousePosition;
node->originalPosition = mousePosition;
node->selected = false;
node->locked = false;
return node;
}
void print_node_list(struct list_item *node_list) {
struct list_item *p = NULL;
printf("Node list: \n");
if (node_list == NULL)
printf("Empty\n");
for (p = node_list; p != NULL; p = p->next) {
printf("id: %d\nx: %f\ny: %f\n\n", ((struct node *)p->data)->id, ((struct node *)p->data)->position.x, ((struct node *)p->data)->position.y);
}
}
void print_lines_list(struct list_item *lines) {
struct list_item *p = NULL;
printf("\n");
if (lines == NULL)
printf("Empty\n");
for (p = lines; p != NULL; p = p->next) {
printf("start node id: %d end node id: %d\n", ((struct line *)p->data)->startNode.id, ((struct line *)p->data)->endNode.id);
}
}
struct line *line_create(struct node *startNode, struct node *endNode) {
if (startNode == NULL || endNode == NULL) {
fprintf(stderr, "Error: NULL node passed to line_create\n");
return NULL;
}
struct line *new_line = malloc(sizeof(struct line));
if (new_line == NULL) {
fprintf(stderr, "Error: Memory allocation failed in line_create\n");
return NULL;
}
new_line->startNode = *startNode;
new_line->endNode = *endNode;
return new_line;
}
struct line *start_line(struct node *selectedNode) {
if (selectedNode == NULL) {
fprintf(stderr, "Error: NULL node passed to start_line\n");
return NULL;
}
struct line *new_line = malloc(sizeof(struct line));
if (new_line == NULL) {
fprintf(stderr, "Error: Memory allocation failed in start_line\n");
return NULL;
}
new_line->startNode = *selectedNode;
new_line->endNode.position = new_line->startNode.position;
return new_line;
}
void end_line(struct list_item *line_list, struct node *selectedNode) {
if (line_list == NULL || selectedNode == NULL) {
fprintf(stderr, "Error: NULL parameter passed to end_line\n");
return;
}
if (line_list->data == NULL) {
fprintf(stderr, "Error: Line list data is NULL in end_line\n");
return;
}
((struct line *)line_list->data)->endNode = *selectedNode;
}
bool is_empty(struct list_item *list) { return list == NULL; }
// Add to head of list
struct list_item *add_to_list(struct list_item *list, void *n)
{
if (n == NULL) {
fprintf(stderr, "Error: NULL data passed to add_to_list\n");
return list; // Return unchanged list
}
struct list_item *new_list_item = NULL;
new_list_item = malloc(sizeof(struct list_item));
if (new_list_item == NULL) {
fprintf(stderr, "Error: Memory allocation failed in add_to_list\n");
return list; // Return unchanged list
}
new_list_item->data = n;
new_list_item->next = list;
return new_list_item;
}
void free_list(struct list_item *list) {
struct list_item *temp = NULL;
while (list != NULL) {
temp = list;
list = list->next;
free(temp);
}
}
void CustomLog(int msgType, const char *text, va_list args)
{
char timeStr[64] = { 0 };
time_t now = time(NULL);
struct tm *tm_info = localtime(&now);
strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", tm_info);
printf("[%s] ", timeStr);
switch (msgType)
{
case LOG_INFO: printf("[INFO] : "); break;
case LOG_ERROR: printf("[ERROR]: "); break;
case LOG_WARNING: printf("[WARN] : "); break;
case LOG_DEBUG: printf("[DEBUG]: "); break;
default: break;
}
vprintf(text, args);
printf("\n");
}
struct graph cyclic_graph_create(int num_nodes, int *nodeCount, Color nodeColors[], int nodeDiameter, Vector2 mousePosition) {
struct graph cyclic_graph;
cyclic_graph.nodes = NULL;
cyclic_graph.lines = NULL;
cyclic_graph.id = next_graph_id;
if (num_nodes <= 0 || nodeCount == NULL || nodeColors == NULL) {
fprintf(stderr, "Error: Invalid parameters to cyclic_graph_create\n");
return cyclic_graph; // Return empty graph
}
int colorIndex = 0;
Vector2 *node_positions = n_vertices_from_centroid(mousePosition, num_nodes);
if (node_positions == NULL) {
fprintf(stderr, "Error: Failed to generate node positions for cyclic graph\n");
return cyclic_graph; // Return empty graph
}
for (int i = 0; i < num_nodes; i++) {
//create node and add to list
struct node *new_node = node_create(*nodeCount, colorIndex,
nodeColors, nodeDiameter,
node_positions[i]);
if (new_node == NULL) {
fprintf(stderr, "Error: Failed to create node for cyclic graph\n");
free(node_positions);
// Free already created nodes
struct list_item *temp = cyclic_graph.nodes;
while (temp != NULL) {
struct list_item *next = temp->next;
free(temp->data);
free(temp);
temp = next;
}
cyclic_graph.nodes = NULL;
return cyclic_graph; // Return empty graph
}
new_node->graph_id = next_graph_id;
cyclic_graph.nodes = add_to_list(cyclic_graph.nodes, new_node);
//update variables
colorIndex++;
(*nodeCount)++;
}
free(node_positions);
// Check if any nodes were actually created
if (cyclic_graph.nodes == NULL) {
fprintf(stderr, "Error: No nodes created for cyclic graph\n");
return cyclic_graph; // Return empty graph
}
//create lines and add to list
struct list_item *temp = cyclic_graph.nodes;
struct node *startNode = temp->data;
struct node *endNode = startNode;
while (temp != NULL && temp->next != NULL) {
endNode = temp->next->data;
struct line *new_line = line_create(startNode, endNode);
if (new_line == NULL) {
fprintf(stderr, "Error: Failed to create line for cyclic graph\n");
// Continue despite this error
startNode = endNode;
temp = temp->next;
continue;
}
cyclic_graph.lines = add_to_list(cyclic_graph.lines, new_line);
startNode = endNode;
temp = temp->next;
}
// add the last line connecting the last and first nodes
endNode = cyclic_graph.nodes->data;
struct line *new_line = line_create(startNode, endNode);
if (new_line != NULL) {
cyclic_graph.lines = add_to_list(cyclic_graph.lines, new_line);
} else {
fprintf(stderr, "Error: Failed to create final line for cyclic graph\n");
}
next_graph_id++;
return cyclic_graph;
}
Vector2 *n_vertices_from_centroid(Vector2 centroid, int num_nodes) {
if (num_nodes <= 0) {
fprintf(stderr, "Error: Invalid number of nodes (%d) in n_vertices_from_centroid\n", num_nodes);
return NULL;
}
Vector2 *vertices = malloc(num_nodes * sizeof(Vector2));
if (vertices == NULL) {
fprintf(stderr, "Error: Memory allocation failed in n_vertices_from_centroid\n");
return NULL;
}
double radius = 70;
double angle_increment_value = 2 * M_PI / num_nodes;
double angle = 0;
if (num_nodes % 2 == 0) {
angle = -(M_PI / 2) - (M_PI / num_nodes);
} else {
angle = -(M_PI / 2);
}
for (int i = 0; i < num_nodes; i++) {
vertices[i].x = centroid.x + cos(angle) * radius;
vertices[i].y = centroid.y + sin(angle) * radius;
angle += angle_increment_value;
}
return vertices;
}
bool update_lists(struct graph *graph, struct list_item **node_list, struct list_item **line_list) {
if (graph == NULL || node_list == NULL || line_list == NULL) {
fprintf(stderr, "Error: NULL pointer passed to update_lists\n");
return false;
}
printf("\nDEBUG: Updating lists from graph (id=%d)\n", graph->id);
// Count nodes and lines in the graph
int graph_node_count = 0;
int graph_line_count = 0;
struct list_item *count_ptr;
for (count_ptr = graph->nodes; count_ptr != NULL; count_ptr = count_ptr->next) {
graph_node_count++;
}
for (count_ptr = graph->lines; count_ptr != NULL; count_ptr = count_ptr->next) {
graph_line_count++;
}
printf("DEBUG: Graph contains %d nodes and %d lines\n", graph_node_count, graph_line_count);
// Check main lists before update
int main_node_count = 0;
int main_line_count = 0;
for (count_ptr = *node_list; count_ptr != NULL; count_ptr = count_ptr->next) {
main_node_count++;
}
for (count_ptr = *line_list; count_ptr != NULL; count_ptr = count_ptr->next) {
main_line_count++;
}
printf("DEBUG: Main lists before update: %d nodes and %d lines\n",
main_node_count, main_line_count);
// First, add all nodes
int nodes_added = 0;
struct list_item *node_temp = graph->nodes;
while (node_temp != NULL) {
struct node *node_data = (struct node*)node_temp->data;
printf("DEBUG: Adding node %d to main list\n", node_data->id);
struct list_item* new_node = (struct list_item*)malloc(sizeof(struct list_item));
if (new_node == NULL) {
fprintf(stderr, "Error: Memory allocation failed in update_lists\n");
return false;
}
new_node->data = node_temp->data;
new_node->next = *node_list;
*node_list = new_node;
node_temp = node_temp->next;
nodes_added++;
}
// Then add all lines
int lines_added = 0;
struct list_item *line_temp = graph->lines;
while (line_temp != NULL) {
struct line *line_data = (struct line*)line_temp->data;
printf("DEBUG: Adding line (start=%d, end=%d) to main list\n",
line_data->startNode.id, line_data->endNode.id);
struct list_item* new_line = (struct list_item*)malloc(sizeof(struct list_item));
if (new_line == NULL) {
fprintf(stderr, "Error: Memory allocation failed in update_lists\n");
return false;
}
new_line->data = line_temp->data;
new_line->next = *line_list;
*line_list = new_line;
line_temp = line_temp->next;
lines_added++;
}
printf("DEBUG: Added %d nodes and %d lines to main lists\n", nodes_added, lines_added);
// Check main lists after update
main_node_count = 0;
main_line_count = 0;
for (count_ptr = *node_list; count_ptr != NULL; count_ptr = count_ptr->next) {
main_node_count++;
}
for (count_ptr = *line_list; count_ptr != NULL; count_ptr = count_ptr->next) {
main_line_count++;
}
printf("DEBUG: Main lists after update: %d nodes and %d lines\n",
main_node_count, main_line_count);
return true;
}
struct graph complete_graph_create(int num_nodes, int *nodeCount, Color nodeColors[], int nodeDiameter, Vector2 mousePosition) {
struct graph complete_graph;
complete_graph.nodes = NULL;
complete_graph.lines = NULL;
complete_graph.id = next_graph_id;
printf("DEBUG: Creating complete graph with %d nodes\n", num_nodes);
int colorIndex = 0;
// Generate node positions
Vector2 *nodePositions = n_vertices_from_centroid(mousePosition, num_nodes);
if (nodePositions == NULL) {
fprintf(stderr, "Error: Failed to generate node positions for complete graph\n");
return complete_graph; // Return empty graph
}
// Create nodes
for (int i = 0; i < num_nodes; i++) {
//create node and add to list
struct node *new_node = node_create(*nodeCount, colorIndex,
nodeColors, nodeDiameter,
nodePositions[i]);
if (new_node == NULL) {
fprintf(stderr, "Error: Failed to create node for complete graph\n");
free(nodePositions);
// Free already created nodes
struct list_item *temp = complete_graph.nodes;
while (temp != NULL) {
struct list_item *next = temp->next;
free(temp->data);
free(temp);
temp = next;
}
return complete_graph; // Return empty graph
}
printf("DEBUG: Created node %d at position (%f, %f)\n",
*nodeCount, nodePositions[i].x, nodePositions[i].y);
new_node->graph_id = next_graph_id;
complete_graph.nodes = add_to_list(complete_graph.nodes, new_node);
//update variables
colorIndex++;
(*nodeCount)++;
}
// Log nodes just to verify
int node_count = 0;
struct list_item *node_ptr;
for (node_ptr = complete_graph.nodes; node_ptr != NULL; node_ptr = node_ptr->next) {
struct node *node = (struct node*)node_ptr->data;
printf("DEBUG: Node in list: id=%d, position=(%f, %f)\n",
node->id, node->position.x, node->position.y);
node_count++;
}
printf("DEBUG: Found %d nodes in the list\n", node_count);
// Create remaining lines to make complete graph
int expected_lines = (num_nodes * (num_nodes - 1)) / 2;
int actual_lines = 0;
printf("DEBUG: Creating %d lines for complete graph\n", expected_lines);
struct list_item *temp1 = complete_graph.nodes;
int outer_node_id = -1;
while (temp1 != NULL && temp1->next != NULL) {
struct node *startNode = temp1->data;
outer_node_id = startNode->id;
printf("DEBUG: Processing lines from node %d\n", outer_node_id);
struct list_item *temp2 = temp1->next;
while (temp2 != NULL) {
struct node *endNode = temp2->data;
int inner_node_id = endNode->id;
printf("DEBUG: Creating line between nodes %d and %d\n",
outer_node_id, inner_node_id);
// Use the proper line_create function with error checking
struct line *new_line = line_create(startNode, endNode);
if (new_line == NULL) {
fprintf(stderr, "Error: Failed to create line for complete graph between nodes %d and %d\n",
outer_node_id, inner_node_id);
// Continue with graph creation despite this error
temp2 = temp2->next;
continue;
}
// Verify line data
printf("DEBUG: Line created startNode=%d, endNode=%d\n",
new_line->startNode.id, new_line->endNode.id);
complete_graph.lines = add_to_list(complete_graph.lines, new_line);
actual_lines++;
temp2 = temp2->next;
}
temp1 = temp1->next;
}
printf("DEBUG: Created %d out of %d expected lines\n", actual_lines, expected_lines);
// Count and debug list lines
int line_count = 0;
struct list_item *line_ptr;
for (line_ptr = complete_graph.lines; line_ptr != NULL; line_ptr = line_ptr->next) {
struct line *line = (struct line*)line_ptr->data;
printf("DEBUG: Line in list: start=%d, end=%d\n",
line->startNode.id, line->endNode.id);
line_count++;
}
printf("DEBUG: Found %d lines in the list\n", line_count);
free(nodePositions);
next_graph_id++;
return complete_graph;
}
int add_nodes_to_graph(struct node *nodes)
{
int graph_id;
/* Check nodes to see if they already belong to a graph (non-zero).
If one of them belong to a graph, change all the graph ids of the other nodes to that node's graph id.
Return the graph id number. */
return graph_id;
}