fork download
  1. #include <stdio.h>
  2.  
  3. /*
  4.   (2w+2h-4)/0 1 .. w-2 w-1
  5.   +---------------+
  6.   2w+2h-5 | | w
  7.   . | | .
  8.   . | | .
  9.   . | | .
  10.   2w+h-2 | | w+h-3
  11.   +---------------+
  12.   2w+h-3 2w+h-4 .. w+h-1 w+h-2
  13. */
  14.  
  15. int EnvelopeLength(int w, int h)
  16. {
  17. if (w <= 0 || h <= 0)
  18. return 0;
  19.  
  20. if (h == 1)
  21. return w;
  22.  
  23. if (w == 1)
  24. return h;
  25.  
  26. return 2 * (w + h) - 4;
  27. }
  28.  
  29. #if 0 // this function is currently unused
  30. int Coords2EnvelopePosition(int x, int y, int w, int h)
  31. {
  32. int pos = -1;
  33. if (w <= 0 || h <= 0)
  34. pos = -1;
  35. else if (w == 1 && h == 1)
  36. pos = 0;
  37. else if (h == 1)
  38. pos = x;
  39. else if (w == 1)
  40. pos = y;
  41. else if (y == 0)
  42. pos = x;
  43. else if (x == w - 1)
  44. pos = w + y - 1;
  45. else if (y == h - 1)
  46. pos = 2 * w + h - x - 3;
  47. else if (x == 0)
  48. pos = 2 * w + 2 * h - y - 4;
  49. return pos;
  50. }
  51. #endif
  52.  
  53. void EnvelopePosition2Coords(int* px, int* py, int w, int h, int pos)
  54. {
  55. *py = *px = -1;
  56. if (w <= 0 || h <= 0)
  57. *py = *px = -1;
  58. else if (w == 1 && h == 1)
  59. *py = *px = 0;
  60. else if (h == 1)
  61. *px = pos, *py = 0;
  62. else if (w == 1)
  63. *px = 0, *py = pos;
  64. else if (pos < w)
  65. *px = pos, *py = 0;
  66. else if (pos <= w + h - 2)
  67. *px = w - 1, *py = pos - w + 1;
  68. else if (pos <= 2 * w + h - 3)
  69. *px = 2 * w + h - 3 - pos, *py = h - 1;
  70. else if (pos <= 2 * w + 2 * h - 5)
  71. *px = 0, *py = 2 * w + 2 * h - 4 - pos;
  72. }
  73.  
  74. void SpiralShift(char* a, int w, int h)
  75. {
  76. int w0 = w;
  77.  
  78. while (w > 0 && h > 0)
  79. {
  80. int len = EnvelopeLength(w, h), pos;
  81. int xto, yto, xfrom, yfrom;
  82.  
  83. for (pos = 0; pos < len - 1; pos++)
  84. {
  85. EnvelopePosition2Coords(&xto, &yto, w, h, pos);
  86. EnvelopePosition2Coords(&xfrom, &yfrom, w, h, pos + 1);
  87. a[yto * w0 + xto] = a[yfrom * w0 + xfrom];
  88. }
  89.  
  90. EnvelopePosition2Coords(&xto, &yto, w, h, len - 1);
  91.  
  92. if (w > 2 && h > 2)
  93. {
  94. EnvelopePosition2Coords(&xfrom, &yfrom, w - 2, h - 2, 0);
  95. a[yto * w0 + xto] = a[w0 + 1 + yfrom * w0 + xfrom];
  96. w -= 2;
  97. h -= 2;
  98. a += w0 + 1;
  99. }
  100. else
  101. {
  102. a[yto * w0 + xto] = ' ';
  103. break;
  104. }
  105. }
  106. }
  107.  
  108. void PrintSpiral(const char*s, int w, int h)
  109. {
  110. int x, y;
  111. puts("Spiral:");
  112. for (y = 0; y < h; y++)
  113. {
  114. for (x = 0; x < w; x++)
  115. printf("%c ", s[y * w + x]);
  116. puts("");
  117. }
  118. puts("");
  119. }
  120.  
  121. int main(void)
  122. {
  123. char spiral1x1[1 * 1] =
  124. "a";
  125. char spiral2x2[2 * 2] =
  126. "wo"
  127. "dr";
  128. char spiral5x6[5 * 6] =
  129. "This i"
  130. "ing ss"
  131. "lce.e "
  132. "lnetna"
  133. "arips ";
  134. int i;
  135.  
  136. PrintSpiral(spiral1x1, 1, 1);
  137. for (i = 0; i < 1 * 1; i++)
  138. {
  139. SpiralShift(spiral1x1, 1, 1);
  140. PrintSpiral(spiral1x1, 1, 1);
  141. }
  142.  
  143. PrintSpiral(spiral2x2, 2, 2);
  144. for (i = 0; i < 2 * 2; i++)
  145. {
  146. SpiralShift(spiral2x2, 2, 2);
  147. PrintSpiral(spiral2x2, 2, 2);
  148. }
  149.  
  150. PrintSpiral(spiral5x6, 6, 5);
  151. for (i = 0; i < 5 * 6; i++)
  152. {
  153. SpiralShift(spiral5x6, 6, 5);
  154. PrintSpiral(spiral5x6, 6, 5);
  155. }
  156. return 0;
  157. }
  158.  
