From 8270ca448dd86517468872d9fc26f0f5d402864a Mon Sep 17 00:00:00 2001 From: kartofen Date: Wed, 12 Apr 2023 23:33:50 +0300 Subject: cirlce-thingy done --- circle-thingy/build.sh | 22 +++++++++ circle-thingy/main.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++ circle-thingy/shell.html | 82 +++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100755 circle-thingy/build.sh create mode 100644 circle-thingy/main.c create mode 100644 circle-thingy/shell.html (limited to 'circle-thingy') diff --git a/circle-thingy/build.sh b/circle-thingy/build.sh new file mode 100755 index 0000000..47c4d75 --- /dev/null +++ b/circle-thingy/build.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +FLAGS="-Wall -Wextra -g -pedantic" +RAYLIB_FLAGS="-lraylib -lm -lpthread -lGL -ldl -lrt -lX11" +RAYLIB_SRC="/usr/local/src/raylib/src" + +mkdir -p bin +set -xe + +if ! [[ $1 = "web" ]]; then + gcc -o bin/main main.c $DEBUG_FLAGS $RAYLIB_FLAGS +else + source emsdk_env.sh + + emcc -o bin/circle-thingy.html *.c \ + -O2 -DPLATFORM_WEB \ + -DRAYLIB_SRC="\"$RAYLIB_SRC/raylib.h\"" \ + $RAYLIB_SRC/libraylib.a \ + -I$RAYLIB_SRC/raylib.h \ + -s USE_GLFW=3 -s ASYNCIFY \ + --shell-file shell.html +fi diff --git a/circle-thingy/main.c b/circle-thingy/main.c new file mode 100644 index 0000000..38ba0fb --- /dev/null +++ b/circle-thingy/main.c @@ -0,0 +1,124 @@ +#include +#include + +#ifdef PLATFORM_WEB + #include RAYLIB_SRC +#else + #include +#endif + +// game coords to screen coords +#define GTOS(n) ((n) * (SH) / 2) +#define GTOSV(n) ((Vector2){(GTOS(n.x)), (GTOS(n.y))}) +// screen coords to game coords +#define STOG(n) ((n) / (SH) * 2) +#define STOGV(n) ((Vector2){(STOG(n.x)), (STOG(n.y))}) + +#define IN_RANGE(x, y, z) (((x) > ((y) - (z))) && ((x) < ((y) + (z)))) +#define IN_RANGEV(a, b, c) ((IN_RANGE(a.x, b.x, c)) && (IN_RANGE(a.y, b.y, c))) + +int SW = 1200; +int SH = 800; +int MAX_ITER = 50; + +Vector2 a = {-1, 0}; +Vector2 b = { 1, 0}; + +char text[1024]; + +Vector2 median(Vector2 p1, Vector2 p2) +{ + return (Vector2){(p1.x + p2.x)/2, (p1.y + p2.y)/2}; +} + +void calc_points(Vector2 p1, Vector2 p2, Vector2 q) +{ + static int iter = 0; + if(iter++ >= MAX_ITER) { + sprintf(text, "iter: %d; x: %f; y: %f", iter-1, p1.x, -p1.y); + iter = 0; + return; + } + + DrawLineV(GTOSV(p1), GTOSV(p2), (iter % 2 == 0) ? RED : BLUE); + + calc_points(q, median(p1, p2), p2); +} + +int main(void) +{ + InitWindow(SW, SH, "circle thingy"); + SetTargetFPS(60); + SetWindowState(FLAG_WINDOW_RESIZABLE); + + printf("\n----------------------- Instructions -----------------------\n"); + printf("Use WASD to move around\n"); + printf("Use Q and E increase or decrease the iterations\n"); + printf("Use the mouse to move the points and to zoom in or out\n"); + printf("------------------------------------------------------------\n"); + + Camera2D camera = {0}; + camera.zoom = 1.0f; + + while (!WindowShouldClose()) + { + // for resizing + SW = GetScreenWidth(); + SH = GetScreenHeight(); + camera.offset = (Vector2){SW/2.0f, SH/2.0f}; + + if(IsKeyDown(KEY_W)) camera.target.y -= 500.0f * GetFrameTime(); + if(IsKeyDown(KEY_A)) camera.target.x -= 500.0f * GetFrameTime(); + if(IsKeyDown(KEY_S)) camera.target.y += 500.0f * GetFrameTime(); + if(IsKeyDown(KEY_D)) camera.target.x += 500.0f * GetFrameTime(); + if(IsKeyPressed(KEY_Q)) MAX_ITER++; + if(IsKeyPressed(KEY_E)) MAX_ITER--; + camera.zoom += ((float)GetMouseWheelMove()*0.05f); + + static bool a_moving = false; + static bool b_moving = false; + + BeginDrawing(); + ClearBackground(RAYWHITE); + + BeginMode2D(camera); + DrawCircleLines(0, 0, GTOS(1), BLACK); + DrawLine(GTOS(0), GTOS(1), GTOS(0), GTOS(-1), BLACK); + DrawLine(GTOS(1), GTOS(0), GTOS(-1), GTOS(0), BLACK); + + DrawCircleV(GTOSV(a), 10, BLUE); + DrawCircleV(GTOSV(b), 10, RED); + + // moving the point a + if(IN_RANGEV(GetScreenToWorld2D(GetMousePosition(), camera), GTOSV(a), 10) + || a_moving) { + DrawCircleLines(GTOS(a.x), GTOS(a.y), 13, BLUE); + + if(IsMouseButtonDown(MOUSE_BUTTON_LEFT) && !b_moving) { + a = STOGV(GetScreenToWorld2D(GetMousePosition(), camera)); + a_moving = true; + } else a_moving = false; + } + + // moving the point b + if(IN_RANGEV(GetScreenToWorld2D(GetMousePosition(), camera), GTOSV(b), 10) + || b_moving) { + DrawCircleLines(GTOS(b.x), GTOS(b.y), 13, RED); + + if(IsMouseButtonDown(MOUSE_BUTTON_LEFT) && !a_moving) { + b = STOGV(GetScreenToWorld2D(GetMousePosition(), camera)); + b_moving = true; + } else b_moving = false; + } + + calc_points((Vector2){0, -1}, a, b); + EndMode2D(); + + DrawFPS(10, 10); + DrawText(text, 100, 10, 30, DARKGREEN); + EndDrawing(); + } + + CloseWindow(); + return 0; +} diff --git a/circle-thingy/shell.html b/circle-thingy/shell.html new file mode 100644 index 0000000..2188465 --- /dev/null +++ b/circle-thingy/shell.html @@ -0,0 +1,82 @@ + + + + + + Pendulum + + + +
Downloading...
+ +
+ +
+ + + + + {{{ SCRIPT }}} + + -- cgit v1.2.3