Advertisement
igendel

A Little Fun with FloodFills

Dec 31st, 2020
1,377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3.  
  4. #define PIC_X_SIZE 80U
  5. #define PIC_Y_SIZE 25U
  6.  
  7. const char EMPTY =  '.';
  8. const char WALL  =  '#';
  9. const char FILLED = '/';
  10.  
  11. // Size is Y by (X + 1) because data is stored in strings, so we need room for the \0 .
  12. char pic[PIC_Y_SIZE][PIC_X_SIZE+1] = {
  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. char pic[PIC_Y_SIZE][PIC_X_SIZE+1] = {
  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. int rDepth, calls;
  71. clock_t tStart, tEnd;
  72.  
  73.  
  74. // Recursive, pixel-step
  75. // Assumes the picture array has a WALL frame, and that the outside-call x and y are inside the frame
  76. void floodFill_R_Pixel(const int x, const int y, const int depth) {
  77.    
  78.     if (depth > rDepth) {
  79.         rDepth = depth;
  80.     }  
  81.     calls++;
  82.    
  83.     if (EMPTY == pic[y][x]) {
  84.        
  85.         pic[y][x] = FILLED;
  86.         floodFill_R_Pixel(x - 1, y    , depth + 1);
  87.         floodFill_R_Pixel(x    , y - 1, depth + 1);
  88.         floodFill_R_Pixel(x + 1, y    , depth + 1);
  89.         floodFill_R_Pixel(x    , y + 1, depth + 1);
  90.        
  91.     }  
  92.    
  93. }  
  94.  
  95.  
  96. // Recursive, horizontal line based
  97. // Assumes the picture array has a WALL frame, and that the outside-call x and y are inside the frame
  98. void floodFill_R_Line(const int x, const int y, const int depth) {
  99.    
  100.     int x1 = x, x2 = x + 1;
  101.  
  102.     if (depth > rDepth) {
  103.         rDepth = depth;
  104.     }  
  105.     calls++;
  106.    
  107.     if (EMPTY != pic[y][x]) return;
  108.        
  109.     while (EMPTY == pic[y][x1]) {
  110.         pic[y][x1--] = FILLED;
  111.     }  
  112.  
  113.     while (EMPTY == pic[y][x2]) {
  114.         pic[y][x2++] = FILLED;
  115.     }  
  116.  
  117.     for (++x1 ; x1 < x2; x1++) {
  118.         floodFill_R_Line(x1, y - 1, depth + 1);
  119.         floodFill_R_Line(x1, y + 1, depth + 1);
  120.     }  
  121.    
  122. }  
  123.  
  124.  
  125. // Non-recursive, pixel-step
  126. // Assumes the picture array has a WALL frame, and that the outside-call x and y are inside the frame
  127. void floodFill_Pixel(const int x, const int y) {
  128.    
  129.     int changes, xx, yy;
  130.    
  131.     pic[y][x] = FILLED;
  132.     do {
  133.  
  134.         calls++;       
  135.         changes = 0;
  136.         for (xx = 1; xx < PIC_X_SIZE - 1; xx++) {
  137.             for (yy = 1; yy < PIC_Y_SIZE - 1; yy++) {
  138.                
  139.                 if ((pic[yy][xx] == EMPTY) && (
  140.                      (pic[yy][xx - 1] == FILLED) ||
  141.                      (pic[yy - 1][xx] == FILLED) ||
  142.                      (pic[yy][xx + 1] == FILLED) ||
  143.                      (pic[yy + 1][xx] == FILLED)
  144.                     )
  145.                    ) {
  146.                     pic[yy][xx] = FILLED;
  147.                     changes++;
  148.                 }  
  149.                
  150.             }  
  151.         }
  152.        
  153.        
  154.     } while (changes); 
  155.  
  156. }  
  157.  
  158.  
  159. // Up and down, fill left to right
  160. // Assumes the picture array has a WALL frame, and that the outside-call x and y are inside the frame
  161. // Also that x, y is EMPTY!
  162. void localFill(const int x, int y) {
  163.    
  164.     int xx;
  165.    
  166.     while (EMPTY == pic[y-1][x]) y--;
  167.     while (EMPTY == pic[y][x]) {
  168.    
  169.         for (xx = x ; EMPTY == pic[y][xx] ; xx--) {
  170.             pic[y][xx] = FILLED;
  171.         }
  172.         for (xx = x + 1 ; EMPTY == pic[y][xx] ; xx++) {
  173.             pic[y][xx] = FILLED;
  174.         }
  175.         y++;
  176.        
  177.     }  
  178.    
  179. }
  180.  
  181.  
  182. void floodFill_Smart(const int x, const int y) {
  183.    
  184.     int changes, xx, yy;
  185.  
  186.     localFill(x, y);
  187.     do {
  188.  
  189.         calls++;       
  190.         changes = 0;
  191.         for (xx = 1; xx < PIC_X_SIZE - 1; xx++) {
  192.             for (yy = 1; yy < PIC_Y_SIZE - 1; yy++) {
  193.                
  194.                 if ((pic[yy][xx] == EMPTY) && (
  195.                      (pic[yy - 1][xx] == FILLED) ||
  196.                      (pic[yy + 1][xx] == FILLED)
  197.                     )
  198.                    ) {
  199.                     localFill(xx, yy);
  200.                     changes++;
  201.                 }  
  202.                
  203.             }  
  204.         }
  205.        
  206.        
  207.     } while (changes); 
  208.        
  209. }  
  210.  
  211.  
  212. void drawPic(void) {
  213.    
  214.     int y;
  215.     for (y = 0; y < PIC_Y_SIZE; y++) {
  216.         printf("%s\n", pic[y]);
  217.     }  
  218.     printf("\n");
  219.    
  220. }  
  221.  
  222.  
  223. void clearPic(void) {
  224.    
  225.     int x, y;
  226.    
  227.     for (x = 1; x < PIC_X_SIZE - 1; x++) {
  228.         for (y = 1; y < PIC_Y_SIZE - 1; y++) {
  229.             pic[y][x] = EMPTY;
  230.         }
  231.     }  
  232.    
  233. }  
  234.  
  235.  
  236. int main(void) {
  237.    
  238.     drawPic();
  239.     rDepth = 0;
  240.     calls = 0;
  241.     tStart = clock();
  242.     //floodFill_R_Pixel(40, 12, 1);
  243.     //floodFill_R_Line(40, 12, 1); 
  244.     //floodFill_Pixel(40, 12);
  245.     floodFill_Smart(40, 12);
  246.     tEnd = clock();
  247.     drawPic();
  248.     printf("%d function calls, maximal recursion depth was %d\n", calls, rDepth);
  249.     return 0;
  250.    
  251. }      
  252.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement