home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************************
- * Filename: mbs.c
- * Description: Mandelbrot Set Generator
- * This program is written using Borland C/C++ ver. 3.1
- * or Turbo C/C++ ver. 3.0 utilizing the default BGI
- * interface. The graphics runtime module egavga.bgi is
- * required to be in the same directory as this executable.
- *
- * Application Notes:
- *
- * 1) The screen width and aspect ratio can be altered by changing
- * the values of X_PIXELS and Y_PIXELS. The count limit can be
- * altered by changing MAX_COUNT. The display centering can be
- * adjusted by changing X_ADJUST and Y_ADJUST.
- *
- * 2) The switch statement in GetColor() can be expanded and
- * enhanced to change pixel color and color resolution
- * depending on the value of count.
- *
- * 3) The program can be terminated at any time by pressing a key.
- *
- * (c) 1997 Jeff Stefan
- *
- ***********************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <graphics.h>
- #include <conio.h>
-
- /***************************
- * Define display parameters
- ***************************/
- #define MAX_COUNT 50
- #define X_PIXELS 450
- #define Y_PIXELS 350
- #define X_ADJUST 25
- #define Y_ADJUST 75
-
- /********************
- * Input file pointer
- *********************/
- FILE *fp;
-
- /***********************
- * Variable declarations
- ***********************/
- int FromFile;
- char TextBuffer[512];
- int Count,Color,i;
- int MaxCount = 0;
-
- /**********************
- * Function prototypes
- **********************/
- int GetColor(void);
-
- #define DEBUG
-
- main()
- {
- int j,k;
- double XStart,YStart,Ca,Cb,Zx,Zy,Xtemp,Size,Result;
- int gdriver = DETECT, gmode, errorcode;
- int Done = 0;
- int InChar;
-
- clrscr();
- printf("Read data from File or Keyboard?\n1 File 2 Keyboard\n");
- scanf("%d",&FromFile);
-
- /***************************/
- /* graphics initialization */
- /***************************/
- initgraph(&gdriver, &gmode, "");
- errorcode = graphresult();
- if(errorcode != grOk)
- {
- printf("Graphics error: %s\n", grapherrormsg(errorcode));
- printf("Press any key to halt:");
- getch();
- exit(1);
- }
-
- /***********************
- * initialize variables
- ***********************/
- Ca = Cb = XStart = YStart = Xtemp = 0.0;
-
- /*********************************
- * Get data from file or keyboard
- *********************************/
- switch(FromFile)
- {
- case 0:
- case 1:
- fp = fopen("mbs.dat","r");
- if(fp == NULL)
- exit(0);
- break;
-
- default:
- break;
- }
-
- while(!Done)
- {
- /*************************************
- * Re-init graphics to refresh screen
- **************************************/
- initgraph(&gdriver, &gmode, "");
- errorcode = graphresult();
- if(errorcode != grOk)
- {
- printf("Graphics error: %s\n", grapherrormsg(errorcode));
- printf("Press any key to halt:");
- getch();
- exit(1);
- }
-
- /************************/
- /* get input parameters */
- /************************/
- switch(FromFile)
- {
- case 0:
- case 1:
-
- if(fp!=NULL)
- {
- fscanf(fp,"%lf %lf %lf %d",&XStart,&YStart,&Size,&MaxCount);
- }
- break;
-
- /******************************
- * Get input data from keyboard
- ******************************/
- case 2:
- default:
- ClearPrompt();
- printf("Enter XStart : ");
- scanf("%lf",&XStart);
- ClearPrompt();
- printf("Enter YStart : ");
- scanf("%lf",&YStart);
- ClearPrompt();
- printf("Enter size : ");
- scanf("%lf",&Size);
- ClearPrompt();
- printf("Enter Max Iterations: ");
- scanf("%d",&MaxCount);
- break;
- }
- /*************************************************
- * Default to 100 if count is entered incorrectly
- **************************************************/
- if(MaxCount <= 0)
- {
- MaxCount = 100;
- }
-
- /*************************/
- /* Create Mandelbrot Set */
- /*************************/
- sprintf(TextBuffer,"XStart = %2.3lf YStart = %2.3lf Size = %2.3lf
- MaxCount = %d",XStart,YStart,Size,MaxCount);
- outtextxy(20,50,TextBuffer);
-
- for(j=1;j<X_PIXELS;j++)
- {
- /***************************
- * break out if keyboard hit
- ****************************/
- if(kbhit())
- {
- closegraph();
- exit(1);
- break;
- }
- /******************************************************
- * This section does the actual work by first assigning
- * values to the complex components of C, which are
- * Ca and Cb. In order to adjust the size of the
- * display, change the values of X_PIXELS and Y_PIXELS.
- *******************************************************/
- for(k=1;k<Y_PIXELS;k++)
- {
- Count=0;
- Ca = XStart+j*Size/X_PIXELS;
- Cb = YStart+k*Size/Y_PIXELS;
- Zx=Zy=0;
- /*******************************************************
- * Stay in the loop and test until MaxCount is reached
- * or the result exceeds 4.0 (definitly not in the set)
- ********************************************************/
- do {
- /*****************************************
- * 2
- * Evaluate Z = Z + C in the complex plane
- ******************************************/
- Count+=1;
- Xtemp = Zx*Zx - Zy*Zy;
- Zy = 2*Zx*Zy+Cb;
- Zx = Xtemp+Ca;
- Result = Zx*Zx + Zy*Zy;
- } while ((Count<=MaxCount) && (Result <= 4.0));
-
- /*************************************/
- /* determine pixel color and display */
- /*************************************/
- Color = GetColor();
- putpixel(j+X_ADJUST,k+Y_ADJUST,Color);
- }
- }
- /*********************************
- * Terminate program if desired
- **********************************/
- InChar = NULL;
- outtextxy(10,450,"More?(y/n)");
- InChar = getch();
- if(InChar != 'y')
- {
- Done = 1;
- if(fp)
- close(fp);
- }
- }
- /******************************
- * Restore original screen mode
- *******************************/
- closegraph();
- return(0);
- }
- /***************************************************/
- /* GetColor: determines the pixel Color depending */
- /* on the value of count. */
- /***************************************************/
- int GetColor()
- {
- switch(Count)
- {
- case 1: Color = BLUE;
- break;
- case 2: Color = GREEN;
- break;
- case 3: Color = CYAN;
- break;
- case 4: Color = RED;
- break;
- case 5: Color = MAGENTA;
- break;
-
- case 6: Color = BROWN;
- break;
- case 7: Color = LIGHTGRAY;
- break;
- case 8: Color = DARKGRAY;
- break;
- case 9: Color = LIGHTBLUE;
- break;
- case 10:Color = LIGHTGREEN;
- break;
-
- case 11: Color = CYAN;
- break;
- case 12: Color = RED;
- break;
- case 13: Color = MAGENTA;
- break;
- case 14: Color = YELLOW;
- break;
- case 15: Color = WHITE;
- break;
-
- case 21: Color = BLUE;
- break;
- case 22: Color = GREEN;
- break;
- case 23: Color = LIGHTCYAN;
- break;
- case 24: Color = BROWN;
- break;
- case 25: Color = LIGHTMAGENTA;
- break;
-
- case 31: Color = RED;
- break;
- case 32: Color = GREEN;
- break;
- case 33: Color = BLUE;
- break;
- case 34: Color = CYAN;
- break;
- case 35: Color = BROWN;
- break;
-
- case 41: Color = LIGHTCYAN;
- break;
- case 42: Color = LIGHTRED;
- break;
- case 50: Color = CYAN;
-
- case 51:
- case 100: Color = RED;
- break;
-
- case 101:
- case 125: Color = MAGENTA;
- break;
-
- case 150:
- case 165: Color = BROWN;
- break;
-
- case 166:
- case 199: Color = LIGHTGRAY;
- break;
-
- case 200 :
- case 225 : Color = DARKGRAY;
- break;
-
- case 226:
- case 250: Color = LIGHTBLUE;
- break;
-
- case 251:
- case 275: Color = LIGHTCYAN;
- break;
-
- case 276:
- case 300: Color = LIGHTRED;
- break;
-
- default : Color = BLACK;
- break;
- }
- return(Color);
- }
-
- /**************************************
- * ClearPrompt: clears text entry area
- ***************************************/
- int ClearPrompt()
- {
- gotoxy(30,15);
- printf(" ");
- gotoxy(30,15);
- return(0);
- }