Advertisement
Guest User

mandelset source

a guest
Oct 20th, 2012
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. int fractalCalc(double x0,double y0, double zoom);
  8.  
  9. int main()
  10. {
  11.     //constant variables.
  12.     const double xMin = -2.5; // -2.5
  13.     const double xMax = 1;    // 1
  14.     const double yMin = -1;   // -1
  15.     const double yMax = 1;    // 1
  16.     const double incRate = 0.045;
  17.  
  18.     //variable declaration.
  19.     int color;
  20.     int block;
  21.    
  22.     double x;
  23.     double y;
  24.     double xScaleMin = 0;
  25.     double xScaleMax = 0;
  26.     double yScaleMin = 0;
  27.     double yScaleMax = 0;
  28.     double zoom = 1;
  29.  
  30.     bool ext = true;
  31.     do
  32.     {
  33.         //variable reset.
  34.         x = xMin;
  35.         y = yMin;
  36.        
  37.         cin.clear();
  38.         bool endLoop = true;
  39.         while (endLoop)
  40.         {
  41.             cin.clear();
  42.             color = fractalCalc(x + xScaleMin, y + yScaleMin, zoom);
  43.  
  44.             if (color >= 0 && color <= 250)
  45.                 block = 176;
  46.             else if (color >= 251 && color <= 500)
  47.                 block = 177;
  48.             else if (color >= 501 && color <= 750)
  49.                 block = 178;
  50.             else if (color >= 751 && color <= 1000)
  51.                 block = 219;
  52.                
  53.             cout << char (block);
  54.  
  55.             if (x <= xMax)
  56.                 x += incRate;
  57.             else
  58.             {
  59.                 y += incRate;
  60.                 x = xMin;
  61.                 cout << endl;
  62.             }
  63.             if (y > yMax)
  64.                 endLoop = false;
  65.         }
  66.         char extChar;
  67.  
  68.         cout << "+ and - for zoom, W,A,S,D to move camera: ";
  69.         cin >> extChar;
  70.         switch (extChar)
  71.         {
  72.         case '+':
  73.             zoom += 0.1;
  74.             break;
  75.         case '-':
  76.             zoom -= 0.1;
  77.             break;
  78.         case 'a':
  79.             xScaleMin -= 0.2;
  80.             xScaleMax += 0.2;
  81.             break;
  82.         case 'd':
  83.             xScaleMin += 0.2;
  84.             xScaleMax -= 0.2;
  85.             break;
  86.         case 'w':
  87.             yScaleMin -= 0.2;
  88.             yScaleMax += 0.2;
  89.             break;
  90.         case 's':
  91.             yScaleMin += 0.2;
  92.             yScaleMax -= 0.2;
  93.             break;
  94.         default:
  95.             cout <<"will now exit.";
  96.             ext = false;
  97.         }
  98.    
  99.         system("CLS"); //I do realise I shouldn't use this...
  100.    
  101.     } while (ext);
  102.  
  103.     return 0;
  104. }
  105.  
  106. int fractalCalc(double x0, double y0, double zoom)
  107. {
  108.     double x = 0;
  109.     double y = 0;
  110.  
  111.     int iteration = 0;
  112.     int max_iteration = 1000; //1000
  113.  
  114.     double xtemp;
  115.     while (x*x + y*y < 2*2 && iteration < max_iteration) //x*x + y*y < 2*2 && iteration < max_iteration
  116.     {
  117.         xtemp = x*x - y*y + x0;
  118.         y = 2*x*y + y0;
  119.         y /= zoom;
  120.         x = xtemp;
  121.         x /= zoom;
  122.         iteration++;
  123.     }
  124.    
  125.  
  126.     return iteration;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement