spiral.c: add stupid spiral test thingy - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 922b3eb890066cd5b0f6ff4f5aee9c842c2be88a
 (DIR) parent 1592a2f1945ebb568608a0a41a8a44473b6d9d9c
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Mon, 20 Nov 2023 21:06:28 +0100
       
       spiral.c: add stupid spiral test thingy
       
       Diffstat:
         A spiral.c                            |     113 +++++++++++++++++++++++++++++++
       
       1 file changed, 113 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/spiral.c b/spiral.c
       @@ -0,0 +1,113 @@
       +/* stupid spiral algorithm thingy This was used to test loading OpenStreetMap
       +   from the center outwards straight to the eyeballs. */
       +
       +#include <stdio.h>
       +#include <stdlib.h>
       +#include <unistd.h>
       +
       +#define W 64
       +#define H 64
       +
       +static char grid[H][W];
       +
       +void
       +update(int x, int y)
       +{
       +        grid[y][x] = 1;
       +}
       +
       +void
       +printgrid(void)
       +{
       +        int x, y;
       +
       +        for (y = 0; y < H; y++) {
       +                putchar('|');
       +                for (x = 0; x < W; x++) {
       +                        putchar(grid[y][x] ? '*' : '.');
       +                }
       +                putchar('|');
       +                putchar('\n');
       +        }
       +}
       +
       +int
       +main(void)
       +{
       +        int x, y;
       +
       +        /* initial pattern */
       +        grid[0][0] = 1;
       +        grid[3][3] = 1;
       +        grid[2][1] = 1;
       +        grid[5][5] = 1;
       +
       +#if 1
       +        /* spiral: start approx in the middle */
       +        x = W / 2;
       +        y = H / 2;
       +        int dir = 0; /* R, U, L, D */
       +        int dirp[] = { /* X */ 1, 0, -1, 0, /* Y */ 0, -1, 0, 1 }; /* X[4], Y[4] transform */
       +
       +        int iter = 0;
       +
       +        /* loop until the last cell in the reverse spiral pattern (bottom-left) */
       +        for (; !(x == 0 && y >= H - 1) ;) {
       +                /* check if we need to change direction */
       +                /* R: can we go up or are we out of bounds? */
       +                /* U: can we go left or are we out of bounds? */
       +                /* L: can we go down or are we out of bounds? */
       +                /* D: can we go right or are we out of bounds? */
       +                if ((dir == 0 && y - 1 >= 0 && !grid[y - 1][x]) ||
       +                    (dir == 1 && x - 1 >= 0 && !grid[y][x - 1]) ||
       +                    (dir == 2 && y + 1 < H && !grid[y + 1][x]) ||
       +                    (dir == 3 && x + 1 < W && !grid[y][x + 1])) {
       +                        if (++dir > 3)
       +                                dir = 0;
       +                }
       +
       +                /* check if we need to change direction */
       +                /* R: can we go right or are we out of bounds? */
       +                /* U: can we go up or are we out of bounds? */
       +                /* L: can we go left or are we out of bounds? */
       +                /* D: can we go down or are we out of bounds? */
       +                if ((dir == 0 && x + 1 >= W) ||
       +                    (dir == 1 && y - 1 < 0) ||
       +                    (dir == 2 && x - 1 < 0) ||
       +                    (dir == 3 && y + 1 >= H)) {
       +                        if (++dir > 3)
       +                                dir = 0;
       +                        continue;
       +                }
       +
       +                x += dirp[dir];
       +                y += dirp[dir + 4];
       +
       +                if (!grid[y][x])
       +                        update(x, y);
       +
       +                printf("\x1b\x5b\x48\x1b\x5b\x32\x4a"); /* clear */
       +                printgrid();
       +                putchar('\n');
       +
       +                usleep(10000);
       +                iter++;
       +        }
       +        printf("iterations: %d, grid size: %d\n", iter, W * H);
       +#else
       +        for (y = 0; y < H; y++) {
       +                for (x = 0; x < W; x++) {
       +                        if (!grid[y][x])
       +                                update(x, y);
       +
       +                        printf("\x1b\x5b\x48\x1b\x5b\x32\x4a"); /* clear */
       +                        printgrid();
       +                        putchar('\n');
       +
       +                        usleep(100);
       +                }
       +        }
       +#endif
       +
       +        return 0;
       +}