\\Written by Tarje Hillersoey - thi007 #include #include #include using namespace std; #define WIDTH 1000 //Creating the size of the canvas #define HEIGHT 1000 int find_convergence(int x, int y){ //Creates a function which assings a value to each pixel depending on how fast it converges/diverges and stores it as an integer. complex pixel((double)x/(WIDTH/2) - 1.5, (double)y/(HEIGHT/2) - 1); //Pixelating the plane complex z (0, 0); //Starting point int iter = 0; while(abs(z) < 2 && iter <= 100){ //Conditions for iterations, here 100 is chosen (somewhat arbitrary) as the maximum number of iterations. This may be changed depending on you preference z = pow(z, 2) + pixel; //of how analytical you want the program to be and how much runtime is acceptable. iter++; } if (iter < 100){ double scale = 255*iter/50; if (scale > 255) return 255; else return scale; } else return 0; //If the function remains bounded under the threshold (100) number of itearations for a given point, then it is assigned a color value of 0. } void mandelbrot(){ ofstream output; output.open("mandelbrot.ppm"); //Creating the ppm format TH2F *h2 = new TH2F("h2","Mandelbrot",1000,-2, 2,1000, -2, 2); //Creating the standard Canvas if (output.is_open()){ output << "P3\n" << WIDTH << " " << HEIGHT << " 255\n"; for (int i = 0; i < WIDTH; i++){ //Iterate through the grid of pixels. for (int j = 0; j < HEIGHT; j++){ double x=h2->GetXaxis()->GetBinCenter(i); double y=h2->GetYaxis()->GetBinCenter(j); int intensity = find_convergence(i, j); //output << intensity << " " << intensity << " " << intensity/2 << "\n"; //By removing the double slash on this line a ppm picture will also be created. h2->Fill(x,y, intensity); //The grid is now filled with color depending on the divergence (convergence) rate. } } } output.close(); h2->Draw("colz"); }