home *** CD-ROM | disk | FTP | other *** search
/ Lion Share / lionsharecd.iso / dos_misc / sglass.zip / SGLASS.C next >
C/C++ Source or Header  |  1988-05-14  |  7KB  |  211 lines

  1.  
  2. /*
  3.  *  SGLASS.C - Written by  the Schafts:
  4.  *                              Debbie & George
  5.  *                            SCREEN 9 Productions
  6.  *
  7.  ************************************************************
  8.  ************************************************************
  9.  *
  10.  *      This is a test of recursion in C. It draws a figure, changes
  11.  *      the size, and then redraws it. The redraw is done by a
  12.  *      recursive call. It draws diamonds and boxes on a blue screen.
  13.  *
  14.  *
  15. */
  16. #include <math.h>
  17. #include <conio.h>
  18. #include <graph.h>
  19. #include <stdio.h>
  20.  
  21. #define ESC  27
  22. int RR;                          /* ret area for random() subroutine   */
  23. int box_point(int,int,int);      /* Recursive subroutine               */
  24. int box_box(int,int,int);        /* Same                               */
  25. int getkey(void);                /* Get decoded keypress no echo       */
  26. int random(int);                 /* Generate Random Number 0 <= RR < n */
  27. int HC,CH;                       /* ret areas for color and char       */
  28. int max_col,max_row,limit,size,osize,distfact,x,y;
  29. int in_size,o_size,unoo,onoo;
  30.  
  31. /**********************************************************************/
  32. /*                        main                                        */
  33. /**********************************************************************/
  34.  
  35. main(void)
  36. {
  37.  
  38.     HC = 0;                     /* HC is used for setting the color */
  39.  
  40.     /* CHANGE THE NEXT TWO CONSTANTS FOR NON-EGA        */
  41.  
  42.     max_col = 640;              /* max col on EGA */
  43.     max_row = 350;              /* max row        */
  44.  
  45.     x = max_col/2;          /* middle       */
  46.     y = max_row/2;
  47.  distfact = 2;                  /* multiplier for distance */
  48.      size = 75;                 /* max size to grow to     */
  49.     osize = 50;                 /* same                    */
  50.     limit = 1;                  /* min. size of box - one row/col */
  51.  
  52.     in_size = 1;                /* starting size                */
  53.     o_size  = 49;               /* same                         */
  54.     unoo = 1;                   /* one - changed from + to -    */
  55.     onoo = 1;                   /* same, for other drawing      */
  56.  
  57.     /* CHANGE THE CONSTANTS BELOW FOR NON-EGA           */
  58.  
  59.      _setvideomode(_ERESCOLOR);     /* set to EGA high res  */
  60.      _setbkcolor(_LIGHTBLUE);       /* set background to _..*/
  61.      _clearscreen(_GCLEARSCREEN);   /* clear screen         */
  62.  
  63.  
  64.      HC = 1;                        /* starting color [BLUE] */
  65.  
  66.  
  67.  
  68. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  69.  
  70.  
  71.  
  72.     while (1)                       /* until key hit        */
  73.       {
  74.      HC = random(16);               /* get random color             */
  75.      _setcolor(HC);                 /* set color for DIAMOND        */
  76.      box_point(x,y,in_size);        /* call diamond draw            */
  77.      in_size += unoo;               /* add [ or subtract ] one      */
  78.      if(in_size == size) unoo = -1; /* if max, switch to subtract   */
  79.      if(in_size == 1)    unoo = 1;  /* if min, switch to add        */
  80.  
  81.      HC = random(16);               /* get random color             */
  82.      _setcolor(HC);                 /* set color for BOX            */
  83.      box_box(x,y,o_size);           /* call box draw                */
  84.      o_size += onoo;               /* add[or subtract] one          */
  85.      if(o_size == osize) onoo = -1; /* if max, set to subtract      */
  86.      if(o_size == 1)     onoo = 1;  /* if min, set to add           */
  87.  
  88.  
  89.  
  90.      if (kbhit() )                  /* check for keyboard hits      */
  91.       {
  92.        CH = getkey();
  93.        if (toupper(CH) == 'P')
  94.         CH = getkey();              /*    pause   */
  95.         if (CH == ESC)              /*    done    */
  96.         {
  97.         _setvideomode(_DEFAULTMODE);
  98.         exit (0);
  99.         }
  100.        }
  101. /*  end the while and main                                         */
  102.       }
  103.  
  104. }
  105.  
  106.  
  107. /*******************************************************************/
  108. /*                         box_box  -  draw boxes                  */
  109.  
  110.  
  111. box_box(col_inpt,row_inpt,siz)
  112.     int col_inpt,row_inpt,siz;
  113. {
  114.     int newsz,d,delay;
  115.     newsz=d=0;
  116.  
  117.  
  118.                         /* check for limits */
  119.     if (         siz > limit   &&
  120.         col_inpt+siz < max_col &&
  121.         col_inpt-siz > 0       &&
  122.         row_inpt+siz < max_row &&
  123.         row_inpt-siz > 0 )
  124.          {
  125.  
  126.  
  127.             _moveto(col_inpt+siz,row_inpt-siz);         /* draw box*/
  128.             _lineto(col_inpt+siz,row_inpt+siz);
  129.             _lineto(col_inpt-siz,row_inpt+siz);
  130.             _lineto(col_inpt-siz,row_inpt-siz);
  131.             _lineto(col_inpt+siz,row_inpt-siz);
  132.  
  133.  
  134.  
  135.         d = distfact*siz;       /* d represents 2 * curr size   */
  136.         newsz = siz/2;          /* halve the size of the box    */
  137.  
  138.     /* The following is the recursive part of the call              */
  139.  
  140.         box_box(col_inpt,row_inpt-d,newsz);   /* to the top   */
  141.         box_box(col_inpt,row_inpt+d,newsz);   /* to the bottom*/
  142.         box_box(col_inpt+d,row_inpt,newsz);   /* to the right */
  143.         box_box(col_inpt-d,row_inpt,newsz);   /* to the left  */
  144.          }
  145. }
  146.  
  147. /*********************************************************************/
  148. /*                         box_point  - draw diamonds                */
  149. /*                     [same as box_box, essentially                 */
  150.  
  151.  
  152.  
  153.  
  154. box_point(ccol,rrow,ssiz)
  155.       int ccol,rrow,ssiz;
  156. {
  157.     int news,dd,ddly;
  158.     news=dd=ddly=0;
  159.  
  160.     if(     ssiz > limit   &&
  161.        ccol+ssiz < max_col &&
  162.        ccol-ssiz > 0       &&
  163.        rrow+ssiz < max_row &&
  164.        rrow-ssiz > 0)
  165.      {
  166.  
  167.        _moveto(ccol+ssiz,rrow);             /* draw diamond */
  168.        _lineto(ccol,rrow+ssiz);
  169.        _lineto(ccol-ssiz,rrow);
  170.        _lineto(ccol,rrow-ssiz);
  171.        _lineto(ccol+ssiz,rrow);
  172.  
  173.  
  174.        dd = distfact*ssiz;
  175.        news = ssiz/2;
  176.  
  177.        box_point(ccol,rrow+dd,news);        /* to the bottom*/
  178.        box_point(ccol,rrow-dd,news);        /* to the top   */
  179.        box_point(ccol-dd,rrow,news);        /* to the left  */
  180.        box_point(ccol+dd,rrow,news);        /* to the right */
  181.  
  182.      }
  183. }
  184.  
  185.  
  186. /*              M I S C.   S U B R O U T I N E S                        */
  187. /*************************  getkey  -  get key press********************/
  188.  
  189. getkey()                  /* Get decoded keypress no echo */
  190. {
  191.    int CH;
  192.  
  193.    CH = getch();
  194.    if (CH == 0)
  195.       CH = 1000 + getch();
  196.    return(CH);
  197. }
  198.  
  199. /**************************  random  -  generate random nbr  ************/
  200.  
  201. int random(RR)                 /* Generate Random Number 0-RR */
  202. int RR;
  203. {
  204.    int s;
  205.    long t;
  206.  
  207.    s = rand();                  /* get nbr 0 - 32768            */
  208.    t = (long) s * RR / 32768;   /* mult it by input, divide by max */
  209.    return((int) t);             /* ret nbr between 0 and input nbr */
  210. }
  211.