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