home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / CODICE / COMPR.ZIP / MBS.C < prev   
Encoding:
C/C++ Source or Header  |  1997-03-29  |  8.5 KB  |  350 lines

  1. /*********************************************************************
  2. * Filename: mbs.c
  3. * Description: Mandelbrot Set Generator
  4. *              This program is written using Borland C/C++ ver. 3.1
  5. *              or Turbo C/C++ ver. 3.0 utilizing the default BGI
  6. *              interface. The graphics runtime module egavga.bgi is
  7. *              required to be in the same directory as this executable.
  8. *
  9. *   Application Notes:
  10. *
  11. *     1) The screen width and aspect ratio can be altered by changing
  12. *        the values of X_PIXELS and Y_PIXELS. The count limit can be
  13. *        altered by changing MAX_COUNT. The display centering can be
  14. *        adjusted by changing X_ADJUST and Y_ADJUST.
  15. *
  16. *     2) The switch statement in GetColor() can be expanded and
  17. *        enhanced to change pixel color and color resolution
  18. *        depending on the value of count.
  19. *
  20. *     3) The program can be terminated at any time by pressing a key.
  21. *
  22. *  (c)  1997 Jeff Stefan
  23. *
  24. ***********************************************************************/
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <graphics.h>
  28. #include <conio.h>
  29.  
  30. /***************************
  31. * Define display parameters
  32. ***************************/
  33. #define MAX_COUNT 50
  34. #define X_PIXELS  450
  35. #define Y_PIXELS  350
  36. #define X_ADJUST  25
  37. #define Y_ADJUST  75
  38.  
  39. /********************
  40. * Input file pointer
  41. *********************/
  42. FILE *fp;
  43.  
  44. /***********************
  45. * Variable declarations
  46. ***********************/
  47. int FromFile;
  48. char TextBuffer[512];
  49. int Count,Color,i;
  50. int MaxCount = 0;
  51.  
  52. /**********************
  53. * Function prototypes
  54. **********************/
  55. int GetColor(void);
  56.  
  57. #define DEBUG
  58.  
  59. main()
  60. {
  61. int j,k;
  62. double XStart,YStart,Ca,Cb,Zx,Zy,Xtemp,Size,Result;
  63. int gdriver = DETECT, gmode, errorcode;
  64. int Done = 0;
  65. int InChar;
  66.  
  67.   clrscr();
  68.   printf("Read data from File or Keyboard?\n1 File  2 Keyboard\n");
  69.   scanf("%d",&FromFile);
  70.  
  71.   /***************************/
  72.   /* graphics initialization */
  73.   /***************************/
  74.   initgraph(&gdriver, &gmode, "");
  75.   errorcode = graphresult();
  76.   if(errorcode != grOk)
  77.   {
  78.     printf("Graphics error: %s\n", grapherrormsg(errorcode));
  79.     printf("Press any key to halt:");
  80.     getch();
  81.     exit(1);
  82.   }
  83.  
  84.   /***********************
  85.   * initialize variables
  86.   ***********************/
  87.   Ca = Cb = XStart = YStart = Xtemp = 0.0;
  88.  
  89.   /*********************************
  90.   * Get data from file or keyboard
  91.   *********************************/
  92.   switch(FromFile)
  93.   {
  94.   case 0:
  95.   case 1:
  96.     fp = fopen("mbs.dat","r");
  97.     if(fp == NULL)
  98.       exit(0);
  99.     break;
  100.  
  101.   default:
  102.     break;
  103.   }
  104.  
  105.   while(!Done)
  106.   {
  107.     /*************************************
  108.     * Re-init graphics to refresh screen
  109.     **************************************/
  110.     initgraph(&gdriver, &gmode, "");
  111.     errorcode = graphresult();
  112.     if(errorcode != grOk)
  113.     {
  114.       printf("Graphics error: %s\n", grapherrormsg(errorcode));
  115.       printf("Press any key to halt:");
  116.       getch();
  117.       exit(1);
  118.     }
  119.  
  120.     /************************/
  121.     /* get input parameters */
  122.     /************************/
  123.     switch(FromFile)
  124.     {
  125.     case 0:
  126.     case 1:
  127.  
  128.       if(fp!=NULL)
  129.       {
  130.         fscanf(fp,"%lf %lf %lf %d",&XStart,&YStart,&Size,&MaxCount);
  131.       }
  132.       break;
  133.  
  134.     /******************************
  135.     * Get input data from keyboard
  136.     ******************************/
  137.     case 2:
  138.     default:
  139.       ClearPrompt();
  140.       printf("Enter XStart : ");
  141.       scanf("%lf",&XStart);
  142.       ClearPrompt();
  143.       printf("Enter YStart : ");
  144.       scanf("%lf",&YStart);
  145.       ClearPrompt();
  146.       printf("Enter size : ");
  147.       scanf("%lf",&Size);
  148.       ClearPrompt();
  149.       printf("Enter Max Iterations: ");
  150.       scanf("%d",&MaxCount);
  151.       break;
  152.     }
  153.     /*************************************************
  154.     * Default to 100 if count is entered incorrectly
  155.     **************************************************/
  156.     if(MaxCount <= 0)
  157.     {
  158.       MaxCount = 100;
  159.     }
  160.  
  161.     /*************************/
  162.     /* Create Mandelbrot Set */
  163.     /*************************/
  164.     sprintf(TextBuffer,"XStart = %2.3lf YStart = %2.3lf Size = %2.3lf
  165.             MaxCount = %d",XStart,YStart,Size,MaxCount);
  166.     outtextxy(20,50,TextBuffer);
  167.  
  168.     for(j=1;j<X_PIXELS;j++)
  169.     {
  170.       /***************************
  171.       * break out if keyboard hit
  172.       ****************************/
  173.       if(kbhit())
  174.             {
  175.               closegraph();
  176.               exit(1);
  177.         break;
  178.             }
  179.       /******************************************************
  180.       * This section does the actual work by first assigning
  181.       * values to the complex components of C, which are
  182.       * Ca and Cb. In order to adjust the size of the
  183.       * display, change the values of X_PIXELS and Y_PIXELS.
  184.       *******************************************************/
  185.       for(k=1;k<Y_PIXELS;k++)
  186.       {
  187.         Count=0;
  188.         Ca = XStart+j*Size/X_PIXELS;
  189.         Cb = YStart+k*Size/Y_PIXELS;
  190.         Zx=Zy=0;
  191.         /*******************************************************
  192.         *  Stay in the loop and test until MaxCount is reached
  193.         * or the result exceeds 4.0 (definitly not in the set)
  194.         ********************************************************/
  195.         do {
  196.           /*****************************************
  197.           *               2
  198.           * Evaluate Z = Z + C in the complex plane
  199.           ******************************************/
  200.                 Count+=1;
  201.                 Xtemp = Zx*Zx - Zy*Zy;
  202.                 Zy = 2*Zx*Zy+Cb;
  203.                 Zx = Xtemp+Ca;
  204.                 Result = Zx*Zx + Zy*Zy;
  205.         } while ((Count<=MaxCount) && (Result <= 4.0));
  206.  
  207.         /*************************************/
  208.         /* determine pixel color and display */
  209.         /*************************************/
  210.         Color = GetColor();
  211.         putpixel(j+X_ADJUST,k+Y_ADJUST,Color);
  212.       }
  213.     }
  214.     /*********************************
  215.     *  Terminate program if desired
  216.     **********************************/
  217.     InChar = NULL;
  218.     outtextxy(10,450,"More?(y/n)");
  219.     InChar = getch();
  220.     if(InChar != 'y')
  221.     {
  222.       Done = 1;
  223.       if(fp)
  224.         close(fp);
  225.     }
  226.   }
  227.   /******************************
  228.   * Restore original screen mode
  229.   *******************************/
  230.   closegraph();
  231.  return(0);
  232. }
  233. /***************************************************/
  234. /* GetColor: determines the pixel Color depending  */
  235. /*           on the value of count.                */
  236. /***************************************************/
  237. int GetColor()
  238. {
  239.   switch(Count)
  240.   {
  241.    case 1: Color = BLUE;
  242.    break;
  243.    case 2: Color = GREEN;
  244.    break;
  245.    case 3: Color = CYAN;
  246.    break;
  247.    case 4: Color = RED;
  248.    break;
  249.    case 5: Color = MAGENTA;
  250.    break;
  251.  
  252.    case 6: Color = BROWN;
  253.    break;
  254.    case 7: Color = LIGHTGRAY;
  255.    break;
  256.    case 8: Color = DARKGRAY;
  257.    break;
  258.    case 9: Color = LIGHTBLUE;
  259.    break;
  260.    case 10:Color = LIGHTGREEN;
  261.    break;
  262.  
  263.    case 11: Color = CYAN;
  264.      break;
  265.    case 12: Color = RED;
  266.      break;
  267.    case 13: Color = MAGENTA;
  268.      break;
  269.    case 14: Color = YELLOW;
  270.      break;
  271.    case 15: Color = WHITE;
  272.      break;
  273.  
  274.    case 21:  Color = BLUE;
  275.      break;
  276.    case 22:  Color = GREEN;
  277.      break;
  278.    case 23:  Color = LIGHTCYAN;
  279.      break;
  280.    case 24:  Color = BROWN;
  281.      break;
  282.    case 25:  Color = LIGHTMAGENTA;
  283.      break;
  284.  
  285.    case 31:  Color =  RED;
  286.      break;
  287.    case 32:  Color = GREEN;
  288.      break;
  289.    case 33:  Color = BLUE;
  290.      break;
  291.    case 34:  Color = CYAN;
  292.      break;
  293.    case 35:  Color = BROWN;
  294.      break;
  295.  
  296.    case 41:  Color = LIGHTCYAN;
  297.      break;
  298.    case 42:  Color = LIGHTRED;
  299.      break;
  300.    case 50:  Color = CYAN;
  301.  
  302.    case 51:
  303.    case 100: Color = RED;
  304.    break;
  305.  
  306.    case 101:
  307.    case 125: Color = MAGENTA;
  308.    break;
  309.  
  310.    case 150:
  311.    case 165: Color = BROWN;
  312.    break;
  313.  
  314.    case 166:
  315.    case 199: Color = LIGHTGRAY;
  316.    break;
  317.  
  318.    case 200 :
  319.    case 225 : Color = DARKGRAY;
  320.    break;
  321.  
  322.    case 226:
  323.    case 250: Color = LIGHTBLUE;
  324.    break;
  325.  
  326.    case 251:
  327.    case 275: Color = LIGHTCYAN;
  328.    break;
  329.  
  330.    case 276:
  331.    case 300: Color = LIGHTRED;
  332.    break;
  333.  
  334.    default : Color = BLACK;
  335.    break;
  336.   }
  337.   return(Color);
  338. }
  339.  
  340. /**************************************
  341. * ClearPrompt: clears text entry area
  342. ***************************************/
  343. int ClearPrompt()
  344. {
  345.   gotoxy(30,15);
  346.   printf("                      ");
  347.   gotoxy(30,15);
  348.   return(0);
  349. }
  350.