Skip to content

Commit 2dc8b3b

Browse files
committed
Add hilbert curve example
1 parent 2b48cf6 commit 2dc8b3b

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed

examples/shapes/shapes_hilbert.c

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*******************************************************************************************
2+
*
3+
* raylib [shapes] example - hilbert curve example
4+
*
5+
* Example complexity rating: [★★★☆] 3/4
6+
*
7+
* Example originally created with raylib 5.6, last time updated with raylib 5.6
8+
*
9+
* Example contributed by Hamza RAHAL (@hmz-rhl)
10+
*
11+
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
12+
* BSD-like license that allows static linking with closed source software
13+
*
14+
* Copyright (c) 2025 Hamza RAHAL (@hmz-rhl)
15+
*
16+
********************************************************************************************/
17+
18+
19+
#include "raylib.h"
20+
#include "raymath.h"
21+
#include <stdlib.h>
22+
#include <stdio.h>
23+
24+
const int screenWidth = 800;
25+
const int screenHeight = screenWidth + 100;
26+
27+
int order = 2;
28+
int total;
29+
int counter = 0;
30+
Vector2 *hilbertPath = 0;
31+
const Vector2 hilbertPoints[4] =
32+
{
33+
[0] = {
34+
.x = 0,
35+
.y = 0
36+
},
37+
[1] = {
38+
.x = 0,
39+
.y = 1
40+
},
41+
[2] = {
42+
.x = 1,
43+
.y = 1
44+
},
45+
[3] = {
46+
.x = 1,
47+
.y = 0
48+
},
49+
};
50+
51+
Vector2 Hilbert(int index)
52+
{
53+
54+
int hiblertIndex = index&3;
55+
Vector2 vect = hilbertPoints[hiblertIndex];
56+
float temp;
57+
int len;
58+
59+
for (int j = 1; j < order; j++)
60+
{
61+
index = index>>2;
62+
hiblertIndex = index&3;
63+
len = 1<<j;
64+
switch (hiblertIndex)
65+
{
66+
case 0:
67+
temp = vect.x;
68+
vect.x = vect.y;
69+
vect.y = temp;
70+
break;
71+
case 2:
72+
vect.x += len;
73+
case 1:
74+
vect.y += len;
75+
break;
76+
case 3:
77+
temp = len - 1 - vect.x;
78+
vect.x = 2*len - 1 - vect.y;
79+
vect.y = temp;
80+
break;
81+
}
82+
}
83+
return vect;
84+
}
85+
86+
void InitHilbertPath()
87+
{
88+
int N;
89+
float len;
90+
N = 1<<order;
91+
total = N*N;
92+
MemFree(hilbertPath);
93+
hilbertPath = NULL;
94+
hilbertPath = (Vector2*)MemAlloc(sizeof(Vector2)*total);
95+
if(hilbertPath == NULL)
96+
{
97+
printf("%s: malloc failed\n", __func__);
98+
}
99+
len = (float)screenWidth/N;
100+
for (int i = 0; i < total; i++)
101+
{
102+
hilbertPath[i] = Hilbert(i);
103+
hilbertPath[i].x = hilbertPath[i].x*len + len/2.0f;
104+
hilbertPath[i].y = hilbertPath[i].y*len + len/2.0f;
105+
}
106+
}
107+
108+
//------------------------------------------------------------------------------------
109+
// Program main entry point
110+
//------------------------------------------------------------------------------------
111+
112+
int main(void)
113+
{
114+
// Initialization
115+
//--------------------------------------------------------------------------------------
116+
117+
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - hilbert curve example");
118+
119+
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
120+
121+
InitHilbertPath();
122+
123+
//--------------------------------------------------------------------------------------
124+
125+
// Main game loop
126+
//--------------------------------------------------------------------------------------
127+
while (!WindowShouldClose()) // Detect window close button or ESC key
128+
{
129+
BeginDrawing();
130+
DrawText(TextFormat("(press UP or DOWN to change) order : %d", order), 20, screenHeight - 25, 20, WHITE);
131+
132+
if(counter < total)
133+
{
134+
ClearBackground(BLACK);
135+
for (int i = 1; i <= counter; i++)
136+
{
137+
// Draw
138+
//--------------------------------------------------------------------------
139+
DrawLineV(hilbertPath[i], hilbertPath[i-1], ColorFromHSV(((float)i / total) * 360.0f, 1.0f, 1.0f));
140+
//--------------------------------------------------------------------------
141+
}
142+
counter += 1;
143+
}
144+
// Update
145+
//----------------------------------------------------------------------------------
146+
if ((IsKeyPressed(KEY_UP)) && (order < 8))
147+
{
148+
counter = 0;
149+
++order;
150+
InitHilbertPath();
151+
}
152+
else if((IsKeyPressed(KEY_DOWN)) && (order > 1))
153+
{
154+
counter = 0;
155+
--order;
156+
InitHilbertPath();
157+
}
158+
//----------------------------------------------------------------------------------
159+
EndDrawing();
160+
}
161+
//--------------------------------------------------------------------------------------
162+
163+
// De-Initialization
164+
//--------------------------------------------------------------------------------------
165+
CloseWindow(); // Close window and OpenGL context
166+
MemFree(hilbertPath);
167+
//--------------------------------------------------------------------------------------
168+
return 0;
169+
}

0 commit comments

Comments
 (0)