Success #stdin #stdout 0s 1788KB
stdin
Standard input is empty
stdout
Spiral:
a 

Spiral:
  

Spiral:
w o 
d r 

Spiral:
o r 
  d 

Spiral:
r d 
    

Spiral:
d   
    

Spiral:
    
    

Spiral:
T h i s   i 
i n g   s s 
l c e . e   
l n e t n a 
a r i p s   

Spiral:
h i s   i s 
n g   s e   
i e .   n a 
l c n e t   
l a r i p s 

Spiral:
i s   i s   
g   s e n a 
n .     t   
i e c n e s 
l l a r i p 

Spiral:
s   i s   a 
  s e n t   
g       e s 
n . e c n p 
i l l a r i 

Spiral:
  i s   a   
s e n t e s 
        n p 
g   . e c i 
n i l l a r 

Spiral:
i s   a   s 
e n t e n p 
s       c i 
      . e r 
g n i l l a 

Spiral:
s   a   s p 
n t e n c i 
e       e r 
s       . a 
  g n i l l 

Spiral:
  a   s p i 
t e n c e r 
n       . a 
e         l 
s   g n i l 

Spiral:
a   s p i r 
e n c e . a 
t         l 
n         l 
e s   g n i 

Spiral:
  s p i r a 
n c e .   l 
e         l 
t         i 
n e s   g n 

Spiral:
s p i r a l 
c e .     l 
n         i 
e         n 
t n e s   g 

Spiral:
p i r a l l 
e .       i 
c         n 
n         g 
e t n e s   

Spiral:
i r a l l i 
.         n 
e         g 
c           
n e t n e s 

Spiral:
r a l l i n 
          g 
.           
e         s 
c n e t n e 

Spiral:
a l l i n g 
            
          s 
.         e 
e c n e t n 

Spiral:
l l i n g   
          s 
          e 
          n 
. e c n e t 

Spiral:
l i n g   s 
          e 
          n 
          t 
  . e c n e 

Spiral:
i n g   s e 
          n 
          t 
          e 
    . e c n 

Spiral:
n g   s e n 
          t 
          e 
          n 
      . e c 

Spiral:
g   s e n t 
          e 
          n 
          c 
        . e 

Spiral:
  s e n t e 
          n 
          c 
          e 
          . 

Spiral:
s e n t e n 
          c 
          e 
          . 
            

Spiral:
e n t e n c 
          e 
          . 
            
            

Spiral:
n t e n c e 
          . 
            
            
            

Spiral:
t e n c e . 
            
            
            
            

Spiral:
e n c e .   
            
            
            
            

Spiral:
n c e .     
            
            
            
            

Spiral:
c e .       
            
            
            
            

Spiral:
e .         
            
            
            
            

Spiral:
.           
            
            
            
            

Spiral: