#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; }