hilbert curve
There is no browser build of this example yet.
Is it the same as C?
- Desktop:verified The Zig version renders a final frame that is byte-identical to the C example.
- Browser:verified In headless Chrome, the Zig version gives a final frame byte-identical to the C example built for the web.
//! raylib [shapes] example - hilbert curve
//!
//! Zig-API translation of examples/shapes/shapes_hilbert_curve.c (raylib 6.2).
//! Example contributed by Hamza RAHAL (@hmz-rhl) and reviewed by Ramon Santamaria (@raysan5)
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2025 Hamza RAHAL (@hmz-rhl)
const std = @import("std");
const rl = @import("raylib");
const gui = @import("raygui");
pub fn main(init: std.process.Init) !void {
const gpa = init.gpa;
// Initialization
const screen_width = 800;
const screen_height = 450;
rl.initWindow(screen_width, screen_height, "raylib [shapes] example - hilbert curve");
defer rl.closeWindow(); // Close window and OpenGL context
var order: i32 = 2;
var size: f32 = @floatFromInt(rl.getScreenHeight());
var hilbert_path = try loadHilbertPath(gpa, order, size);
defer gpa.free(hilbert_path);
var prev_order = order;
var prev_size: i32 = @intFromFloat(size); // NOTE: Size from slider is float but for comparison we use int
var counter: usize = 0;
var thick: f32 = 2.0;
var animate = true;
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
// Main game loop
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
// Update
// Check if order or size have changed to regenerate
// NOTE: Size from slider is float but for comparison we use int
const int_size: i32 = @intFromFloat(size);
if (prev_order != order or prev_size != int_size) {
gpa.free(hilbert_path);
hilbert_path = try loadHilbertPath(gpa, order, size);
counter = if (animate) 0 else hilbert_path.len;
prev_order = order;
prev_size = int_size;
}
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.ray_white);
if (counter < hilbert_path.len) {
// Draw Hilbert path animation, one stroke every frame
drawHilbertPath(hilbert_path, counter + 1, thick);
counter += 1;
} else {
// Draw full Hilbert path
drawHilbertPath(hilbert_path, hilbert_path.len, thick);
}
// Draw UI using raygui
_ = gui.checkBox(.{ .x = 450, .y = 50, .width = 20, .height = 20 }, "ANIMATE GENERATION ON CHANGE", &animate);
_ = gui.spinner(.{ .x = 585, .y = 100, .width = 180, .height = 30 }, "HILBERT CURVE ORDER: ", &order, 2, 8, false);
_ = gui.slider(.{ .x = 524, .y = 150, .width = 240, .height = 24 }, "THICKNESS: ", null, &thick, 1.0, 10.0);
_ = gui.slider(.{ .x = 524, .y = 190, .width = 240, .height = 24 }, "TOTAL SIZE: ", null, &size, 10.0, @as(f32, @floatFromInt(rl.getScreenHeight())) * 1.5);
}
}
/// Draw the strokes joining the first `point_count` points of the path,
/// colored by their position along the whole path.
fn drawHilbertPath(path: []const rl.Vector2, point_count: usize, thick: f32) void {
const stroke_count: f32 = @floatFromInt(path.len);
for (1..point_count) |i| {
const hue = (@as(f32, @floatFromInt(i)) / stroke_count) * 360.0;
rl.drawLineEx(path[i], path[i - 1], thick, rl.colorFromHSV(hue, 1.0, 1.0));
}
}
/// Load the whole Hilbert path (including each U and their link).
fn loadHilbertPath(gpa: std.mem.Allocator, order: i32, size: f32) ![]rl.Vector2 {
const n = @as(i32, 1) << @intCast(order);
const len = size / @as(f32, @floatFromInt(n));
const path = try gpa.alloc(rl.Vector2, @intCast(n * n));
for (path, 0..) |*point, i| {
const step = computeHilbertStep(order, @intCast(i));
point.* = .{
.x = step.x * len + len / 2.0,
.y = step.y * len + len / 2.0,
};
}
return path;
}
/// Compute Hilbert path U positions.
fn computeHilbertStep(order: i32, index: u32) rl.Vector2 {
// Hilbert points base pattern
const hilbert_points = [4]rl.Vector2{
.{ .x = 0, .y = 0 },
.{ .x = 0, .y = 1 },
.{ .x = 1, .y = 1 },
.{ .x = 1, .y = 0 },
};
var vect = hilbert_points[index & 3];
var remaining = index;
var j: u5 = 1;
while (j < order) : (j += 1) {
remaining >>= 2;
const len: f32 = @floatFromInt(@as(i32, 1) << j);
const prev = vect; // Snapshot: the swaps below read the old coordinates
switch (remaining & 3) {
0 => vect = .{ .x = prev.y, .y = prev.x },
1 => vect.y += len,
2 => {
vect.x += len;
vect.y += len;
},
3 => vect = .{ .x = 2 * len - 1 - prev.y, .y = len - 1 - prev.x },
else => unreachable,
}
}
return vect;
}/*******************************************************************************************
*
* raylib [shapes] example - hilbert curve
*
* 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) and reviewed by Ramon Santamaria (@raysan5)
*
* 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"
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
#include <stdlib.h> // Required for: calloc(), free()
//------------------------------------------------------------------------------------
// Module Functions Declaration
//------------------------------------------------------------------------------------
static Vector2 *LoadHilbertPath(int order, float size, int *strokeCount);
static void UnloadHilbertPath(Vector2 *hilbertPath);
static Vector2 ComputeHilbertStep(int order, int index);
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - hilbert curve");
int order = 2;
float size = (float)GetScreenHeight();
int strokeCount = 0;
Vector2 *hilbertPath = LoadHilbertPath(order, size, &strokeCount);
int prevOrder = order;
int prevSize = (int)size; // NOTE: Size from slider is float but for comparison we use int
int counter = 0;
float thick = 2.0f;
bool animate = true;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
//--------------------------------------------------------------------------------------
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// Check if order or size have changed to regenerate
// NOTE: Size from slider is float but for comparison we use int
if ((prevOrder != order) || (prevSize != (int)size))
{
UnloadHilbertPath(hilbertPath);
hilbertPath = LoadHilbertPath(order, size, &strokeCount);
if (animate) counter = 0;
else counter = strokeCount;
prevOrder = order;
prevSize = (int)size;
}
//----------------------------------------------------------------------------------
// Draw
//--------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
if (counter < strokeCount)
{
// Draw Hilbert path animation, one stroke every frame
for (int i = 1; i <= counter; i++)
{
DrawLineEx(hilbertPath[i], hilbertPath[i - 1], thick, ColorFromHSV(((float)i/strokeCount)*360.0f, 1.0f, 1.0f));
}
counter += 1;
}
else
{
// Draw full Hilbert path
for (int i = 1; i < strokeCount; i++)
{
DrawLineEx(hilbertPath[i], hilbertPath[i - 1], thick, ColorFromHSV(((float)i/strokeCount)*360.0f, 1.0f, 1.0f));
}
}
// Draw UI using raygui
GuiCheckBox((Rectangle){ 450, 50, 20, 20 }, "ANIMATE GENERATION ON CHANGE", &animate);
GuiSpinner((Rectangle){ 585, 100, 180, 30 }, "HILBERT CURVE ORDER: ", &order, 2, 8, false);
GuiSlider((Rectangle){ 524, 150, 240, 24 }, "THICKNESS: ", NULL, &thick, 1.0f, 10.0f);
GuiSlider((Rectangle){ 524, 190, 240, 24 }, "TOTAL SIZE: ", NULL, &size, 10.0f, GetScreenHeight()*1.5f);
EndDrawing();
//--------------------------------------------------------------------------
}
//--------------------------------------------------------------------------------------
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadHilbertPath(hilbertPath);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//------------------------------------------------------------------------------------
// Module Functions Definition
//------------------------------------------------------------------------------------
// Load the whole Hilbert Path (including each U and their link)
static Vector2 *LoadHilbertPath(int order, float size, int *strokeCount)
{
int N = 1 << order;
float len = size/N;
*strokeCount = N*N;
Vector2 *hilbertPath = (Vector2 *)RL_CALLOC(*strokeCount, sizeof(Vector2));
for (int i = 0; i < *strokeCount; i++)
{
hilbertPath[i] = ComputeHilbertStep(order, i);
hilbertPath[i].x = hilbertPath[i].x*len + len/2.0f;
hilbertPath[i].y = hilbertPath[i].y*len + len/2.0f;
}
return hilbertPath;
}
// Unload Hilbert path data
static void UnloadHilbertPath(Vector2 *hilbertPath)
{
RL_FREE(hilbertPath);
}
// Compute Hilbert path U positions
static Vector2 ComputeHilbertStep(int order, int index)
{
// Hilbert points base pattern
static const Vector2 hilbertPoints[4] = {
[0] = { .x = 0, .y = 0 },
[1] = { .x = 0, .y = 1 },
[2] = { .x = 1, .y = 1 },
[3] = { .x = 1, .y = 0 },
};
int hilbertIndex = index&3;
Vector2 vect = hilbertPoints[hilbertIndex];
float temp = 0.0f;
int len = 0;
for (int j = 1; j < order; j++)
{
index = index >> 2;
hilbertIndex = index&3;
len = 1 << j;
switch (hilbertIndex)
{
case 0:
{
temp = vect.x;
vect.x = vect.y;
vect.y = temp;
} break;
case 2: vect.x += len;
case 1: vect.y += len; break;
case 3:
{
temp = len - 1 - vect.x;
vect.x = 2*len - 1 - vect.y;
vect.y = temp;
} break;
default: break;
}
}
return vect;
}From raylib 6.2. The original on GitHub.