/* The C++ source code for the Fractal Genorator This is a relativly simple programm that displays the desired fractal on the screen. In brief, a fractal is basicaly a formula applied to every point on a plane (Real numbers is the horizontal axis, Imaginary numbers is the vertical axis). For both Julia and Mandelbrot, the formula applied is Z->Z^2+C. Where Z is a complex variable(meaning a point on the Real-Imaginary plane) and C is a complex constant. This program loops for every point on the screen and runs the formula above. Throughout this file, comments have been placed to explain what is going on. */ // Basic C++ code, to define functions and other 'stuff' #include #include #include #include #include // This defines my functions (those not availible premade by Borland C++) // where the code for each function is found in this file void menu(void); void init(void); void mandel(int, int); void julia(int _x,int _y); void options(void); // These are the global variables. That means they can be changed from // any of my functions. This is usfull for my options function. That // means that these variables are 'user' variables and can be changed // and the changes will be stored for the displaying part of the program float frac=1; // Fractal type, 1=Julia, 2=Mandelbrot float conx=-0.74; // Real constant, horizontal axis (x) float cony=0.1; // Imaginary constant, verital axis (y) float Maxx=2; // Rightmost Real point of plane to be displayed float Minx=-2; // Leftmost Real point float Maxy=1; // Uppermost Imaginary point float Miny=-1; // Lowermost Imaginary point float initer=50; // # of times to repeat function float pixcorx; // 1 pixel on screen = this many units on the float pixcory; // plane for both x and y axis' int scrsizex; // Horizontal screen size in pixels int scrsizey; // Vertical screen size int newcolor; // Used to speed up generation int lastcolor; // See main function for more explination //***************** MAIN FUNCTION **************** /* It begins by calling the menu function. If the menu command returns control to this function without exiting, the next funtion run is my init(ilization) function (Changes the screen to graphics mode). The next block is in essence, the whole program. The outer loop is for the y axis, the inner loop for the x axis. Meaning, the entire x axis for one y value is computed then repeated for the next y value. If you run the program, this can be seen by the way the pixels are colored in. The exit condition is if any button is pressed (keyboard hit or kbhit()). What happens for each pixel, in the above order, is either the my julia function or my mandelbrot function, depending on the value of the frac variable, which can be changed in the options. There is a little command used to speed up the process. Only every other pixel on each x axis is computed, if the last color and the new color match, the pixel inbetween is assumed to be the same color also, otherwise the function is run for that pixel inbetween. This speeds up the process when there are large areas of the same color. When the displaying is done, the program waits for you to press a key before clearing the screen and starting over (back to the menu command in the beging of the main function) */ int main(void) { starting: menu(); init(); int j=0; do //Start vertical loop { int i=0; do //Start horizontal loop { if (frac) { julia(i,j); if (lastcolor!=newcolor) julia(i-1,j); else putpixel(i-1,j,lastcolor); } else { mandel(i,j); if (lastcolor!=newcolor) mandel(i-1,j); else putpixel(i-1,j,lastcolor); } newcolor=lastcolor; i+=2;} while ( (i4 ) break; // Break condition Meaning the loop will go // on to a value of infinity. } // End each pixel loop int color = k; if (color>15) color=color%15; if (k>=initer) putpixel(xpt,ypt,0); else putpixel(xpt,ypt,color); newcolor=color; } // ************* MANDELBROT FUNCTION *************** /* The mandelbrot function is the same as the Julia function except that the Z is 0 and the constant is the point under consideration. */ void mandel(int xpt, int ypt) { long double x=0; long double y=0; //converting from pixels to points long double xnew=0; long double ynew=0; for(int k=0;k<=initer;k++) // Each pixel loop { //The Mandelbrot Function Z=Z*Z+c into x and y parts xnew=x*x-y*y + xpt*pixcorx+Minx; ynew=2*x*y + Maxy-ypt*pixcory; x=xnew; y=ynew; if ( (x*x+y*y)>4 ) break; // Break condition } // End each pixel loop int color = k; if (color>15) color=color%15; if (k>=initer) putpixel(xpt,ypt,0); else putpixel(xpt,ypt,color); newcolor=color; }