#include #include #include #include #include #include #include #include #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; }