-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
291 lines (228 loc) · 6.56 KB
/
main.c
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
#include "graph.h"
#include "lib/graphics.c"
#include <stdio.h>
#include "algorithms.h"
void draw_graph(void) {
for (i64 i = 0; i < MAX_NUM_EDGES; ++i) {
Edge e = graph.edges[i];
Node n0 = graph.nodes[e.src];
Node n1 = graph.nodes[e.dst];
u32 color = 0x7f7f7f; // grey color
if (e.highlight)
color = 0xff00ff; // pink color
if (e.hover)
color = 0x007f00; // green color
// FIXME: color of line on node
if (e.enabled)
fill_line(OP_SET, color, n0.x, n0.y, n1.x, n1.y, e.width);
}
for (i64 i = 0; i < MAX_NUM_NODES; ++i) {
Node n = graph.nodes[i];
u32 color = 0x7f7f7f; // grey color
if (n.highlight)
color = 0xfff0ff; // no name color
if (n.hover)
color = 0x007f00; // green color
if (n.enabled)
fill_ellipse(OP_SET, color, n.x - n.radius, n.y - n.radius, n.radius * 2,
n.radius * 2);
}
}
i32 readInt(FILE *f) {
i32 x;
fscanf(f, "%d", &x);
return x;
}
void writeInt(FILE *f, i32 value) { fprintf(f, "%d ", value); }
i32 main() {
platform = (Platform){
.title = "Graph",
.frame_width = 960,
.frame_height = 720,
};
p_init();
b8 adding_edge = 0;
i64 adding_src = 0;
i64 adding_dst = 0;
b8 path_changed = 0;
i64 path_src = -1;
i64 path_dst = -1;
b8 dragging = 0;
i64 drag_node_index = -1;
f64 drag_x0 = 0;
f64 drag_y0 = 0;
f64 offset_x = 0;
f64 offset_y = 0;
{
FILE *n = fopen("coords-write.txt", "rb");
i32 num_nodes = readInt(n);
for (i64 i = 0; i < num_nodes; ++i) {
f64 x = readInt(n);
f64 y = readInt(n);
add_node(x, y);
};
i32 num_edges = readInt(n);
for (i64 i = 0; i < num_edges; ++i) {
i32 src = readInt(n);
i32 dst = readInt(n);
add_edge(src, dst);
};
fclose(n);
}
while (!platform.done) {
p_wait_events();
b8 hover_node = 0;
fill_rectangle(OP_SET, 0xffffff, 0, 0, platform.frame_width,
platform.frame_height);
if (platform.key_pressed[BUTTON_RIGHT])
for (i64 i = 0; i < MAX_NUM_NODES; ++i) {
if (graph.nodes[i].enabled && graph.nodes[i].hover) {
adding_edge = 1;
adding_src = i;
adding_dst = i;
break;
}
}
if (platform.key_pressed[BUTTON_LEFT]) {
f64 x = platform.cursor_x;
f64 y = platform.cursor_y;
b8 node_found = 0;
for (i64 i = 0; i < MAX_NUM_NODES; ++i) {
if (graph.nodes[i].enabled && graph.nodes[i].hover) {
drag_node_index = i;
dragging = 1;
node_found = 1;
drag_x0 = platform.cursor_x;
drag_y0 = platform.cursor_y;
for (i64 j = 0; j < MAX_NUM_NODES; ++j) {
graph.nodes[j].drag_x = graph.nodes[j].x;
graph.nodes[j].drag_y = graph.nodes[j].y;
}
/* offset_x = nodes[i].x - platform.cursor_x; */
/* offset_y = nodes[i].y - platform.cursor_y; */
}
}
if (!node_found)
add_node(x, y);
path_changed = 1;
}
if (!platform.key_down[BUTTON_LEFT]) {
dragging = 0;
path_changed = 1;
drag_node_index = -1;
}
if (dragging) {
f64 dx = platform.cursor_x - drag_x0;
f64 dy = platform.cursor_y - drag_y0;
graph.nodes[drag_node_index].x = graph.nodes[drag_node_index].drag_x + dx;
graph.nodes[drag_node_index].y = graph.nodes[drag_node_index].drag_y + dy;
for (i64 i = 0; i < MAX_NUM_NODES; ++i) {
b8 is_neighbor = 0;
if (i == drag_node_index)
continue;
for (i64 j = 0; j < MAX_NUM_EDGES; ++j) {
Edge e = graph.edges[j];
if ((e.src == drag_node_index && e.dst == i) ||
(e.src == i && e.dst == drag_node_index)) {
is_neighbor = 1;
break;
}
}
if (is_neighbor) {
graph.nodes[i].x = graph.nodes[i].drag_x + dx * .4;
graph.nodes[i].y = graph.nodes[i].drag_y + dy * .4;
}
}
/* if (!overlap) { */
/* graph.nodes[drag_node_index].x = platform.cursor_x + offset_x; */
/* graph.nodes[drag_node_index].y = platform.cursor_y + offset_y; */
/* } */
}
if (platform.key_pressed[KEY_DELETE]) {
remove_node();
remove_edge();
}
for (i64 i = 0; i < MAX_NUM_NODES; ++i) {
if (graph.nodes[i].enabled) {
update_node(i);
if (graph.nodes[i].hover)
hover_node = 1;
}
}
for (i64 i = 0; i < MAX_NUM_EDGES; ++i) {
if (hover_node) {
graph.edges[i].hover = 0;
} else {
update_edge(i);
}
}
if (platform.key_pressed[BUTTON_RIGHT])
for (i64 i = 0; i < MAX_NUM_NODES; ++i) {
if (graph.nodes[i].enabled && graph.nodes[i].hover) {
adding_edge = 1;
adding_src = i;
adding_dst = i;
break;
}
}
if (adding_edge) {
f64 x0 = graph.nodes[adding_src].x;
f64 y0 = graph.nodes[adding_src].y;
f64 x1 = platform.cursor_x;
f64 y1 = platform.cursor_y;
fill_line(OP_SET, 0x7f007f, x0, y0, x1, y1, 30);
}
if (adding_edge)
for (i64 i = 0; i < MAX_NUM_NODES; ++i)
if (graph.nodes[i].enabled && graph.nodes[i].hover) {
adding_dst = i;
break;
}
if (adding_edge && !platform.key_down[BUTTON_RIGHT]) {
adding_edge = 0;
add_edge(adding_src, adding_dst);
}
// Finding shortest path //
if (platform.key_pressed['1']) {
for (i64 i = 0; i < MAX_NUM_NODES; ++i)
if (graph.nodes[i].enabled && graph.nodes[i].hover) {
path_src = i;
path_changed = 1;
break;
}
}
if (platform.key_pressed['2'])
for (i64 i = 0; i < MAX_NUM_NODES; ++i)
if (graph.nodes[i].enabled && graph.nodes[i].hover) {
path_dst = i;
path_changed = 1;
break;
}
if (path_changed) {
highlight_path(&graph, path_src, path_dst);
path_changed = 0; // FIXME: if enabled, highlight isn't showing
}
draw_graph();
p_render_frame();
}
p_cleanup();
{
FILE *n = fopen("coords-write.txt", "wb");
writeInt(n, nodes_count());
for (i64 i = 0; i < MAX_NUM_NODES; ++i) {
if (graph.nodes[i].enabled) {
writeInt(n, (i32)graph.nodes[i].x);
writeInt(n, (i32)graph.nodes[i].y);
}
};
writeInt(n, edges_count());
for (i64 i = 0; i < MAX_NUM_EDGES; ++i) {
if (graph.edges[i].enabled) {
writeInt(n, graph.edges[i].src);
writeInt(n, graph.edges[i].dst);
}
};
fclose(n);
}
return 0;
}