spiral.c - 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
       ---
       spiral.c (2337B)
       ---
            1 /* stupid spiral algorithm thingy.
            2    This was used to test loading OpenStreetMap from the center outwards
            3    straight to the eyeballs. */
            4 
            5 #include <stdio.h>
            6 #include <stdlib.h>
            7 #include <unistd.h>
            8 
            9 #define W 64
           10 #define H 64
           11 
           12 static char grid[H][W];
           13 
           14 void
           15 update(int x, int y)
           16 {
           17         grid[y][x] = 1;
           18 }
           19 
           20 void
           21 printgrid(void)
           22 {
           23         int x, y;
           24 
           25         for (y = 0; y < H; y++) {
           26                 putchar('|');
           27                 for (x = 0; x < W; x++) {
           28                         putchar(grid[y][x] ? '*' : '.');
           29                 }
           30                 putchar('|');
           31                 putchar('\n');
           32         }
           33 }
           34 
           35 int
           36 main(void)
           37 {
           38         int x, y;
           39 
           40         /* initial pattern */
           41         grid[0][0] = 1;
           42         grid[3][3] = 1;
           43         grid[2][1] = 1;
           44         grid[5][5] = 1;
           45 
           46 #if 1
           47         /* spiral: start approx in the middle */
           48         x = W / 2;
           49         y = H / 2;
           50         int dir = 0; /* R, U, L, D */
           51         int dirp[] = { /* X */ 1, 0, -1, 0, /* Y */ 0, -1, 0, 1 }; /* X[4], Y[4] transform */
           52 
           53         int iter = 0;
           54 
           55         /* loop until the last cell in the reverse spiral pattern (bottom-left) */
           56         for (; !(x == 0 && y >= H - 1) ;) {
           57                 /* check if we need to change direction */
           58                 /* R: can we go up or are we out of bounds? */
           59                 /* U: can we go left or are we out of bounds? */
           60                 /* L: can we go down or are we out of bounds? */
           61                 /* D: can we go right or are we out of bounds? */
           62                 if ((dir == 0 && y - 1 >= 0 && !grid[y - 1][x]) ||
           63                     (dir == 1 && x - 1 >= 0 && !grid[y][x - 1]) ||
           64                     (dir == 2 && y + 1 < H && !grid[y + 1][x]) ||
           65                     (dir == 3 && x + 1 < W && !grid[y][x + 1])) {
           66                         if (++dir > 3)
           67                                 dir = 0;
           68                 }
           69 
           70                 /* check if we need to change direction */
           71                 /* R: can we go right or are we out of bounds? */
           72                 /* U: can we go up or are we out of bounds? */
           73                 /* L: can we go left or are we out of bounds? */
           74                 /* D: can we go down or are we out of bounds? */
           75                 if ((dir == 0 && x + 1 >= W) ||
           76                     (dir == 1 && y - 1 < 0) ||
           77                     (dir == 2 && x - 1 < 0) ||
           78                     (dir == 3 && y + 1 >= H)) {
           79                         if (++dir > 3)
           80                                 dir = 0;
           81                         continue;
           82                 }
           83 
           84                 x += dirp[dir];
           85                 y += dirp[dir + 4];
           86 
           87                 if (!grid[y][x])
           88                         update(x, y);
           89 
           90                 printf("\x1b\x5b\x48\x1b\x5b\x32\x4a"); /* clear */
           91                 printgrid();
           92                 putchar('\n');
           93 
           94                 usleep(10000);
           95                 iter++;
           96         }
           97         printf("iterations: %d, grid size: %d\n", iter, W * H);
           98 #else
           99         for (y = 0; y < H; y++) {
          100                 for (x = 0; x < W; x++) {
          101                         if (!grid[y][x])
          102                                 update(x, y);
          103 
          104                         printf("\x1b\x5b\x48\x1b\x5b\x32\x4a"); /* clear */
          105                         printgrid();
          106                         putchar('\n');
          107 
          108                         usleep(100);
          109                 }
          110         }
          111 #endif
          112 
          113         return 0;
          114 }