home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / vifs / rifs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-14  |  5.1 KB  |  230 lines

  1. static char *header = "\n\
  2.                                                                             \n\
  3. /*                                                                          \n\
  4.  *    rifs.c  --  Random Iterated Function System.                        \n\
  5.  *    Theory from M. Barnsley 'Fractals Everywhere' (ISBN 0-12-079062-9). \n\
  6.  *                                                                          \n\
  7.  *    4 june 1989  Olle Olsson  (olle@ergodata.se).                       \n\
  8.  */                                                                         \n\
  9.                                                                             \n\
  10.                                                                             \n\
  11. ";
  12.  
  13. #include <conio.h>
  14. #include <process.h>
  15. #include <time.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <dos.h>
  19. #include "ifs.h"
  20.  
  21.  
  22. /* max number of transforms */
  23. #define NTRANSF 50
  24.  
  25. /* max group index */
  26. #define MAXGROUP (NTRANSF/2)
  27.  
  28. /* global trace flag */
  29. int trace;
  30.  
  31. /* the transformations */
  32. static transform trf[NTRANSF];    /* (could have been a linked list...) */
  33.  
  34. /* the colors (palette) */
  35. static rgb colors[MAXCOLORS + 1];
  36.  
  37. /* locals */
  38. void usage( void );
  39.  
  40. void usage()
  41. {
  42. printf( "Usage:\n\trifs options\n\n" );
  43. printf( "options are:\n" );
  44. printf( "\t-l number\tlower limit for transformation count\n" );
  45. printf( "\t-u number\tupper limit for transformation count\n" );
  46. printf( "\t-d number\tdensity scaling factor (float)\n" );
  47. printf( "\t-n\t\tshow number of currently active transformations\n" );
  48. printf( "\nwhen running: space bar gives another ifs.\n" );
  49. printf( "\n" );
  50. }
  51.  
  52. void main( argc, argv )
  53. int argc;
  54. char *argv[];
  55. {
  56. ifsdes dd;            /* ifs descriptor */
  57. int minsize = 2;        /* minimum number of transformations */
  58. int maxsize = 8;        /* maximum number of transformations */
  59. int size;            /* current number of transformations */
  60. double cscale = 1;        /* count scale (density) */
  61. long count;            /* iterations to do */
  62. int shown = 0;            /* show number of transformations flag */
  63. long tm;            /* time */
  64. int running;            /* status */
  65. rgb *rp;            /* tmp */
  66. char buf[100];            /* tmp */
  67. int i;                /* tmp */
  68. int c;
  69. char *ap, **p;            /* argument pointers */
  70.  
  71. /* advertise */
  72. printf( header );
  73. sleep( 1 );
  74.  
  75.  
  76. /* initialize */
  77. dd.tp = trf;
  78. dd.maxsize = NTRANSF;
  79. dd.colors = colors;
  80. dd.clrsize = sizeof (colors) / sizeof (colors[0]);
  81. dd.textcolor = dd.clrsize - 1;
  82. dd.prrdens = 0;
  83. dd.maxgroup = MAXGROUP;
  84. dd.im = 0;
  85.  
  86. /* set text color */
  87. rp = &dd.colors[dd.textcolor];
  88. rp -> r = rp -> g = rp -> b = 2;
  89.  
  90. /* setup */
  91. srand( time( &tm ));
  92. initshow();
  93.  
  94. /* read arguments */
  95. for (p = argv + 1, c = 1; c < argc; ++c)
  96.     {
  97.     if (trace) printf( "%s:\n", *p );
  98.     ap = *p++;
  99.  
  100.     if (*ap == '-')    for (i = 1; ap[i]; ++i) switch (ap[i])
  101.         {
  102.         case 'd':
  103.             if (++c >= argc)
  104.                 error( "-d: density factor missing");
  105.  
  106.             if ((cscale = atof( *p++ ) ) <= 0)
  107.                 error( "density %g is too low", cscale );
  108.  
  109.             continue;
  110.  
  111.         case 'l':
  112.             if (++c >= argc)
  113.                 error(
  114.              "-l: lower limit for transformation count missing");
  115.  
  116.             if ((minsize = atoi( *p++ ) ) < 2)
  117.                 error( "low limit %d is too low", minsize );
  118.  
  119.             if (maxsize < minsize) maxsize = minsize;
  120.  
  121.             continue;
  122.  
  123.         case 'u':
  124.             if (++c >= argc)
  125.                 error(
  126.              "-u: upper limit for transformation count missing");
  127.  
  128.             if ((maxsize = atoi( *p++ ) ) < 2)
  129.                 error( "upper limit %d is too low", maxsize );
  130.  
  131.             if (minsize > maxsize) minsize = maxsize;
  132.  
  133.             continue;
  134.  
  135.         case 't':
  136.             trace++;
  137.             continue;
  138.  
  139.         case 'n':
  140.             shown++;
  141.             continue;
  142.  
  143.         case 'i':
  144.             dd.im++;
  145.             continue;
  146.  
  147.         default:
  148.             usage();
  149.             error( "don't understand flag '-%c'", ap[i] );
  150.         }
  151.     else
  152.         {
  153.         usage();
  154.         error( "don't understand argument '%s'", ap );
  155.         }
  156.     }
  157.  
  158. for (running = 1; running;)
  159.     {
  160.     setcolor( dd.textcolor );
  161.  
  162.     /* make a random ifs (area and transforms) */
  163.     size = ifsrand( &dd, minsize, maxsize, trace );
  164.  
  165.     /* show trace printouts from ifsrand()  */
  166.     if (trace) sleep( 5 );
  167.  
  168.     /* randrgb( &dd.colors[0] ); */
  169.     /* black background */
  170.     dd.colors[0].r = dd.colors[0].g = dd.colors[0].b = 0;
  171.  
  172.     /* clear & set colors */
  173.     cleardevice();
  174.     palsetup( &dd );
  175.  
  176.     /* show it */
  177.     for (dd.size = minsize; dd.size <= size; ++dd.size)
  178.         {
  179.         if (shown)
  180.             {
  181.             /* show number of transforms */
  182.             moveto( 0, 0 );
  183.             sprintf( buf, "%d", dd.size );
  184.             setcolor( dd.textcolor );
  185.             gputs( buf );
  186.             }
  187.  
  188.         count = 2000 * cscale;
  189.         dd.density = ITER2DENS( count );
  190.         ifsshow( &dd, kbhit, 1, putpixel, getpixel );
  191.  
  192.         /* stop ? */
  193.         if (kbhit()) if (getch() != ' ')
  194.             {
  195.             running = 0;
  196.             break;
  197.             }
  198.         }
  199.  
  200.     /* stop ? */
  201.     if (kbhit()) if (getch() != ' ')
  202.         running = 0;
  203.     }
  204.  
  205. /* get any key (no echo) */
  206. /*getch();*/
  207.  
  208. endshow();
  209.  
  210. exit( 0 );
  211. }
  212.  
  213.  
  214. void error( s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 )
  215. char *s;
  216. int v1, v2, v3, v4, v5, v6, v7, v8, v9, v10;
  217. {
  218. fprintf( stderr, "\nError:" );
  219. fprintf( stderr, s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 );
  220. fprintf( stderr, "\n" );
  221.  
  222. sleep( 5 );
  223. /* this will clear the screen */
  224. endshow();
  225.  
  226. exit( 2 );
  227. }
  228.  
  229.  
  230.