Advertisement
ZemaToxic

Untitled

May 8th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>>
  3. using namespace std;
  4.  
  5. int findMandelbrot(double cr, double ci, int max_iterations)
  6. {
  7.     int i = 0;
  8.     double zr = 0.0, zi = 0.0;
  9.     while (i < max_iterations && zr * zr + zi * zi < 4.0)
  10.     {
  11.         double temp = zr * zr - zi * zi + cr;
  12.         zi = 2.0 * zr * zi + ci;
  13.         zr = temp;
  14.         i++;
  15.     }
  16.  
  17.     return i;
  18. }
  19.  
  20. double mapToReal(int x, int imageWidth, double minR, double maxR)
  21. {
  22.     double range = maxR - minR;
  23.     return x * (range / imageWidth) + minR;
  24. }
  25.  
  26. double mapToImaginary(int y, int imageHeight, double minI, double maxI)
  27. {
  28.     double range = maxI - minI;
  29.     return y * (range / imageHeight) + minI;
  30. }
  31.  
  32. int main()
  33. {
  34.     // Get the required input values from file...
  35.     ifstream fin("input.txt");
  36.     int imageWidth, imageHeight, maxN;
  37.     double minR, maxR, minI, maxI;
  38.  
  39.     if (!fin)
  40.     {
  41.         cout << "Could not open file!" << endl;
  42.         return 1;
  43.     }
  44.  
  45.     fin >> imageWidth >> imageHeight >> maxN;
  46.     fin >> minR >> maxR >> minI >> maxI;
  47.     fin.close(); // Not necessary, good practice :D
  48.  
  49.                  // Open the output file, write the PPM header...
  50.     ofstream fout("output_image.ppm");
  51.     fout << "P3" << endl; // "Magic Number" - PPM file
  52.     fout << imageWidth << " " << imageHeight << endl; // Dimensions
  53.     fout << "255" << endl; // Maximum value of a pixel R,G,B value...
  54.  
  55.                            // For every pixel...
  56.     for (int y = 0; y < imageHeight; y++) // Rows...
  57.     {
  58.         for (int x = 0; x < imageWidth; x++) // Pixels in row (columns)...
  59.         {
  60.             // ... Find the real and imaginary values for c, corresponding to that
  61.             //     x, y pixel in the image.
  62.             double cr = mapToReal(x, imageWidth, minR, maxR);
  63.             double ci = mapToImaginary(y, imageHeight, minI, maxI);
  64.  
  65.             // ... Find the number of iterations in the Mandelbrot formula
  66.             //     using said c.
  67.             int n = findMandelbrot(cr, ci, maxN);
  68.  
  69.             // ... Map the resulting number to an RGP value
  70.             int r = (n % 256);
  71.             int g = (n % 256);
  72.             int b = (n % 256);
  73.  
  74.             // ... Output it to an image
  75.             fout << r << " " << g << " " << b << " ";
  76.         }
  77.         fout << endl;
  78.     }
  79.     fout.close();
  80.  
  81.     cout << "Finished!" << endl;
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement