diff options
author | kartofen <mladenovnasko0@gmail.com> | 2023-04-12 23:33:50 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2023-04-12 23:33:50 +0300 |
commit | 8270ca448dd86517468872d9fc26f0f5d402864a (patch) | |
tree | d55590d4e1812a0d31e2fdb69668b78352eab950 |
-rw-r--r-- | .gitignore | 1 | ||||
-rwxr-xr-x | circle-thingy/build.sh | 22 | ||||
-rw-r--r-- | circle-thingy/main.c | 124 | ||||
-rw-r--r-- | circle-thingy/shell.html | 82 | ||||
-rw-r--r-- | main.py | 15 |
5 files changed, 244 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e660fd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/ 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 <stdio.h> +#include <stdbool.h> + +#ifdef PLATFORM_WEB + #include RAYLIB_SRC +#else + #include <raylib.h> +#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 @@ +<!DOCTYPE html> +<html lang="en-us"> + <head> + <meta charset="utf-8"> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> + <title>Pendulum</title> + <style> + .el { margin: 5px; } + div.el { float: left; } + textarea.el { font-family: monospace; width: 100%; } + canvas.el { border: 0px none; background-color: black; } + </style> + </head> + <body> + <div class="el" id="status">Downloading...</div> + + <div class="el"> + <canvas id="canvas" oncontextmenu="event.preventDefault()" tabindex=-1></canvas> + </div> + + <textarea class="el" id="output" rows="12"></textarea> + + <script type='text/javascript'> + var statusElement = document.getElementById('status'); + + var Module = { + attachGLFWEventsToCanvas: true, + preRun: [], + postRun: [], + arguments: [], + print: (function() { + var element = document.getElementById('output'); + if (element) element.value = ''; // clear browser cache + return function(text) { + if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' '); + // These replacements are necessary if you render to raw HTML + //text = text.replace(/&/g, "&"); + //text = text.replace(/</g, "<"); + //text = text.replace(/>/g, ">"); + //text = text.replace('\n', '<br>', 'g'); + console.log(text); + if (element) { + element.value += text + "\n"; + element.scrollTop = element.scrollHeight; // focus on bottom + } + }; + })(), + canvas: (function() { + var canvas = document.getElementById('canvas'); + + // As a default initial behavior, pop up an alert when webgl context is lost. To make your + // application robust, you may want to override this behavior before shipping! + // See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2 + canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false); + + return canvas; + })(), + setStatus: function(text) { + if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' }; + if (text === Module.setStatus.last.text) return; + var now = Date.now(); + Module.setStatus.last.time = now; + Module.setStatus.last.text = text; + statusElement.innerHTML = text; + }, + totalDependencies: 0, + monitorRunDependencies: function(left) { + this.totalDependencies = Math.max(this.totalDependencies, left); + Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.'); + } + }; + Module.setStatus('Downloading...'); + window.onerror = function() { + Module.setStatus('Exception thrown, see JavaScript console'); + Module.setStatus = function(text) { + if (text) console.error('[post-exception status] ' + text); + }; + }; + </script> + {{{ SCRIPT }}} + </body> +</html> @@ -0,0 +1,15 @@ +def sum_digits(n): + s = 0 + while n: + s += n % 10 + n //= 10 + return s + +n = 0 +dig_sum = sum_digits(n) +for i in range(3): + n = n*10 + 9; + + for i in range(1, 200): + power = pow(n, i); + print(n, "^", i, "=", power, ';', sum_digits(power), power % 9); |