home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / microcrn / issue_39.arc / MAN-JUL.FIG < prev    next >
Text File  |  1987-12-02  |  9KB  |  303 lines

  1.  
  2.  
  3. /* MAN-JUL - generates Mandelbrot and Julia sets
  4.  
  5.  
  6.   This program supports an article on fractals in Micro Cornucopia issue #39.
  7. I've only taken the code far enough to allow convenient generation of the sets.
  8. The following problems WILL cause trouble for the sloppy typist:
  9.  
  10.   -no error checking on numeric inputs
  11.   -doesn't check for existing file when saving a screen
  12.   -doesn't check for existing file when viewing a saved file. This is
  13.    particularly ugly since the user gets a text error message while still
  14.    in graphics mode. Do a <cr> to recover.
  15.  
  16.   HERCLIB.C is required for compilation. As set up, it should be located in
  17. your main TURBOC sub-directory. HERCLIB is a collection of Hercules graphics
  18. functions from the same issue of Micro C.
  19.  
  20.   I used Turbo C version 1.5 compact model for compilation.
  21.  
  22.   This program will only run on Herc or Herc clone systems. To use it on a CGA
  23. system (waste of time due to poor resolution) or an EGA system (good idea -
  24. looks cool with all those colors) edit HERCLIB to use the BIOS graphics calls
  25. or call your own graphics library. Be sure to change max_colors to reflect the
  26. number of colors available on your system. Of course max_col and max_row need
  27. to be changed in HERCLIB as well.
  28.  
  29.  
  30.   12-1-87   Larry Fogg    Happy Holidays !!!  */
  31.  
  32.  
  33.  
  34. #include <stdio.h>
  35. #include "herclib.c"                               /* Herc graphics routines */
  36.  
  37. #define sqr(X) (X*X)
  38.  
  39. const int max_colors = 2;
  40.  
  41. char choice;
  42.  
  43.  
  44.  
  45. void first_screen ()                                               /* say hi */
  46. {
  47.   int i;
  48.  
  49.   clear_txt_scr ();
  50.   scr_write (0, 4,
  51. "                                  MAN-JUL                                    ",
  52.              reverse);
  53.   scr_write (0, 5,
  54. "                      Mandelbrot And Julia Set Generator                     ",
  55.              reverse);
  56.   scr_write (11, 24,
  57.              "Brought to you by Larry Fogg and Micro Cornucopia Magazine",
  58.              under_line);
  59.   sleep (4);
  60. }  /* first_screen */
  61.  
  62.  
  63.  
  64.  
  65. char get_choice ()
  66. {
  67.   char ch;
  68.  
  69.   clear_txt_scr ();
  70.   scr_write (35, 6, "Mandelbrot", normal);
  71.   scr_write (35, 8, "Julia", normal);
  72.   scr_write (35, 10, "View a screen", normal);
  73.   scr_write (35, 12, "Quit", normal);
  74.   scr_write (20, 24, "Please enter your choice (M, J, V, or Q): ", normal);
  75.   return toupper (getch ());
  76. }  /* get_choice */
  77.  
  78.  
  79.  
  80.  
  81. int bail_out ()   /* any shift key returns true (non-zero). Did this instead of
  82.                      int 16 service 1 since Turbo won't allow access to the
  83.                      flag register and inline assembler is buggy in vers 1.0 */
  84. {
  85.   _AH = 2;                                               /* get shift status */
  86.   geninterrupt (0x16);
  87.   return (_AL & 0x0f);        /* tests for alt, control, and both shift keys */
  88. }  /* bail_out */
  89.  
  90.  
  91.  
  92.  
  93.  
  94. void get_params (int *K, int *M)
  95. {
  96.   clear_txt_scr ();
  97.   scr_write (1, 7, "Please enter maximum number of iterations: ", normal);
  98.   scanf ("%d", K);
  99.   scr_write (1, 9, "Please enter maximum modulus: ", normal);
  100.   scanf ("%d", M);
  101. }  /* get_params */
  102.  
  103.  
  104.  
  105.  
  106. void get_mandel_params (float *Pmax, float *Pmin, float *Qmax, float *Qmin)
  107. {
  108.   clear_txt_scr ();
  109.   scr_write (5, 5, "Pmax = ", normal);
  110.   scr_write (5, 7, "Pmin = ", normal);
  111.   scr_write (5, 9, "Qmax = ", normal);
  112.   scr_write (5, 11, "Qmin = ", normal);
  113.   gotoXY (12, 5); scanf ("%f", Pmax);
  114.   gotoXY (12, 7); scanf ("%f", Pmin);
  115.   gotoXY (12, 9); scanf ("%f", Qmax);
  116.   gotoXY (12, 11); scanf ("%f", Qmin);
  117. }  /* get_mandel_params */
  118.  
  119.  
  120.  
  121.  
  122. void get_julia_params (float *Xmax, float *Xmin, float *Ymax, float *Ymin,
  123.                        float *P, float *Q)
  124. {
  125.   clear_txt_scr ();
  126.   scr_write (5, 5, "Xmax = ", normal);
  127.   scr_write (5, 7, "Xmin = ", normal);
  128.   scr_write (5, 9, "Ymax = ", normal);
  129.   scr_write (5, 11, "Ymin = ", normal);
  130.   scr_write (5, 13, "P = ", normal);
  131.   scr_write (5, 15, "Q = ", normal);
  132.   gotoXY (12, 5); scanf ("%f", Xmax);
  133.   gotoXY (12, 7); scanf ("%f", Xmin);
  134.   gotoXY (12, 9); scanf ("%f", Ymax);
  135.   gotoXY (12, 11); scanf ("%f", Ymin);
  136.   gotoXY (9, 13); scanf ("%f", P);
  137.   gotoXY (9, 15); scanf ("%f", Q);
  138. }  /* get_julia_params */
  139.  
  140.  
  141.  
  142.  
  143. void save_it (boolean *save, char f_name [13])         /* get file save info */
  144. {
  145.   set_text ();
  146.   scr_write (5, 2, "Will you want to save this screen? (Y/N) ", normal);
  147.   if (toupper (getch ()) == 'Y')
  148.   {
  149.     scr_write (5, 4, "Please enter file name - ", normal);
  150.     scanf ("%s", f_name);
  151.     *save = yes;
  152.   }
  153.   else
  154.     *save = no;
  155.   clear_txt_scr ();
  156.   scr_write (6, 12,
  157.     " Press any shift key to quit drawing - then any key returns to menu ",
  158.     reverse);
  159.   sleep (4);
  160. }  /* save_it */
  161.  
  162.  
  163.  
  164.  
  165. void view_it ()                            /* view a previously saved screen */
  166. {
  167.   char file_name [13];
  168.  
  169.   clear_txt_scr ();
  170.   scr_write (5, 7, "When you finish viewing, press any key to return to menu.",
  171.                     normal);
  172.   scr_write (5, 5, "Please enter name of file to be viewed: ", normal);
  173.   scanf ("%s", file_name);
  174.   set_graphics ();
  175.   get_screen (file_name, page0);                          /* show the screen */
  176.   getch ();                                /* display until a key is pressed */
  177.   set_text ();
  178. }  /* view_it */
  179.  
  180.  
  181.  
  182.  
  183.  
  184. void mandel ()
  185. {
  186.   float Pmax, Pmin, Qmax, Qmin;
  187.   int color, row, col, max_iterations, max_size;
  188.   float P, Q, modulus, deltaP, deltaQ,
  189.         Xcur, Xlast, Ycur, Ylast;
  190.   boolean save;
  191.   char file_name [13];
  192.  
  193.   get_params (&max_iterations, &max_size);
  194.   get_mandel_params (&Pmax, &Pmin, &Qmax, &Qmin);
  195.   save_it (&save, file_name);                        /* get screen save info */
  196.   set_graphics ();
  197.   deltaP = (Pmax - Pmin)/(max_col - 1.0);   /* determine real axis increment */
  198.   deltaQ = (Qmax - Qmin)/(max_row - 1.0);/*determine imaginary axis increment*/
  199.   for (col = 0; col <= max_col; col++)       /* look at each point on screen */
  200.     for (row = 0; row <= max_row; row++)
  201.     {
  202.       P = Pmin + col*deltaP;    /* determine coordinates of point in C plane */
  203.       Q = Qmin + row*deltaQ;
  204.       Xlast = Ylast = modulus = 0.0;
  205.       color = 0;
  206.       while ((modulus < max_size) && (color < max_iterations))
  207.       {         /* go around until function blows up or until max_iterations */
  208.         Xcur = sqr (Xlast) - sqr (Ylast) + P;   /* find components of next Z */
  209.         Ycur = 2 * Xlast * Ylast + Q;
  210.         color++;
  211.         Xlast = Xcur;                                       /* update last Z */
  212.         Ylast = Ycur;
  213.         modulus = sqr (Xcur) + sqr (Ycur);                 /* find size of Z */
  214.       }
  215.       draw_point (col, row, (color % max_colors));
  216.       if (bail_out ())                            /* does user want to quit? */
  217.       {
  218.         col = max_col + 1;
  219.         row = max_row + 1;
  220.       }
  221.     }
  222.   if (save)
  223.     save_screen (file_name, page0);
  224.   getch ();                                /* show fractal until key pressed */
  225.   set_text ();
  226. }  /* mandel */
  227.  
  228.  
  229.  
  230.  
  231. void julia ()
  232. {
  233.   float Xmax, Xmin, Ymax, Ymin, P, Q;
  234.   int color, row, col, max_iterations, max_size;
  235.   float modulus, deltaX, deltaY,
  236.         Xcur, Xlast, Ycur, Ylast;
  237.   boolean save;
  238.   char file_name [13];
  239.  
  240.   get_params (&max_iterations, &max_size);
  241.   get_julia_params (&Xmax, &Xmin, &Ymax, &Ymin, &P, &Q);
  242.   save_it (&save, file_name);                        /* get screen save info */
  243.   set_graphics ();
  244.   deltaX = (Xmax - Xmin)/(max_col - 1.0);   /* determine real axis increment */
  245.   deltaY = (Ymax - Ymin)/(max_row - 1.0);/*determine imaginary axis increment*/
  246.   for (col = 0; col <= max_col; col++)       /* look at each point on screen */
  247.     for (row = 0; row <= max_row; row++)
  248.     {
  249.       modulus = 0.0;
  250.       color = 0;
  251.       Xlast = Xmin + col * deltaX;              /* find coordinates of Z [0] */
  252.       Ylast = Ymin + row * deltaY;
  253.       while ((modulus < max_size) && (color < max_iterations))
  254.       {         /* go around until function blows up or until max_iterations */
  255.         Xcur = sqr (Xlast) - sqr (Ylast) + P;   /* find components of next Z */
  256.         Ycur = 2 * Xlast * Ylast + Q;
  257.         color++;
  258.         Xlast = Xcur;                                       /* update last Z */
  259.         Ylast = Ycur;
  260.         modulus = sqr (Xcur) + sqr (Ycur);                 /* find size of Z */
  261.       }
  262.       draw_point (col, row, (color % max_colors));
  263.       if (bail_out ())                        /* does user want to bail out? */
  264.       {
  265.         col = max_col + 1;
  266.         row = max_row + 1;
  267.       }
  268.     }
  269.   if (save)
  270.     save_screen (file_name, page0);
  271.   getch ();                                /* show fractal until key pressed */
  272.   set_text ();
  273. }  /* julia */
  274.  
  275.  
  276.  
  277.  
  278. main ()
  279. {
  280.   init ();                                        /* set up graphics globals */
  281.   first_screen ();
  282.   while ((choice = get_choice ()) != 'Q')
  283.   {
  284.     switch (choice)
  285.     {
  286.       case 'M':
  287.         mandel ();                                    /* draw Mandelbrot set */
  288.         break;
  289.       case 'J':
  290.         julia ();                                          /* draw Julia set */
  291.         break;
  292.       case 'V':
  293.         view_it ();                                /* look at a saved screen */
  294.         break;
  295.       default:
  296.         break;
  297.     }
  298.   }
  299.   clear_txt_scr ();
  300.   scr_write (36, 12, "bye ...", normal);
  301.   gotoXY (0, 23);
  302. }
  303.