fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. // plane size
  5. #define FSIZE_X 160
  6. #define FSIZE_Y 80
  7.  
  8. #define SPACE_CH ' '
  9.  
  10. int draw_plane(char *p, int x, int y, char *str) {
  11. int len, i;
  12.  
  13. len = strlen(str);
  14. if ((x < 0) || (x + len - 1 >= FSIZE_X) ||
  15. (y < 0) || (y >= FSIZE_Y)) return 1;
  16. for (i = 0; i < len; i++) {
  17. p[y * FSIZE_X + x + i] = str[i];
  18. }
  19. return 0;
  20. }
  21.  
  22. int print_plane(char *p) {
  23. int x1, y1, x2, y2, x, y, i;
  24. char str[FSIZE_X + 1];
  25.  
  26. x1 = FSIZE_X - 1;
  27. x2 = 0;
  28. y1 = FSIZE_Y - 1;
  29. y2 = 0;
  30.  
  31. for (x = 0; x < FSIZE_X; x++) {
  32. for (y = 0; y < FSIZE_Y; y++) {
  33. if (p[y * FSIZE_X + x] != SPACE_CH) {
  34. if (x < x1) x1 = x;
  35. if (y < y1) y1 = y;
  36. if (x > x2) x2 = x;
  37. if (y > y2) y2 = y;
  38. }
  39. }
  40. }
  41.  
  42. for (y = y1; y <= y2; y++) {
  43. i = 0;
  44. for (x = x1; x <= x2; x++) {
  45. str[i] = p[y * FSIZE_X + x];
  46. i++;
  47. }
  48. str[i] = 0;
  49. printf("%s\n", str);
  50. }
  51. return 0;
  52. }
  53.  
  54. int draw_hex(int n) {
  55. char p[FSIZE_X * FSIZE_Y];
  56. int md[6] = { 1, 0, 1, 1, 1, 1};
  57. int xd[6] = { 1, -1, -4, -1, 1, 0};
  58. int yd[6] = { -1, -1, 0, 1, 1, 0};
  59. int xad[6] = { 1, -1, 0, -1, 1, 4};
  60. int yad[6] = { -1, -1, 0, 1, 1, 0};
  61. char *ach[6] = {"/", "\\", "----", "/", "\\", "----"};
  62. int x, y, xp, yp, m, co, i;
  63. char str[32];
  64.  
  65. for (i = 0; i < FSIZE_X * FSIZE_Y; i++) {
  66. p[i] = SPACE_CH;
  67. }
  68.  
  69. x = FSIZE_X / 2;
  70. y = FSIZE_Y / 2;
  71. m = 0;
  72. co = 0;
  73. for (i = 0; i < n; i++) {
  74. xp = x;
  75. yp = y;
  76.  
  77. while(md[m] == 0) {
  78. md[m]++;
  79. m++;
  80. m = m % 6;
  81. }
  82.  
  83. if (co == 0) {
  84. co = md[m];
  85. }
  86.  
  87. x += xd[m];
  88. y += yd[m];
  89. draw_plane(p, x, y, ach[m]);
  90. x += xad[m];
  91. y += yad[m];
  92.  
  93. snprintf(str, sizeof(str) - 1, "%d", i);
  94. draw_plane(p, xp - strlen(str) + 1, yp, str);
  95.  
  96. co--;
  97. if (co == 0) {
  98. md[m]++;
  99. m++;
  100. m = m % 6;
  101. }
  102. }
  103. snprintf(str, sizeof(str) - 1, "%d", i);
  104. draw_plane(p, x - strlen(str) + 1, y, str);
  105.  
  106. printf("n=%d\n", n);
  107. print_plane(p);
  108. printf("\n");
  109. return 0;
  110. }
  111.  
  112. int main(void) {
  113. // your code goes here
  114. draw_hex(0);
  115. draw_hex(6);
  116. draw_hex(32);
  117. draw_hex(99);
  118. draw_hex(200);
  119. return 0;
  120. }
  121.  
Success #stdin #stdout 0s 4488KB
stdin
Standard input is empty
stdout
n=0
0

n=6
  2---1  
 /   /   
3   0   6
 \     / 
  4---5  

n=32
      24--23--22--21    
      /             \   
    25  10---9---8  20  
    /   /         \   \ 
  26  11   2---1   7  19
  /   /   /   /   /   / 
27  12   3   0   6  18  
  \   \   \     /   /   
  28  13   4---5  17    
    \   \         /     
    29  14--15--16      
      \                 
      30--31--32        

n=99
                    99--98--97--96          
                                  \         
          70--69--68--67--66--65  95        
          /                     \   \       
        71  44--43--42--41--40  64  94      
        /   /                 \   \   \     
      72  45  24--23--22--21  39  63  93    
      /   /   /             \   \   \   \   
    73  46  25  10---9---8  20  38  62  92  
    /   /   /   /         \   \   \   \   \ 
  74  47  26  11   2---1   7  19  37  61  91
  /   /   /   /   /   /   /   /   /   /   / 
75  48  27  12   3   0   6  18  36  60  90  
  \   \   \   \   \     /   /   /   /   /   
  76  49  28  13   4---5  17  35  59  89    
    \   \   \   \         /   /   /   /     
    77  50  29  14--15--16  34  58  88      
      \   \   \             /   /   /       
      78  51  30--31--32--33  57  87        
        \   \                 /   /         
        79  52--53--54--55--56  86          
          \                     /           
          80--81--82--83--84--85            

n=200
                184-183-182-181-180-179-178-177-176              
                 /                                 \             
              185 140-139-138-137-136-135-134-133 175            
               /   /                             \   \           
            186 141 102-101-100--99--98--97--96 132 174          
             /   /   /                         \   \   \         
          187 142 103  70--69--68--67--66--65  95 131 173        
           /   /   /   /                     \   \   \   \       
        188 143 104  71  44--43--42--41--40  64  94 130 172      
         /   /   /   /   /                 \   \   \   \   \     
      189 144 105  72  45  24--23--22--21  39  63  93 129 171    
       /   /   /   /   /   /             \   \   \   \   \   \   
    190 145 106  73  46  25  10---9---8  20  38  62  92 128 170  
     /   /   /   /   /   /   /         \   \   \   \   \   \   \ 
  191 146 107  74  47  26  11   2---1   7  19  37  61  91 127 169
   /   /   /   /   /   /   /   /   /   /   /   /   /   /   /   / 
192 147 108  75  48  27  12   3   0   6  18  36  60  90 126 168  
   \   \   \   \   \   \   \   \     /   /   /   /   /   /   /   
  193 148 109  76  49  28  13   4---5  17  35  59  89 125 167    
     \   \   \   \   \   \   \         /   /   /   /   /   /     
    194 149 110  77  50  29  14--15--16  34  58  88 124 166      
       \   \   \   \   \   \             /   /   /   /   /       
      195 150 111  78  51  30--31--32--33  57  87 123 165        
         \   \   \   \   \                 /   /   /   /         
        196 151 112  79  52--53--54--55--56  86 122 164          
           \   \   \   \                     /   /   /           
          197 152 113  80--81--82--83--84--85 121 163            
             \   \   \                         /   /             
            198 153 114-115-116-117-118-119-120 162              
               \   \                             /               
              199 154-155-156-157-158-159-160-161                
                 \                                               
                200