From 5b4a191d1cf310a3d7a99351e716a1a60004be69 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Wed, 11 Nov 2026 09:32:30 +0300 Subject: publish --- src/bez7.c | 283 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 283 insertions(+) create mode 100644 src/bez7.c (limited to 'src/bez7.c') diff --git a/src/bez7.c b/src/bez7.c new file mode 100644 index 0000000..d63014c --- /dev/null +++ b/src/bez7.c @@ -0,0 +1,283 @@ +#include +#include +#include +#include +#include +#include + +typedef Vector2 bezier7[8]; + +struct input { + Vector2 r_beg; + Vector2 r_end; + Vector2 v_beg; + Vector2 v_end; + float v_max; + float a_max; + float j_max; +}; + +#define RGBA(HEX) (Color) { \ + (HEX) >> 3*8 & 0xFF, \ + (HEX) >> 2*8 & 0xFF, \ + (HEX) >> 1*8 & 0xFF, \ + (HEX) >> 0*8 & 0xFF, \ + } + +// #define LABEL_WIDTH 140 +#define LABEL_WIDTH 300 +#define SLIDER_WIDTH 40 +#define ITEM_WIDTH (LABEL_WIDTH + SLIDER_WIDTH) +#define ITEM_HEIGHT 15 +#define ITEM_MARGIN 5 +#define PANEL_WIDTH (ITEM_WIDTH + 2*ITEM_MARGIN) +#define PANEL_HEIGHT(N) (RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT + (N)*(ITEM_HEIGHT + ITEM_MARGIN) + ITEM_MARGIN) + +#ifdef DARK +#define BACKGROUND RGBA (0x000000FF) +#else +#define BACKGROUND RGBA (0xFFFFFFFF) +#endif + +#define ARROW_THICKNESS 2.5 +#define ARROW_LENGTH 15 +#define ARROW_ANGLE 10 + +#define CONTROL_POINT_RADIUS 5 +#define CONTROL_POINT_RADIUS_FOCUSED 10 + + +void +draw_arrow (Vector2 beg, Vector2 end, float thick, Color c) +{ + Vector2 off = Vector2Scale (Vector2Normalize (Vector2Subtract (end, beg)), + ARROW_LENGTH); + Vector2 ahl = Vector2Add (end, Vector2Rotate (off, +ARROW_ANGLE)); + Vector2 ahr = Vector2Add (end, Vector2Rotate (off, -ARROW_ANGLE)); + + DrawLineEx (beg, end, thick, c); + DrawLineEx (end, ahl, thick, c); + DrawLineEx (end, ahr, thick, c); + + DrawCircleV (beg, thick/2., c); + DrawCircleV (end, thick/2., c); + DrawCircleV (ahl, thick/2., c); + DrawCircleV (ahr, thick/2., c); +} + +Vector2 +sample_bezier7 (const bezier7 pts, float t) +{ + float u = 1.0f - t; + + float t2 = t * t; + float t3 = t2 * t; + float t4 = t3 * t; + float t5 = t4 * t; + float t6 = t5 * t; + float t7 = t6 * t; + + /* Precompute powers of u (1 - t) */ + float u2 = u * u; + float u3 = u2 * u; + float u4 = u3 * u; + float u5 = u4 * u; + float u6 = u5 * u; + float u7 = u6 * u; + + /* Bernstein basis polynomials for degree 7 */ + float b0 = u7; + float b1 = 7.0f * u6 * t; + float b2 = 21.0f * u5 * t2; + float b3 = 35.0f * u4 * t3; + float b4 = 35.0f * u3 * t4; + float b5 = 21.0f * u2 * t5; + float b6 = 7.0f * u * t6; + float b7 = t7; + + Vector2 result; + + /* Calculate final x and y by blending control points with basis weights */ + result.x = (b0 * pts[0].x) + (b1 * pts[1].x) + (b2 * pts[2].x) + (b3 * pts[3].x) + + (b4 * pts[4].x) + (b5 * pts[5].x) + (b6 * pts[6].x) + (b7 * pts[7].x); + + result.y = (b0 * pts[0].y) + (b1 * pts[1].y) + (b2 * pts[2].y) + (b3 * pts[3].y) + + (b4 * pts[4].y) + (b5 * pts[5].y) + (b6 * pts[6].y) + (b7 * pts[7].y); + + return result; +} + +void +draw_bezier7 (bezier7 cp) +{ + char text[] = "P0"; + DrawLineStrip (cp, 8, BLUE); + for (int i = 0; i < 8; ++i) { + DrawCircleLinesV (cp[i], 4, BLUE); + text[1] = '0' + i; + DrawText (text, cp[i].x - 5, cp[i].y + 7, 14, BLACK); + } + + for (float t = 0; t <= 1; t += 0.01) { + DrawCircleV (sample_bezier7 (cp, t), 2, BLACK); + } +} + +int +main (int argc, char *argv[]) +{ + srand (time (NULL)); + + SetConfigFlags (FLAG_MSAA_4X_HINT); + SetConfigFlags (FLAG_WINDOW_RESIZABLE); + InitWindow (800, 450, "GA"); + SetTargetFPS (60); + + Camera2D camera = { + .zoom = 1.0f, + }; + + Vector2 screen = { + .x = GetScreenWidth (), + .y = GetScreenHeight (), + }; + + struct input in = { + .r_beg = { 100, 100 }, + // .r_end = { 100, 100 }, + .r_end = { 300, 300 }, + .v_beg = { 100, 10 }, + .v_end = { 100, 10 }, + .v_max = 250, + .a_max = 500, + .j_max = 16, + }; + + bezier7 cp; + + float arrow_thickness = ARROW_THICKNESS; + float spline_thickness = ARROW_THICKNESS; + bool move_r_beg = false; + bool move_r_end = false; + bool move_v_beg = false; + bool move_v_end = false; + bool recompute = true; + bool show_fps = true; + bool show_menu = true; + bool show_graph = true; + bool draw_cp = true; + bool animate_sample_offset = false; + float animation_speed = 20; + int panel_item = 0; + bool resizing_graph = false; + int graph_height = screen.y/3; + float T = 10; + while (!WindowShouldClose ()) { + screen = (Vector2) { + .x = GetScreenWidth (), + .y = GetScreenHeight (), + }; + float t = GetTime (); + float dt = GetFrameTime (); + + if (IsMouseButtonDown (MOUSE_BUTTON_RIGHT)) { + Vector2 delta = GetMouseDelta (); + delta = Vector2Scale (delta, -1.0f/camera.zoom); + camera.target = Vector2Add (camera.target, delta); + } + + if (IsKeyPressed (KEY_F)) show_fps = !show_fps; + + if (IsKeyDown (KEY_LEFT )) T -= .1; + if (IsKeyDown (KEY_RIGHT)) T += .1; + + + if (1 || recompute) { + // chromosome_to_control_points (cp, in, ch); + cp[0] = in.r_beg; + cp[1] = Vector2Add (in.r_beg, Vector2Scale (in.v_beg, +1*T/7)); + cp[2] = Vector2Add (in.r_beg, Vector2Scale (in.v_beg, +2*T/7)); + cp[3] = Vector2Add (in.r_beg, Vector2Scale (in.v_beg, +3*T/7)); + cp[4] = Vector2Add (in.r_end, Vector2Scale (in.v_end, -3*T/7)); + cp[5] = Vector2Add (in.r_end, Vector2Scale (in.v_end, -2*T/7)); + cp[6] = Vector2Add (in.r_end, Vector2Scale (in.v_end, -1*T/7)); + cp[7] = in.r_end; + recompute = false; + } + + float wheel = GetMouseWheelMove(); + if (wheel != 0) { + Vector2 offset = GetMousePosition (); + camera.target = GetScreenToWorld2D (offset, camera); + camera.offset = offset; + camera.zoom = Clamp (expf (logf (camera.zoom) + .2*wheel), .125, 64); + } + + Vector2 mouse_hud = GetMousePosition (); + Vector2 mouse = GetScreenToWorld2D (mouse_hud, camera); + + BeginDrawing (); + ClearBackground (BACKGROUND); + BeginMode2D (camera); + + if (CheckCollisionPointCircle (mouse, in.r_beg, 10)) { + DrawCircleLinesV (in.r_beg, 10, BLUE); + if (!move_v_beg && IsMouseButtonDown (MOUSE_BUTTON_LEFT)) + move_r_beg = true; + } else if (CheckCollisionPointCircle (mouse, in.r_end, 10)) { + DrawCircleLinesV (in.r_end, 10, BLUE); + if (!move_v_end && IsMouseButtonDown (MOUSE_BUTTON_LEFT)) + move_r_end = true; + } else if (CheckCollisionPointCircle (mouse, Vector2Add (in.r_beg, in.v_beg), 10)) { + DrawCircleLinesV (Vector2Add (in.r_beg, in.v_beg), 10, BLUE); + if (!move_r_beg && IsMouseButtonDown (MOUSE_BUTTON_LEFT)) + move_v_beg = true; + } else if (CheckCollisionPointCircle (mouse, Vector2Add (in.r_end, in.v_end), 10)) { + DrawCircleLinesV (Vector2Add (in.r_end, in.v_end), 10, BLUE); + if (!move_r_end && IsMouseButtonDown (MOUSE_BUTTON_LEFT)) + move_v_end = true; + } + + if (move_r_beg) { + in.r_beg = mouse; + if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT)) + move_r_beg = false; + } + if (move_r_end) { + in.r_end = mouse; + if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT)) + move_r_end = false; + } + if (move_v_beg) { + in.v_beg = Vector2Subtract (mouse, in.r_beg); + if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT)) + move_v_beg = false; + } + if (move_v_end) { + in.v_end = Vector2Subtract (mouse, in.r_end); + if (IsMouseButtonReleased (MOUSE_BUTTON_LEFT)) + move_v_end = false; + } + + draw_bezier7 (cp); + draw_arrow (in.r_beg, Vector2Add (in.r_beg, in.v_beg), arrow_thickness, GREEN); + draw_arrow (in.r_end, Vector2Add (in.r_end, in.v_end), arrow_thickness, RED); + + EndMode2D (); + + if (move_r_beg || + move_r_end || + move_v_beg || + move_v_end) { + recompute = true; + } + + if (show_fps) + DrawFPS (screen.x - 75, 0); + EndDrawing (); + } + + CloseWindow (); + + return EXIT_SUCCESS; +} -- cgit v1.3.1