This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

MandelbrotDOS

By: a guest on Dec 25th, 2012  |  syntax: C++  |  size: 2.63 KB  |  views: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*      Bryan Tremblay 25Dec12@0454E
  2.  
  3.         I tried to make a Mandelbrot genertor for the command prompt
  4.         but for some reason it only produces ovals... the equivalant
  5.         of low-res mandelbrot iterations. I can't seem to get it to
  6.         go higher. I submitted a request to stackoverflow.com:
  7.         http://stackoverflow.com/questions/14029538/low-resolution-mandelbrot-fractal-not-high-enough-resolution
  8.         TURNS OUT that I wasn't running the entire equation, guy name Potatoswatter fixed my code
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <conio.h>
  13. #include <math.h>
  14.  
  15. #define DOSWidth 80
  16. #define DOSHeight 25
  17.  
  18. int iterations = 32;
  19. float escapeValue = 3.0f;
  20.  
  21. struct ivar {
  22.         ivar(float _x, float _i) {
  23.                 x = _x;
  24.                 i = _i;
  25.         }
  26.         void log() {printf("(%g%c%gi)", x, (i<0)?'-':'+', fabs(i));}
  27.         float magnitude() {return sqrtf(x*x+i*i);}
  28.         ivar square() {return ivar(x, i)*ivar(x, i);}
  29.  
  30.         ivar operator + (ivar v) {return ivar(x+v.x, i+v.i);};
  31.         ivar operator - (ivar v) {return ivar(x-v.x, i-v.i);};
  32.         ivar operator * (ivar v) {return ivar(x*v.x-(i*v.i), x*v.i+i*v.x);};
  33.  
  34.         float x, i;
  35. };
  36.  
  37. struct rect {
  38.         rect(float _x, float _y, float _width, float _height) {
  39.                 x = _x;y = _y;width = _width;height = _height;
  40.         }
  41.  
  42.         void log() {printf("(%f, %f, %f, %f)", x-width/2.0f, y-height/2.0f, x+width/2.0f, y+height/2.0f);}
  43.  
  44.         float x, y;
  45.         float width, height;
  46. };
  47.  
  48. void render(rect region);
  49.  
  50. int main() {
  51.         bool shouldContinue = true;
  52.         float DOSRatio = DOSWidth/DOSHeight;
  53.         rect region = rect(0, 0, 2*DOSRatio, 2);
  54.         while(shouldContinue) {
  55.                 render(region);
  56.                 char input = getch();
  57.                 float xSize = region.height/10.0f;
  58.                 float ySize = region.width/(10.0f*DOSRatio);
  59.                 if(input == 'w')region.y += xSize;
  60.                 if(input == 's')region.y -= xSize;
  61.                 if(input == 'a')region.x += ySize;
  62.                 if(input == 'd')region.x -= ySize;
  63.                 if(input == '+' || input == '=')iterations+=10;
  64.                 if(input == '-')iterations-=10;
  65.                 if(input == 'e')region = rect(region.x, region.y, region.width*0.9f, region.height*0.9f);
  66.                 if(input == 'q')region = rect(region.x, region.y, region.width/0.9f, region.height/0.9f);
  67.                 if(input == 0x1b)shouldContinue = false;
  68.         }
  69. }
  70.  
  71. void render(rect region) {
  72.         float xSize = region.width / (float)DOSWidth;
  73.         float ySize = region.height / (float)DOSHeight;
  74.         for(int y=0;y<DOSHeight;y++) {
  75.                 for(int x=0;x<DOSWidth;x++) {
  76.                         ivar pos = ivar(x*xSize-(region.width/2.0f+region.x), y*ySize-(region.height/2.0f+region.y));
  77.                         ivar pos0 = pos;
  78.                         bool escapes = false;
  79.                         for(int i=0;i<iterations;i++) {
  80.                                 if(pos.magnitude() > escapeValue) {
  81.                                         escapes = true;
  82.                                         break;
  83.                                 }
  84.                                 pos = pos.square()+pos0;
  85.                         }
  86.                         if(escapes)printf(" ");
  87.                         else printf("X");
  88.                 }
  89.         }
  90. }
clone this paste RAW Paste Data