home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource3 / 196_01 / sierp.c < prev    next >
Text File  |  1985-11-15  |  3KB  |  221 lines

  1. /* [SIERP.C of JUGPDS Vol.19]
  2. *****************************************************************
  3. *                                *
  4. *    Written by  Hakuo Katayose (JUG-CP/M No.179)        *
  5. *            49-114 Kawauchi-Sanjuunin-machi        *
  6. *                Sendai, Miyagi 980                          *
  7. *            Phone: 0222-61-3219                *
  8. *                                *
  9. *    Edited by Y. Monma (JUG-C/M Disk Editor)                * 
  10. *                                *
  11. *****************************************************************
  12. */
  13.  
  14. #define MAXLINE 64
  15. #define MAXLEN MAXLINE*8
  16. #define ESC 0x1b
  17. #define H0 512
  18.  
  19. int    sx, sy, h, x, y;
  20. char    pattarn[MAXLINE][MAXLEN];
  21.  
  22. main()
  23. {
  24.     int    level;
  25.     char    s;
  26.  
  27.     printf( "\nLEVEL : " );
  28.     s = getchar();
  29.     level = s -'0';
  30.     initpat();
  31.     writepat(makepat(level));
  32.     finish();
  33. }    
  34.  
  35.  
  36. makepat(mlevel)
  37. int    mlevel;
  38. {
  39.     int    n, i, x0, y0;
  40.  
  41.     i = 0;    h = H0 / 4;    x0 = h + h;    y0 = h + h + h;
  42.     for(i = 1; i <= mlevel; i++) {
  43.         x0 -= h;
  44.         h /= 2;
  45.         y0 += h;
  46.         sx = x = x0; sy = y = y0;
  47.         a(i);    x += h;  y -= h; plot();
  48.         b(i);    x -= h;  y -= h; plot();
  49.         c(i);    x -= h;  y += h; plot();
  50.         d(i);    x += h;  y += h; plot();
  51.     }
  52.     return(MAXLINE);
  53. }
  54.  
  55.  
  56. a(i)
  57. int    i;
  58. {
  59.     if (i > 0) {
  60.         a(i-1);    x += h;    y -= h;    plot();
  61.         b(i-1);    x += h + h;    plot();
  62.         d(i-1);    x += h;    y += h;    plot();
  63.         a(i-1);
  64.     }
  65. }
  66.  
  67.  
  68. b(i)
  69. int    i;
  70. {
  71.     if (i > 0) {
  72.         b(i-1);    x -= h;    y -= h;    plot();
  73.         c(i-1);    y -= h + h;    plot();
  74.         a(i-1);    x += h;    y -= h;    plot();
  75.         b(i-1);
  76.     }
  77. }
  78.  
  79.  
  80. c(i)
  81. int    i;
  82. {
  83.     if (i > 0) {
  84.         c(i-1);    x -= h;    y += h;    plot();
  85.         d(i-1);    x -= h + h;    plot();
  86.         b(i-1);    x -= h;    y -= h;    plot();
  87.         c(i-1);
  88.     }
  89. }
  90.  
  91.  
  92. d(i)
  93. int    i;
  94. {
  95.     if (i > 0) {
  96.         d(i-1);    x += h;    y += h;    plot();
  97.         a(i-1);    y += h + h;    plot();
  98.         c(i-1);    x -= h;    y += h;    plot();
  99.         d(i-1);
  100.     }
  101. }
  102.  
  103.  
  104. plot()
  105. {
  106.     line(sx,sy,x,y);
  107.     sx = x;
  108.     sy = y;
  109. }
  110.  
  111.  
  112.  
  113. boxplot(x1, x2, y1, y2)
  114. int    x1, x2, y1, y2;
  115. {
  116.     line(x1,y1,x1,y2);
  117.     line(x1,y2,x2,y2);
  118.     line(x2,y2,x2,y1);
  119.     line(x2,y1,x1,y1);
  120. }
  121.  
  122.  
  123. line(x1, y1, x2, y2)
  124. int    x1, y1, x2, y2;
  125. {
  126.     unsigned    dxx, dyy, dd;
  127.     int        xx, dx, dy, x, y;
  128.  
  129.     dy = y2 - y1;
  130.     dx = x2 - x1;
  131.     dyy = abs(dy);
  132.     dxx = abs(dx);
  133.     if (dyy > dxx) {
  134.         if (y1 > y2) {
  135.             y  = y1;    y1 = y2;    y2 = y;
  136.             x  = x1;    x1 = x2;    x2 = x;
  137.         }
  138.         for (y = y1; y < y2; y++) {
  139.             dd = (y - y1);
  140.             xx = (dd * dxx / dyy);
  141.             xx = x2 > x1 ? xx : -xx;
  142.             plotdot(x1 + xx, y);
  143.         }
  144.     }
  145.     else {
  146.         if (x1 > x2) {
  147.             x  = x1;    x1 = x2;    x2 = x;
  148.             y  = y1;    y1 = y2;    y2 = y;
  149.         }
  150.         for (x = x1; x < x2; x++) {
  151.             dd = (x - x1);
  152.             xx = (dd * dyy / dxx);
  153.             xx = y2 > y1 ? xx : -xx;
  154.             plotdot(x, y1 + xx);
  155.         }
  156.     }
  157. }
  158.  
  159.  
  160. plotdot(x, y)
  161. int    x, y;
  162. {
  163.     pattarn[ y >> 3 ][ x ] |= ( 128 >> (y & 7) );
  164. }
  165.  
  166.  
  167.  
  168.  
  169. initpat()
  170. {
  171.     int    i, j;
  172.  
  173.     outc( ESC );
  174.     outc( 'A' );
  175.     outc(  8  );
  176.     for(i = 0; i < MAXLINE; i++)
  177.         for (j = 0; j < MAXLEN; j++)
  178.             pattarn[i][j] = 0;
  179. }
  180.  
  181.  
  182. finish()
  183. {
  184.     outc( ESC );
  185.     outc( '2' );
  186. }
  187.  
  188.  
  189.  
  190. writepat(nlines)
  191. int    nlines;
  192. {
  193.     int    i;
  194.  
  195.     for (i = 0; i < nlines; i++)
  196.         imageout(pattarn[i]);
  197. }
  198.  
  199.  
  200. imageout(line)
  201. char    *line;
  202. {
  203.     int    i, maxlen;
  204.  
  205.     outc( ESC );
  206.     outc( 'L' );
  207.     outc( MAXLEN & 0x0ff );
  208.     outc( MAXLEN >> 8 );
  209.     maxlen = MAXLEN;
  210.     while (maxlen--)
  211.         outc(*line++);
  212.     outc( '\n' );
  213. }
  214.  
  215.  
  216. outc( ch )
  217. char    ch;
  218. {
  219.     bdos(5, ch);
  220. }
  221.