-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.h
More file actions
186 lines (145 loc) Β· 3.24 KB
/
graph.h
File metadata and controls
186 lines (145 loc) Β· 3.24 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
#ifndef GRAPH_H
#define GRAPH_H
#include "lib/graphics.c"
#include <math.h>
#include <stdio.h>
enum {
MAX_NUM_NODES = 1024,
MAX_NUM_EDGES = 1024,
MAX_PATH_SIZE = 1024,
};
typedef struct {
b8 enabled;
i64 src;
i64 dst;
f64 width;
b8 hover;
b8 highlight;
} Edge;
typedef struct {
b8 enabled;
f64 radius;
f64 x;
f64 y;
b8 hover;
b8 highlight;
f64 distance;
f64 weight;
f64 drag_x;
f64 drag_y;
} Node;
typedef struct {
Node nodes[MAX_NUM_NODES];
Edge edges[MAX_NUM_EDGES];
i64 path[MAX_PATH_SIZE];
i64 path_size;
} Graph;
Graph graph = {0};
void add_node(f64 x, f64 y) {
for (i64 j = 0; j < MAX_NUM_NODES; ++j) {
Node n2 = graph.nodes[j];
if (!n2.enabled) {
continue;
}
if (fabs(x - n2.x) < 50 + n2.radius && fabs(y - n2.y) < 50 + n2.radius) {
printf("Error: Cannot add node, overlapping nodes detected.\n");
return;
}
}
for (i64 i = 0; i < MAX_NUM_NODES; ++i) {
if (!graph.nodes[i].enabled) {
graph.nodes[i] = (Node){
.enabled = 1,
.x = x,
.y = y,
.radius = 50,
.weight = 2,
};
return;
}
}
}
/*********/
/* EDGES */
/*********/
void add_edge(i64 src, i64 dst) {
assert(src >= 0 && src < MAX_NUM_NODES);
assert(dst >= 0 && dst < MAX_NUM_NODES);
for (i64 i = 0; i < MAX_NUM_EDGES; ++i) {
if (!graph.edges[i].enabled) {
graph.edges[i] = (Edge){
.enabled = 1,
.src = src,
.dst = dst,
.width = 35,
};
return;
}
}
}
void remove_node() {
for (i64 i = 0; i < MAX_NUM_NODES; ++i) {
Node n = graph.nodes[i];
if (n.enabled && n.hover) {
graph.nodes[i].enabled = 0;
for (i64 j = 0; j < MAX_NUM_EDGES; ++j) {
Edge e = graph.edges[j];
if (e.src == i || e.dst == i) {
graph.edges[j].enabled = 0;
}
}
return;
}
}
}
void remove_edge() {
for (i64 i = 0; i < MAX_NUM_EDGES; ++i) {
Edge e = graph.edges[i];
if (e.enabled && e.hover) {
graph.edges[i].enabled = 0;
return;
}
}
}
void update_edge(i64 edge_index) {
assert(edge_index >= 0 && edge_index < MAX_NUM_EDGES);
/* assert(validate_node(graph.edges[edge_index].src)); */
/* assert(validate_node(graph.edges[edge_index].dst)); */
f64 x = platform.cursor_x;
f64 y = platform.cursor_y;
Edge e = graph.edges[edge_index];
Node n0 = graph.nodes[e.src];
Node n1 = graph.nodes[e.dst];
graph.edges[edge_index].hover =
line_contains(n0.x, n0.y, n1.x, n1.y, e.width, x, y);
}
i32 edges_count() {
i32 count = 0;
for (i64 i = 0; i < MAX_NUM_EDGES; ++i) {
if (graph.edges[i].enabled)
++count;
}
return count;
}
/*********/
/* NODES */
/*********/
void update_node(i64 node_index) {
assert(node_index >= 0 && node_index < MAX_NUM_NODES);
f64 x = platform.cursor_x;
f64 y = platform.cursor_y;
Node n = graph.nodes[node_index];
graph.nodes[node_index].hover =
ellipse_contains(n.x - n.radius, n.y - n.radius, n.radius * 2,
n.radius * 2, platform.cursor_x, platform.cursor_y
);
}
i32 nodes_count() {
i32 count = 0;
for (i64 i = 0; i < MAX_NUM_NODES; ++i) {
if (graph.nodes[i].enabled)
++count;
}
return count;
}
#endif