From 87818848122ac94995e359a8db3a0ed9a9173d42 Mon Sep 17 00:00:00 2001 From: kartofen Date: Sat, 29 Oct 2022 13:18:45 +0300 Subject: implemented --- src/main.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/set.c | 40 ++++++++++++++++++++++++++ src/set.h | 14 +++++++++ src/shell.html | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 226 insertions(+) create mode 100644 src/main.c create mode 100644 src/set.c create mode 100644 src/set.h create mode 100644 src/shell.html (limited to 'src') diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..90de051 --- /dev/null +++ b/src/main.c @@ -0,0 +1,90 @@ +#include +#include +#include +#include + +#include "set.h" + +#ifndef PLATFORM_WEB + #include +#else + #include RAYLIB_SRC +#endif + +#define W 800 +#define H 600 + +#define NODES_X 20 +#define NODES_Y 15 +#define NODE_L 40 + +set *nodes[NODES_X][NODES_Y]; +bool walls_hori[NODES_X][NODES_Y-1] = {0}; // false - exists +bool walls_vert[NODES_X-1][NODES_Y] = {0}; // true - doesnt exist + +void init_nodes() +{ + for(int i = 0; i < NODES_Y; i++) + for(int j = 0; j < NODES_X; j++) + nodes[j][i] = set_create(); +} + +void deinit_nodes() +{ + for(int i = 0; i < NODES_Y; i++) + for(int j = 0; j < NODES_X; j++) + set_free(nodes[j][i]); +} + +void update() +{ + // optimize picking random number thingy + if(rand() % 2) { + int x = rand() % NODES_X; + int y = rand() % (NODES_Y-1); + + if(set_find(nodes[x][y]) != set_find(nodes[x][y+1])) { + walls_hori[x][y] = true; + set_union(nodes[x][y], nodes[x][y+1]); + } + } else { + int x = rand() % (NODES_X-1); + int y = rand() % NODES_Y; + + if(set_find(nodes[x][y]) != set_find(nodes[x+1][y])) { + walls_vert[x][y] = true; + set_union(nodes[x][y], nodes[x+1][y]); + } + } + + for(int i = 0; i < NODES_Y; i++) { + for(int j = 0; j < NODES_X; j++) { + if(i < NODES_Y-1) if(!walls_hori[j][i]) + DrawLine((j+0)*NODE_L, (i+1)*NODE_L, (j+1)*NODE_L, (i+1)*NODE_L, RED); + + if(j < NODES_X-1) if(!walls_vert[j][i]) + DrawLine((j+1)*NODE_L, (i+0)*NODE_L, (j+1)*NODE_L, (i+1)*NODE_L, RED); + + } + } +} + +int main(void) +{ + srand(time(NULL)); + InitWindow(W, H, "Kruskal"); + SetTargetFPS(60); + init_nodes(); + + while(!WindowShouldClose()) + { + BeginDrawing(); + ClearBackground(RAYWHITE); + DrawFPS(10, 10); + update(); + EndDrawing(); + } + + deinit_nodes(); + return 0; +} diff --git a/src/set.c b/src/set.c new file mode 100644 index 0000000..b84ae6a --- /dev/null +++ b/src/set.c @@ -0,0 +1,40 @@ +#include + +#include "set.h" + +set *set_create() +{ + set *s = malloc(sizeof(set)); + s->parent = s; + s->rank = 0; + return s; +} + +void set_free(set *s) +{ + free(s); +} + +set *set_find(set *s) +{ + if(s->parent == s) + return s; + s->parent = set_find(s->parent); + return s->parent; +} + +void set_union(set *x, set *y) +{ + x = set_find(x); + y = set_find(y); + + if(x == y) return; + + if(x->rank > y->rank) { + y->parent = x; + } else { + x->parent = y; + } + + if(x->rank == y->rank) x->rank++; +} diff --git a/src/set.h b/src/set.h new file mode 100644 index 0000000..b92d229 --- /dev/null +++ b/src/set.h @@ -0,0 +1,14 @@ +#ifndef SET_H +#define SET_H + +typedef struct set { + struct set *parent; + int rank; +} set; + +set *set_create(); +void set_free(set *s); + +set *set_find(set *s); +void set_union(set *x, set *y); +#endif diff --git a/src/shell.html b/src/shell.html new file mode 100644 index 0000000..3abc1b0 --- /dev/null +++ b/src/shell.html @@ -0,0 +1,82 @@ + + + + + + Pendulum + + + +
Downloading...
+ +
+ +
+ + + + + {{{ SCRIPT }}} + + -- cgit v1.2.3