From 752373867741e044244df0e64695463598a4a742 Mon Sep 17 00:00:00 2001 From: Hamza RAHAL <77698738+hmz-rhl@users.noreply.github.com> Date: Tue, 30 Dec 2025 20:11:37 +0100 Subject: [PATCH] Add hilbert curve example (#5454) --- examples/shapes/shapes_hilbert_curve.c | 187 +++++++++++++++++++++++ examples/shapes/shapes_hilbert_curve.png | Bin 0 -> 15288 bytes 2 files changed, 187 insertions(+) create mode 100644 examples/shapes/shapes_hilbert_curve.c create mode 100644 examples/shapes/shapes_hilbert_curve.png diff --git a/examples/shapes/shapes_hilbert_curve.c b/examples/shapes/shapes_hilbert_curve.c new file mode 100644 index 000000000..1af263b34 --- /dev/null +++ b/examples/shapes/shapes_hilbert_curve.c @@ -0,0 +1,187 @@ +/******************************************************************************************* +* +* raylib [shapes] example - hilbert curve example +* +* Example complexity rating: [★★★☆] 3/4 +* +* Example originally created with raylib 5.6, last time updated with raylib 5.6 +* +* Example contributed by Hamza RAHAL (@hmz-rhl) +* +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +* BSD-like license that allows static linking with closed source software +* +* Copyright (c) 2025 Hamza RAHAL (@hmz-rhl) +* +********************************************************************************************/ + + +#include "raylib.h" +#include "raymath.h" +#include +#include + +const int screenWidth = 800; + +const int screenHeight = 450; + +int order = 2; + +int total; + +int counter = 0; + +Vector2 *hilbertPath = 0; + +const Vector2 hilbertPoints[4] = +{ + [0] = { + .x = 0, + .y = 0 + }, + [1] = { + .x = 0, + .y = 1 + }, + [2] = { + .x = 1, + .y = 1 + }, + [3] = { + .x = 1, + .y = 0 + }, +}; + +//------------------------------------------------------------------------------------ +// Module Functions Declaration +//------------------------------------------------------------------------------------ +Vector2 Hilbert(int index); + +void InitHilbertPath(void); + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + + InitWindow(screenWidth, screenHeight, "raylib [shapes] example - hilbert curve example"); + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + + InitHilbertPath(); + + //-------------------------------------------------------------------------------------- + + // Main game loop + //-------------------------------------------------------------------------------------- + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + if ((IsKeyPressed(KEY_UP)) && (order < 8)) + { + counter = 0; + ++order; + InitHilbertPath(); + } + else if((IsKeyPressed(KEY_DOWN)) && (order > 1)) + { + counter = 0; + --order; + InitHilbertPath(); + } + //---------------------------------------------------------------------------------- + + // Draw + //-------------------------------------------------------------------------- + BeginDrawing(); + DrawText(TextFormat("(press UP or DOWN to change)\norder : %d", order), screenWidth/2 + 70, 25, 20, WHITE); + + if(counter < total) + { + ClearBackground(BLACK); + for (int i = 1; i <= counter; i++) + { + DrawLineV(hilbertPath[i], hilbertPath[i-1], ColorFromHSV(((float)i / total) * 360.0f, 1.0f, 1.0f)); + } + counter += 1; + } + EndDrawing(); + //-------------------------------------------------------------------------- + } + //-------------------------------------------------------------------------------------- + + // De-Initialization + //-------------------------------------------------------------------------------------- + CloseWindow(); // Close window and OpenGL context + MemFree(hilbertPath); + //-------------------------------------------------------------------------------------- + return 0; +} + +//------------------------------------------------------------------------------------ +// Module Functions Definition +//------------------------------------------------------------------------------------ + +// calculate U positions +Vector2 Hilbert(int index) +{ + + int hiblertIndex = index&3; + Vector2 vect = hilbertPoints[hiblertIndex]; + float temp; + int len; + + for (int j = 1; j < order; j++) + { + index = index>>2; + hiblertIndex = index&3; + len = 1<sQpi&T zhjR{2Cx9Z!1ZP4q_~VpN!j@tZ+K)934rFj#&}DJ6R#@Dbi^~b%NMgeoNp0~QJ?9#F zmc7D~3c(&{+|_y@gFVG$V=pczfFkKKG(xZ>mJi&F(=K|*qK4ptI*vd8|H~f@aC19$ zF#(nY6lIyNXkB-kp3s}tg2z1!85Ym}XH=ktNq{;_f^y4ZW4R-(>Kh-nuH&m!nKmKU zS=FxNtN5<^6TJLEm$lbbD2VKva?3d4^zw=G?w|bnACv-nt#RcMhwTCqcd?}Lwj2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