diff options
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 580 |
1 files changed, 580 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..d297e72 --- /dev/null +++ b/src/main.c | |||
| @@ -0,0 +1,580 @@ | |||
| 1 | #define _GNU_SOURCE | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <string.h> | ||
| 4 | #include <stdio.h> | ||
| 5 | #include <stdint.h> | ||
| 6 | #include <assert.h> | ||
| 7 | #include <time.h> | ||
| 8 | #include <math.h> | ||
| 9 | #include <limits.h> | ||
| 10 | #include <raylib.h> | ||
| 11 | #include <raymath.h> | ||
| 12 | #include <gsl/gsl_matrix.h> | ||
| 13 | #include <gsl/gsl_blas.h> | ||
| 14 | #include <gsl/gsl_eigen.h> | ||
| 15 | #include <gsl/gsl_sort_vector.h> | ||
| 16 | |||
| 17 | #include "config.h" | ||
| 18 | #include "utils.h" | ||
| 19 | #include "distribution.h" | ||
| 20 | #include "graph.h" | ||
| 21 | #include "dynamic-array.h" | ||
| 22 | |||
| 23 | #define BUTTONS(X, ...) \ | ||
| 24 | X(__VA_ARGS__, EDIT, "Edit edge" ) \ | ||
| 25 | X(__VA_ARGS__, NEW_POSITIVE, "New graph+" ) \ | ||
| 26 | X(__VA_ARGS__, NEW_POSITIVE_NEGATIVE, "New graph±" ) \ | ||
| 27 | X(__VA_ARGS__, ROUTE_COUNT, "Route count" ) \ | ||
| 28 | X(__VA_ARGS__, SHIMBELL_MIN, "Min Shimbell" ) \ | ||
| 29 | X(__VA_ARGS__, SHIMBELL_MAX, "Max Shimbell" ) \ | ||
| 30 | X(__VA_ARGS__, DIJKSTRA_POSITIVE_MIN, "Min Dijkstra+" ) \ | ||
| 31 | X(__VA_ARGS__, DIJKSTRA_POSITIVE_MAX, "Max Dijkstra+" ) \ | ||
| 32 | X(__VA_ARGS__, FLOYD_WARSHALL_MIN, "Min Floyd-Warshall" ) \ | ||
| 33 | X(__VA_ARGS__, FLOYD_WARSHALL_MAX, "Max Floyd-Warshall" ) \ | ||
| 34 | X(__VA_ARGS__, MAX_FLOW_FORD_FULKERSON, "Max flow Ford-Fulkerson" ) \ | ||
| 35 | X(__VA_ARGS__, MIN_COST_FLOW, "Min cost flow" ) \ | ||
| 36 | X(__VA_ARGS__, COUNT_SPANNING_TREES, "Count spanning trees" ) \ | ||
| 37 | X(__VA_ARGS__, MIN_WEIGHT_SPANNIG_TREE_PRIM, "Min weight spanning tree Prim" ) \ | ||
| 38 | X(__VA_ARGS__, MIN_WEIGHT_SPANNIG_TREE_BORUVKA, "Min weight spanning tree Borůvka") \ | ||
| 39 | X(__VA_ARGS__, ENSURE_EULERIAN, "Ensure Eulerian" ) \ | ||
| 40 | X(__VA_ARGS__, ENSURE_HAMILTONIAN, "Ensure Hamiltonian" ) \ | ||
| 41 | |||
| 42 | #define ENUM_ENTRY(PREFIX, SYMBOL, ...) PREFIX##_##SYMBOL, | ||
| 43 | #define ENUM_ENTRY_VALUE(PREFIX, SYMBOL, VALUE, ...) [PREFIX##_##SYMBOL] = VALUE, | ||
| 44 | |||
| 45 | enum { | ||
| 46 | BUTTONS(ENUM_ENTRY, BUTTON) | ||
| 47 | BUTTON_COUNT | ||
| 48 | } button; | ||
| 49 | #define BUTTON_NONE BUTTON_COUNT | ||
| 50 | |||
| 51 | static const char *button_labels[] = { | ||
| 52 | BUTTONS(ENUM_ENTRY_VALUE, BUTTON) | ||
| 53 | }; | ||
| 54 | |||
| 55 | static Font font; | ||
| 56 | static Rectangle button_recs[BUTTON_COUNT]; | ||
| 57 | static int button_hover = BUTTON_NONE; | ||
| 58 | static bool button_pressed = false; | ||
| 59 | static double start_time; | ||
| 60 | static Vector2 *model_vertices = NULL; | ||
| 61 | static struct distribution dist = {0}; | ||
| 62 | static struct graph g = {0}; | ||
| 63 | static gsl_matrix *saved_adjecency_matrix = NULL; | ||
| 64 | static gsl_matrix *saved_weight_matrix = NULL; | ||
| 65 | static gsl_matrix *saved_capacity_matrix = NULL; | ||
| 66 | |||
| 67 | void | ||
| 68 | new_graph (size_t vertex_count, enum graph_type type) | ||
| 69 | { | ||
| 70 | distribution_init (&dist, DIST_N*vertex_count, DIST_B, DIST_R, DIST_C); | ||
| 71 | generate_graph (&g, &dist, vertex_count, type); | ||
| 72 | print_graph (&g); | ||
| 73 | start_time = GetTime (); | ||
| 74 | |||
| 75 | // Initialize vertex positions in a circle | ||
| 76 | model_vertices = realloc (model_vertices, vertex_count*(sizeof *model_vertices)); | ||
| 77 | for (size_t i = 0; i < vertex_count; ++i) { | ||
| 78 | model_vertices[i].x = cosf (PI + 2*PI*i/(float)vertex_count); | ||
| 79 | model_vertices[i].y = sinf (PI + 2*PI*i/(float)vertex_count); | ||
| 80 | } | ||
| 81 | |||
| 82 | // Print graph in dot format | ||
| 83 | // puts ("digraph G {"); | ||
| 84 | // for (int i = 0; i < vertex_count; i++) { | ||
| 85 | // for (int j = 0; j < vertex_count; j++) { | ||
| 86 | // if (gsl_matrix_get (m, i, j) == 0) | ||
| 87 | // continue; | ||
| 88 | // printf (" %d -> %d [label=\"%g\"]\n", i, j, gsl_matrix_get (m, i, j)); | ||
| 89 | // } | ||
| 90 | // } puts ("}"); | ||
| 91 | } | ||
| 92 | |||
| 93 | void | ||
| 94 | startup (void) | ||
| 95 | { | ||
| 96 | SetConfigFlags (FLAG_MSAA_4X_HINT | | ||
| 97 | FLAG_WINDOW_RESIZABLE | | ||
| 98 | FLAG_VSYNC_HINT | | ||
| 99 | FLAG_WINDOW_UNDECORATED); | ||
| 100 | InitWindow (INITIAL_SCREEN_WIDTH, INITIAL_SCREEN_HEIGHT, WINDOW_TITLE); | ||
| 101 | SetTargetFPS (60); | ||
| 102 | |||
| 103 | const int codepoints_count = 96 + 256 + 2; | ||
| 104 | int codepoints[codepoints_count]; | ||
| 105 | for (int i = 0; i < 95; i++) codepoints[ 0 + i] = 0x0020 + i; | ||
| 106 | for (int i = 0; i < 255; i++) codepoints[96 + i] = 0x0400 + i; | ||
| 107 | codepoints[codepoints_count - 2] = 0x16f; | ||
| 108 | codepoints[codepoints_count - 1] = 0x0b1; | ||
| 109 | |||
| 110 | font = LoadFontEx ("./res/iosevka-term-slab-regular.ttf", FONT_SIZE, codepoints, codepoints_count); | ||
| 111 | SetTextureFilter (font.texture, TEXTURE_FILTER_ANISOTROPIC_16X); | ||
| 112 | |||
| 113 | // srand (time (NULL)); | ||
| 114 | srand (4); | ||
| 115 | |||
| 116 | // Initialize buttons | ||
| 117 | for (int i = 0; i < BUTTON_COUNT; ++i) { | ||
| 118 | Rectangle rec = { | ||
| 119 | .x = PADDING, | ||
| 120 | .y = PADDING + i*(BUTTON_HEIGHT + PADDING), | ||
| 121 | .width = BUTTON_WIDTH, | ||
| 122 | .height = BUTTON_HEIGHT, | ||
| 123 | }; | ||
| 124 | button_recs[i] = rec; | ||
| 125 | } | ||
| 126 | |||
| 127 | start_time = GetTime (); | ||
| 128 | |||
| 129 | new_graph (INITIAL_VERTEX_COUNT, GRAPH_TYPE_POSITIVE); | ||
| 130 | } | ||
| 131 | |||
| 132 | static inline size_t | ||
| 133 | read_vertex (char *label) | ||
| 134 | { | ||
| 135 | size_t v; | ||
| 136 | for (;;) { | ||
| 137 | prompt (label, "%zu", &v); | ||
| 138 | if (v >= g.vertex_count) { | ||
| 139 | printf ("ERROR: Vertex out of range [0; %zu)!\n", g.vertex_count); | ||
| 140 | } else return v; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | static inline size_t | ||
| 145 | read_vertex_count (void) | ||
| 146 | { | ||
| 147 | size_t vertex_count; | ||
| 148 | for (;;) { | ||
| 149 | prompt ("VERTEX_COUNT", "%zu", &vertex_count); | ||
| 150 | if (vertex_count > MAX_VERTEX_COUNT) { | ||
| 151 | printf ("ERROR: Vertex count cannot be larger than %zu!\n", MAX_VERTEX_COUNT); | ||
| 152 | } else return vertex_count; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | static inline void | ||
| 157 | handle_buttons (void) | ||
| 158 | { | ||
| 159 | button_hover = BUTTON_NONE; | ||
| 160 | button_pressed = false; | ||
| 161 | for (int i = 0; i < BUTTON_COUNT; ++i) { | ||
| 162 | if (CheckCollisionPointRec (GetMousePosition (), button_recs[i])) { | ||
| 163 | button_hover = i; | ||
| 164 | if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT)) | ||
| 165 | button_pressed = true; | ||
| 166 | break; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | if (button_pressed) { | ||
| 170 | if (button_hover != BUTTON_NONE) { | ||
| 171 | graph_unhighlight (&g); | ||
| 172 | if (saved_adjecency_matrix) { | ||
| 173 | gsl_matrix_free (g.adjecency_matrix); | ||
| 174 | g.adjecency_matrix = saved_adjecency_matrix; | ||
| 175 | saved_adjecency_matrix = NULL; | ||
| 176 | start_time = GetTime (); | ||
| 177 | } | ||
| 178 | if (saved_weight_matrix) { | ||
| 179 | gsl_matrix_free (g.weight_matrix); | ||
| 180 | g.weight_matrix = saved_weight_matrix; | ||
| 181 | saved_weight_matrix = NULL; | ||
| 182 | } | ||
| 183 | if (saved_capacity_matrix) { | ||
| 184 | gsl_matrix_free (g.capacity_matrix); | ||
| 185 | g.capacity_matrix = saved_capacity_matrix; | ||
| 186 | saved_capacity_matrix = NULL; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | switch (button_hover) { | ||
| 190 | case BUTTON_EDIT: { | ||
| 191 | size_t from = read_vertex ("FROM"); | ||
| 192 | size_t to = read_vertex ("TO"); | ||
| 193 | if (to == from) { | ||
| 194 | printf("ERROR: Self-loops are not allowed!\n"); | ||
| 195 | break; | ||
| 196 | } | ||
| 197 | |||
| 198 | double weight; | ||
| 199 | for (;;) { | ||
| 200 | prompt ("WEIGHT", "%lf", &weight); | ||
| 201 | if (weight > MAX_WEIGHT) { | ||
| 202 | printf ("ERROR: Weight cannot be larger than %lf!\n", MAX_WEIGHT); | ||
| 203 | } else if (g.type == GRAPH_TYPE_POSITIVE && weight < 0) { | ||
| 204 | printf ("ERROR: Weight cannot be negative in a positive graph!\n"); | ||
| 205 | } else if (g.type == GRAPH_TYPE_POSITIVE_NEGATIVE && weight < -MAX_WEIGHT) { | ||
| 206 | printf ("ERROR: Weight cannot be less than %lf!\n", -MAX_WEIGHT); | ||
| 207 | } else break; | ||
| 208 | } | ||
| 209 | |||
| 210 | double capacity; | ||
| 211 | for (;;) { | ||
| 212 | prompt ("CAPACITY", "%lf", &capacity); | ||
| 213 | if (weight > MAX_WEIGHT) { | ||
| 214 | printf ("ERROR: Capacity cannot be larger than %lf!\n", MAX_CAPACITY); | ||
| 215 | } else break; | ||
| 216 | } | ||
| 217 | |||
| 218 | graph_unhighlight (&g); | ||
| 219 | graph_edge_set (&g, from, to, weight, capacity); | ||
| 220 | print_graph (&g); | ||
| 221 | } break; | ||
| 222 | |||
| 223 | case BUTTON_NEW_POSITIVE: { | ||
| 224 | new_graph (read_vertex_count (), GRAPH_TYPE_POSITIVE); | ||
| 225 | } break; | ||
| 226 | |||
| 227 | case BUTTON_NEW_POSITIVE_NEGATIVE: { | ||
| 228 | new_graph (read_vertex_count (), GRAPH_TYPE_POSITIVE_NEGATIVE); | ||
| 229 | } break; | ||
| 230 | |||
| 231 | case BUTTON_ROUTE_COUNT: { | ||
| 232 | size_t from = read_vertex ("FROM"); | ||
| 233 | size_t to = read_vertex ("TO"); | ||
| 234 | size_t routes = graph_count_routes (&g, from, to); | ||
| 235 | if (routes) printf ("ROUTE COUNT FROM %zu TO %zu: %zu\n", from, to, routes); | ||
| 236 | else printf ("NO ROUTES FROM %zu TO %zu\n", from, to); | ||
| 237 | } break; | ||
| 238 | |||
| 239 | case BUTTON_SHIMBELL_MIN: { | ||
| 240 | int edge_count = 0; | ||
| 241 | do { | ||
| 242 | printf ("SHIMBELL METHOD EDGE COUNT: "); | ||
| 243 | scanf ("%d", &edge_count); | ||
| 244 | while (getchar () != '\n'); | ||
| 245 | } while (!edge_count); | ||
| 246 | gsl_matrix *sh = shimbell_method (&g, edge_count, false); | ||
| 247 | puts ("PATH MATRIX:"); | ||
| 248 | print_gsl_matrix (sh); | ||
| 249 | gsl_matrix_free (sh); | ||
| 250 | } break; | ||
| 251 | |||
| 252 | case BUTTON_SHIMBELL_MAX: { | ||
| 253 | int edge_count = 0; | ||
| 254 | do { | ||
| 255 | printf ("SHIMBELL METHOD EDGE COUNT: "); | ||
| 256 | scanf ("%d", &edge_count); | ||
| 257 | while (getchar () != '\n'); | ||
| 258 | } while (!edge_count); | ||
| 259 | gsl_matrix *sh = shimbell_method (&g, edge_count, true); | ||
| 260 | puts ("PATH MATRIX:"); | ||
| 261 | print_gsl_matrix (sh); | ||
| 262 | gsl_matrix_free (sh); | ||
| 263 | } break; | ||
| 264 | |||
| 265 | case BUTTON_DIJKSTRA_POSITIVE_MIN: { | ||
| 266 | size_t from = read_vertex ("FROM"); | ||
| 267 | size_t to = read_vertex ("TO"); | ||
| 268 | g.highlighted_path = dijkstra_positive (&g, from, to, false); | ||
| 269 | } break; | ||
| 270 | |||
| 271 | case BUTTON_DIJKSTRA_POSITIVE_MAX: { | ||
| 272 | size_t from = read_vertex ("FROM"); | ||
| 273 | size_t to = read_vertex ("TO"); | ||
| 274 | g.highlighted_path = dijkstra_positive (&g, from, to, true); | ||
| 275 | } break; | ||
| 276 | |||
| 277 | case BUTTON_FLOYD_WARSHALL_MIN: { | ||
| 278 | size_t from = read_vertex ("FROM"); | ||
| 279 | size_t to = read_vertex ("TO"); | ||
| 280 | g.highlighted_path = floyd_warshall (&g, from, to, false); | ||
| 281 | } break; | ||
| 282 | |||
| 283 | case BUTTON_FLOYD_WARSHALL_MAX: { | ||
| 284 | size_t from = read_vertex ("FROM"); | ||
| 285 | size_t to = read_vertex ("TO"); | ||
| 286 | g.highlighted_path = floyd_warshall (&g, from, to, true); | ||
| 287 | } break; | ||
| 288 | |||
| 289 | case BUTTON_MAX_FLOW_FORD_FULKERSON: { | ||
| 290 | size_t from = read_vertex ("FROM"); | ||
| 291 | size_t to = read_vertex ("TO"); | ||
| 292 | double max_flow = max_flow_ford_fulkerson (&g, from, to); | ||
| 293 | printf ("MAX_FLOW: %.0lf\n", max_flow); | ||
| 294 | } break; | ||
| 295 | |||
| 296 | case BUTTON_MIN_COST_FLOW: { | ||
| 297 | size_t from = read_vertex ("FROM"); | ||
| 298 | size_t to = read_vertex ("TO"); | ||
| 299 | double max_flow = max_flow_ford_fulkerson(&g, from, to); | ||
| 300 | if (max_flow == 0) { | ||
| 301 | printf ("NO PATH FROM %zu TO %zu\n", from, to); | ||
| 302 | break; | ||
| 303 | } | ||
| 304 | double required_flow = floor (max_flow*2/3); | ||
| 305 | printf ("REQUIRED_FLOW: [%.0lf*2/3] = %.0lf\n", max_flow, required_flow); | ||
| 306 | |||
| 307 | double min_cost = min_cost_flow (&g, from, to, required_flow); | ||
| 308 | printf ("MIN_COST: %.0lf\n", min_cost); | ||
| 309 | } break; | ||
| 310 | |||
| 311 | case BUTTON_COUNT_SPANNING_TREES: { | ||
| 312 | double spannig_tree_count = count_spanning_trees (&g); | ||
| 313 | printf ("SPANNING TREE COUNT %.0lf\n", spannig_tree_count); | ||
| 314 | } break; | ||
| 315 | |||
| 316 | case BUTTON_MIN_WEIGHT_SPANNIG_TREE_PRIM: { | ||
| 317 | gsl_matrix *minimal_spanning_tree = min_weight_spannig_tree_prim (&g); | ||
| 318 | prufer_code (minimal_spanning_tree); | ||
| 319 | highlight_graph (&g, minimal_spanning_tree); | ||
| 320 | gsl_matrix_free (minimal_spanning_tree); | ||
| 321 | } break; | ||
| 322 | |||
| 323 | case BUTTON_MIN_WEIGHT_SPANNIG_TREE_BORUVKA: { | ||
| 324 | gsl_matrix *minimal_spanning_tree = min_weight_spannig_tree_boruvka (&g); | ||
| 325 | prufer_code (minimal_spanning_tree); | ||
| 326 | highlight_graph (&g, minimal_spanning_tree); | ||
| 327 | gsl_matrix_free (minimal_spanning_tree); | ||
| 328 | } break; | ||
| 329 | |||
| 330 | case BUTTON_ENSURE_EULERIAN: { | ||
| 331 | saved_adjecency_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count); | ||
| 332 | gsl_matrix_memcpy (saved_adjecency_matrix, g.adjecency_matrix); | ||
| 333 | saved_weight_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count); | ||
| 334 | gsl_matrix_memcpy (saved_weight_matrix, g.weight_matrix); | ||
| 335 | saved_capacity_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count); | ||
| 336 | gsl_matrix_memcpy (saved_capacity_matrix, g.capacity_matrix); | ||
| 337 | graph_ensure_eulerian (&g); | ||
| 338 | start_time = GetTime (); | ||
| 339 | } break; | ||
| 340 | |||
| 341 | case BUTTON_ENSURE_HAMILTONIAN: { | ||
| 342 | saved_adjecency_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count); | ||
| 343 | gsl_matrix_memcpy (saved_adjecency_matrix, g.adjecency_matrix); | ||
| 344 | saved_weight_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count); | ||
| 345 | gsl_matrix_memcpy (saved_weight_matrix, g.weight_matrix); | ||
| 346 | saved_capacity_matrix = gsl_matrix_alloc (g.vertex_count, g.vertex_count); | ||
| 347 | gsl_matrix_memcpy (saved_capacity_matrix, g.capacity_matrix); | ||
| 348 | graph_ensure_hamiltonian (&g); | ||
| 349 | start_time = GetTime (); | ||
| 350 | } break; | ||
| 351 | default: break; | ||
| 352 | } | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | static inline void | ||
| 357 | draw_buttons (void) | ||
| 358 | { | ||
| 359 | for (int i = 0; i < BUTTON_COUNT; i++) { | ||
| 360 | DrawRectangleRec (button_recs[i], i == button_hover ? BUTTON_HOVER_COLOR | ||
| 361 | : BUTTON_COLOR); | ||
| 362 | DrawRectangleLinesEx (button_recs[i], 2, i == button_hover ? BUTTON_HOVER_OUTLINE_COLOR | ||
| 363 | : BUTTON_OUTLINE_COLOR); | ||
| 364 | Vector2 text_center = Vector2Scale ( | ||
| 365 | MeasureTextEx (font, button_labels[i], | ||
| 366 | FONT_SIZE, TEXT_SPACING), | ||
| 367 | 0.5); | ||
| 368 | Vector2 rect_center = { | ||
| 369 | .x = button_recs[i].x + button_recs[i].width/2, | ||
| 370 | .y = button_recs[i].y + button_recs[i].height/2 + 1, | ||
| 371 | }; | ||
| 372 | DrawTextEx (font, button_labels[i], Vector2Subtract (rect_center, | ||
| 373 | text_center), | ||
| 374 | FONT_SIZE, TEXT_SPACING, i == button_hover ? BUTTON_TEXT_HOVER_COLOR | ||
| 375 | : BUTTON_TEXT_COLOR); | ||
| 376 | } | ||
| 377 | } | ||
| 378 | |||
| 379 | static inline void | ||
| 380 | progress_model (void) | ||
| 381 | { | ||
| 382 | size_t n = g.vertex_count; | ||
| 383 | Vector2 force_on_vertices[MAX_VERTEX_COUNT] = {0}; | ||
| 384 | for (size_t i = 0; i < n; ++i) { | ||
| 385 | for (size_t j = 0; j < n; ++j) { | ||
| 386 | if (i == j) | ||
| 387 | continue; | ||
| 388 | Vector2 dir = Vector2Normalize (Vector2Subtract (model_vertices[j], | ||
| 389 | model_vertices[i])); | ||
| 390 | if (gsl_matrix_get (g.adjecency_matrix, i, j) || | ||
| 391 | gsl_matrix_get (g.adjecency_matrix, j, i)) { | ||
| 392 | float distance = Vector2Distance (model_vertices[i], | ||
| 393 | model_vertices[j]); | ||
| 394 | float mag = MODEL_C_SPRING*logf (distance/MODEL_L); | ||
| 395 | force_on_vertices[i] = Vector2Add (force_on_vertices[i], | ||
| 396 | Vector2Scale (dir, mag)); | ||
| 397 | } else { | ||
| 398 | float square_distance = Vector2DistanceSqr (model_vertices[j], | ||
| 399 | model_vertices[i]); | ||
| 400 | float mag = MODEL_C_REP/square_distance; | ||
| 401 | force_on_vertices[i] = Vector2Add (force_on_vertices[i], | ||
| 402 | Vector2Scale (dir, -mag)); | ||
| 403 | } | ||
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | float heat = exp ((-1 + MODEL_COOLING_FACTOR)*(GetTime () - start_time))/100; | ||
| 408 | for (int i = 0; i < n; ++i) { | ||
| 409 | model_vertices[i] = Vector2Add (model_vertices[i], | ||
| 410 | Vector2Scale (force_on_vertices[i], heat)); | ||
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | void | ||
| 415 | draw_edges (Vector2 *vertex_screen_positions) | ||
| 416 | { | ||
| 417 | size_t n = g.vertex_count; | ||
| 418 | for (size_t i = 0; i < n; ++i) { | ||
| 419 | for (size_t j = 0; j < n; ++j) { | ||
| 420 | if (!gsl_matrix_get (g.adjecency_matrix, i, j)) continue; | ||
| 421 | double capacity = gsl_matrix_get ( | ||
| 422 | g.capacity_matrix, i, j); | ||
| 423 | float line_thickness = Remap (capacity, 0, MAX_CAPACITY, 4, 8); | ||
| 424 | float arrow_side = 3*line_thickness; | ||
| 425 | float arrow_height = arrow_side*sqrt (3)/2; | ||
| 426 | |||
| 427 | Vector2 dir = Vector2Normalize (Vector2Subtract ( | ||
| 428 | vertex_screen_positions[j], | ||
| 429 | vertex_screen_positions[i])); | ||
| 430 | |||
| 431 | Vector2 line_offset = Vector2Scale (dir, 1.5*VERTEX_RADIUS); | ||
| 432 | Vector2 line_begin = Vector2Add (vertex_screen_positions[i], | ||
| 433 | line_offset); | ||
| 434 | Vector2 line_end = Vector2Subtract ( | ||
| 435 | Vector2Subtract (vertex_screen_positions[j], line_offset), | ||
| 436 | Vector2Scale (dir, arrow_height)); | ||
| 437 | |||
| 438 | double weight = gsl_matrix_get (g.weight_matrix, i, j); | ||
| 439 | float brightness = Remap (Normalize (fabs (weight), 0, MAX_WEIGHT), | ||
| 440 | 0, 1, 0.25, 0.75); | ||
| 441 | Color line_color = ColorBrightness (BLACK, 1 - brightness); | ||
| 442 | if (g.type == GRAPH_TYPE_POSITIVE_NEGATIVE) { | ||
| 443 | if (weight < 0) { | ||
| 444 | line_color.b = 255/6; | ||
| 445 | line_color.g = 255/6; | ||
| 446 | } else { | ||
| 447 | line_color.r = 255/6; | ||
| 448 | line_color.b = 255/6; | ||
| 449 | } | ||
| 450 | } | ||
| 451 | if (g.highlighted_path) { | ||
| 452 | for (size_t v = 0; g.highlighted_path[v] != n; ++v) { | ||
| 453 | if (g.highlighted_path[v + 0] == i && | ||
| 454 | g.highlighted_path[v + 1] == j || | ||
| 455 | g.highlighted_path[v + 0] == j && | ||
| 456 | g.highlighted_path[v + 1] == i) { | ||
| 457 | if (g.type == GRAPH_TYPE_POSITIVE_NEGATIVE) { | ||
| 458 | line_color = weight < 0 ? (Color) { 0xff, 0x00, 0x00, 0xff } | ||
| 459 | : (Color) { 0x00, 0xff, 0x00, 0xff }; | ||
| 460 | } else { | ||
| 461 | line_color = VERTEX_OUTLINE_HIGHLIGHTED_COLOR; | ||
| 462 | } break; | ||
| 463 | } | ||
| 464 | } | ||
| 465 | } else if (g.highlighted_edges.count) { | ||
| 466 | for (size_t v = 0; v < g.highlighted_edges.count; ++v) { | ||
| 467 | if (g.highlighted_edges.items[v].from == i && | ||
| 468 | g.highlighted_edges.items[v].to == j) { | ||
| 469 | if (g.type == GRAPH_TYPE_POSITIVE_NEGATIVE) { | ||
| 470 | line_color = weight < 0 ? (Color) { 0xff, 0x00, 0x00, 0xff } | ||
| 471 | : (Color) { 0x00, 0xff, 0x00, 0xff }; | ||
| 472 | } else { | ||
| 473 | line_color = VERTEX_OUTLINE_HIGHLIGHTED_COLOR; | ||
| 474 | } break; | ||
| 475 | } | ||
| 476 | } | ||
| 477 | } | ||
| 478 | |||
| 479 | const Vector2 side = Vector2Scale (dir, -arrow_side/2); | ||
| 480 | DrawLineEx (line_begin, line_end, line_thickness, line_color); | ||
| 481 | DrawTriangle ( | ||
| 482 | Vector2Add (line_end, Vector2Scale (dir, arrow_height)), | ||
| 483 | Vector2Add (line_end, Vector2Rotate (side, PI/2)), | ||
| 484 | Vector2Add (line_end, Vector2Rotate (side, -PI/2)), | ||
| 485 | line_color); | ||
| 486 | |||
| 487 | char *anot; | ||
| 488 | asprintf (&anot, "%.0lf/%.0lf", capacity, weight); | ||
| 489 | Vector2 center = Vector2Scale ( | ||
| 490 | MeasureTextEx (font, anot, FONT_SIZE, TEXT_SPACING), 0.5); | ||
| 491 | Vector2 midpoint = Vector2Lerp(line_begin, line_end, 0.5); | ||
| 492 | Rectangle bounding_box = { | ||
| 493 | .x = midpoint.x - center.x - 4, | ||
| 494 | .y = midpoint.y - center.y, | ||
| 495 | .width = 2*(center.x + 4), | ||
| 496 | .height = 2*center.y, | ||
| 497 | }; | ||
| 498 | DrawRectangleRounded(bounding_box, 0.5, 16, ColorAlpha (BACKGROUND_COLOR, 0.75)); | ||
| 499 | DrawTextEx (font, anot, | ||
| 500 | Vector2Subtract (midpoint, center), | ||
| 501 | FONT_SIZE, TEXT_SPACING, EDGE_TEXT_COLOR); | ||
| 502 | free (anot); | ||
| 503 | } | ||
| 504 | } | ||
| 505 | } | ||
| 506 | |||
| 507 | void | ||
| 508 | draw_vertices (Vector2 *vertex_screen_positions) | ||
| 509 | { | ||
| 510 | for (size_t i = 0; i < g.vertex_count; ++i) { | ||
| 511 | Color outline_color = VERTEX_OUTLINE_COLOR; | ||
| 512 | |||
| 513 | if (g.highlighted_path) { | ||
| 514 | for (size_t v = 0; g.highlighted_path[v] != g.vertex_count; ++v) { | ||
| 515 | if (g.highlighted_path[v] == i) | ||
| 516 | outline_color = VERTEX_OUTLINE_HIGHLIGHTED_COLOR; | ||
| 517 | } | ||
| 518 | } else if (g.highlighted_edges.count) { | ||
| 519 | for (size_t v = 0; v < g.highlighted_edges.count; ++v) { | ||
| 520 | if (g.highlighted_edges.items[v].from == i || | ||
| 521 | g.highlighted_edges.items[v].to == i) { | ||
| 522 | outline_color = VERTEX_OUTLINE_HIGHLIGHTED_COLOR; | ||
| 523 | } | ||
| 524 | } | ||
| 525 | } | ||
| 526 | |||
| 527 | DrawCircleV (vertex_screen_positions[i], 1.2*VERTEX_RADIUS, outline_color); | ||
| 528 | DrawCircleV (vertex_screen_positions[i], VERTEX_RADIUS, VERTEX_COLOR); | ||
| 529 | char *anot; | ||
| 530 | asprintf (&anot, "%zu", i); | ||
| 531 | Vector2 center = Vector2Scale ( | ||
| 532 | MeasureTextEx (font, anot, FONT_SIZE, TEXT_SPACING), | ||
| 533 | 0.5); | ||
| 534 | DrawTextEx (font, anot, | ||
| 535 | Vector2Subtract (vertex_screen_positions[i], center), | ||
| 536 | FONT_SIZE, TEXT_SPACING, VERTEX_TEXT_COLOR); | ||
| 537 | free (anot); | ||
| 538 | } | ||
| 539 | } | ||
| 540 | |||
| 541 | int | ||
| 542 | main (void) | ||
| 543 | { | ||
| 544 | startup (); | ||
| 545 | while (!WindowShouldClose ()) { | ||
| 546 | if (IsKeyDown (KEY_SPACE)) | ||
| 547 | start_time = GetTime (); | ||
| 548 | handle_buttons (); | ||
| 549 | progress_model (); | ||
| 550 | |||
| 551 | // Find model space position supremums | ||
| 552 | Vector2 pos_max = { -INFINITY, -INFINITY }; | ||
| 553 | Vector2 pos_min = { INFINITY, INFINITY }; | ||
| 554 | for (size_t i = 0; i < g.vertex_count; ++i) { | ||
| 555 | if (model_vertices[i].x > pos_max.x) pos_max.x = model_vertices[i].x; | ||
| 556 | if (model_vertices[i].y > pos_max.y) pos_max.y = model_vertices[i].y; | ||
| 557 | if (model_vertices[i].x < pos_min.x) pos_min.x = model_vertices[i].x; | ||
| 558 | if (model_vertices[i].y < pos_min.y) pos_min.y = model_vertices[i].y; | ||
| 559 | } | ||
| 560 | |||
| 561 | // Remap model space positions to screen space | ||
| 562 | Vector2 vertex_screen_positions[g.vertex_count]; | ||
| 563 | for (size_t i = 0; i < g.vertex_count; ++i) { | ||
| 564 | vertex_screen_positions[i] = (Vector2) { | ||
| 565 | .x = Remap (model_vertices[i].x, pos_min.x, pos_max.x, 2*VERTEX_RADIUS + BUTTON_WIDTH + PADDING, GetScreenWidth () - 2*VERTEX_RADIUS), | ||
| 566 | .y = Remap (model_vertices[i].y, pos_min.y, pos_max.y, 2*VERTEX_RADIUS, GetScreenHeight () - 2*VERTEX_RADIUS), | ||
| 567 | }; | ||
| 568 | } | ||
| 569 | |||
| 570 | BeginDrawing (); | ||
| 571 | ClearBackground (BACKGROUND_COLOR); | ||
| 572 | draw_buttons (); | ||
| 573 | draw_edges (vertex_screen_positions); | ||
| 574 | draw_vertices (vertex_screen_positions); | ||
| 575 | // DrawFPS (2, 0); | ||
| 576 | EndDrawing (); | ||
| 577 | } CloseWindow (); | ||
| 578 | |||
| 579 | return EXIT_SUCCESS; | ||
| 580 | } | ||
