aboutsummaryrefslogtreecommitdiff
path: root/src/peano-curve.c
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 02:51:49 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 02:51:49 +0000
commit80893fa3e272d9792ba1206f7006e8defeeccb6e (patch)
tree1867dba062a481bf59313b0fbb16f416834a11a3 /src/peano-curve.c
downloadlab-02-80893fa3e272d9792ba1206f7006e8defeeccb6e.tar.gz
publishHEADmaster
Diffstat (limited to 'src/peano-curve.c')
-rw-r--r--src/peano-curve.c158
1 files changed, 158 insertions, 0 deletions
diff --git a/src/peano-curve.c b/src/peano-curve.c
new file mode 100644
index 0000000..0b4a4ec
--- /dev/null
+++ b/src/peano-curve.c
@@ -0,0 +1,158 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <inttypes.h>
5#include <assert.h>
6#include <raylib.h>
7#include <raymath.h>
8#include <rlgl.h>
9
10#define MALLOC malloc
11#define REALLOC realloc
12#define FREE free
13#define ASSERT assert
14
15#include "dynamic-array.h"
16
17struct squares {
18 Vector2 *items;
19 float side;
20 uint16_t count;
21 uint16_t capacity;
22};
23
24enum ordering {
25 ORDERING_NE = 0b00,
26 ORDERING_NW = 0b01,
27 ORDERING_SE = 0b10,
28 ORDERING_SW = 0b11,
29};
30
31static enum ordering
32ordering_next_vertical (enum ordering ord)
33{
34 return ord ^ 0b01;
35}
36
37static enum ordering
38ordering_next_horizontal (enum ordering ord)
39{
40 return ord ^ 0b10;
41}
42
43static int
44ordering_offset_vertical (enum ordering ord)
45{
46 return 1 - (ord & 0b10);
47}
48
49static int
50ordering_offset_horizontal (enum ordering ord)
51{
52 return 1 - 2*(ord & 0b01);
53}
54
55static void
56advance (struct squares *s)
57{
58 struct squares tmp = { .side = s->side/3 };
59 DYNAMIC_ARRAY_ENSURE_CAPACITY (tmp, 9*s->count);
60
61 enum ordering ord = ORDERING_NE;
62 Vector2 offset = { -1, -1 };
63 for (uint32_t i = 0; i < s->count; ++i) {
64 for (int j = 0; j < 3; ++j) {
65 if (j) {
66 ord = ordering_next_horizontal (ord);
67 offset.x += ordering_offset_horizontal (ord);
68 }
69 for (int k = 0; k < 3; ++k) {
70 if (k) {
71 ord = ordering_next_vertical (ord);
72 offset.y += ordering_offset_vertical (ord);
73 }
74
75 Vector2 c = Vector2Add (s->items[i], Vector2Scale (offset, tmp.side));
76 DYNAMIC_ARRAY_APPEND (tmp, c);
77 }
78 }
79
80 if (i + 1 < s->count) {
81 if (s->items[i + 1].x != s->items[i + 0].x) {
82 ord = ordering_next_horizontal (ord);
83 offset.x += ordering_offset_horizontal (ord);
84 if (offset.x > +1) offset.x = -1;
85 else if (offset.x < -1) offset.x = +1;
86 } else {
87 ord = ordering_next_vertical (ord);
88 offset.y += ordering_offset_vertical (ord);
89 if (offset.y > +1) offset.y = -1;
90 else if (offset.y < -1) offset.y = +1;
91 }
92 }
93 }
94
95 DYNAMIC_ARRAY_FREE (*s);
96 *s = tmp;
97}
98
99int
100main (int argc, char *argv[])
101{
102 SetConfigFlags (FLAG_MSAA_4X_HINT |
103 FLAG_WINDOW_UNDECORATED |
104 FLAG_WINDOW_RESIZABLE |
105 FLAG_FULLSCREEN_MODE |
106 FLAG_VSYNC_HINT);
107 // InitWindow (600, 600, "Peano Curve");
108 InitWindow (0, 0, "Peano Curve");
109
110 Camera2D camera = {
111 .offset = { .5, .5 },
112 };
113
114 struct squares squares = { .side = 1. };
115 DYNAMIC_ARRAY_APPEND (squares, ((Vector2) { .5, .5 }));
116 advance (&squares);
117 advance (&squares);
118 advance (&squares);
119 advance (&squares);
120
121 int draw_up_to = squares.count;
122 while (!WindowShouldClose ()) {
123 if (draw_up_to > 0 && IsKeyDown (KEY_DOWN))
124 draw_up_to -= 1;
125 else if (draw_up_to < squares.count && IsKeyDown (KEY_UP))
126 draw_up_to += 1;
127
128 Vector2 center = (Vector2) { GetScreenWidth ()/2, GetScreenHeight ()/2 };
129 camera.zoom = fminf (center.x, center.y)*2;
130
131 BeginDrawing (); {
132 ClearBackground (BLACK);
133 BeginMode2D (camera); {
134 Vector2 head = Vector2Zero ();
135 for (uint32_t i = 0; i < squares.count; ++i) {
136 if (i == draw_up_to) break;
137 Vector2 c = squares.items[i];
138 c.y = 1 - c.y;
139 Rectangle rec = {
140 .x = c.x - squares.side/2,
141 .y = c.y - squares.side/2,
142 .width = squares.side,
143 .height = squares.side,
144 };
145 DrawRectangleLinesEx (rec, .0005, DARKGRAY);
146 if (i < squares.count - 1) {
147 Vector2 nc = squares.items[i + 1];
148 nc.y = 1 - nc.y;
149 DrawLineEx (c, nc, 0.001, BLUE);
150 }
151 DrawCircleV (c, .0015, RED);
152 }
153 } EndMode2D ();
154 } EndDrawing ();
155 }
156
157 return EXIT_SUCCESS;
158}