Rodex_

Mandelbrot

Mar 28th, 2017
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. <-------- Header File ----------->
  2.  
  3. #include <string>
  4. #include <vector>
  5.  
  6. #ifndef MANDELBROT_ASCIIIMAGE_H
  7. #define MANDELBROT_ASCIIIMAGE_H
  8.  
  9. class ASCIIImage
  10. {
  11. private:
  12.     int height;
  13.     int length;
  14.     char** field;
  15.     std::string charset="$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
  16.  
  17. public:
  18.  
  19.     ASCIIImage(int height, int length);
  20.  
  21.     void setPix(int x, int y, int color);
  22.  
  23.  
  24.     void show();
  25.  
  26.  
  27.  
  28. };
  29.  
  30. #endif //MANDELBROT_ASCIIIMAGE_H
  31.  
  32. <------- image.cpp --------->
  33.  
  34. #include "asciiimage.h"
  35. #include <iostream>
  36.  
  37.  
  38. ASCIIImage::ASCIIImage(int height, int weidth) : height(height), length(weidth) {
  39.     field = new char*[height];
  40.  
  41.     for(int i = 0; i < height; i++){
  42.         field[i] = new char[length];
  43.     }
  44. }
  45.  
  46. void  ASCIIImage::setPix(int x, int y, int color) {
  47.     int index = color;
  48.     if(index < 0){
  49.         index = 0;
  50.     }else if(color >= charset.size()){
  51.         index = charset.size()-1;
  52.     }
  53.     index = (index-charset.size() + 1) * (-1);
  54.     field[y][x] = charset[index];
  55. }
  56.  
  57. void ASCIIImage::show() {
  58.     for (int i = 0; i < height; ++i) {
  59.         for (int j = 0; j < length; ++j) {
  60.             std::cout << field[i][j];
  61.         }
  62.         std::cout << std::endl;
  63.     }
  64. }
  65.  
  66.  
  67. <--------- main.cpp --------->
  68.  
  69.  
  70. #include <iostream>
  71. #include "asciiimage.h"
  72.  
  73. using namespace std;
  74.  
  75. int main() {
  76.     int height, weidth, max, finish;
  77.     double x0, x1, y0, y1, y, x, zr, zi;
  78.  
  79.     cout << "Enter Width: ";
  80.     cin >> weidth;
  81.     cout << "Enter Height: ";
  82.     cin >> height;
  83.  
  84.  
  85.     cout << "Enter x0: ";
  86.     cin >> x0;
  87.     cout << "Enter y0: ";
  88.     cin >> y0;
  89.     cout << "Enter x1: ";
  90.     cin >> x1;
  91.     cout << "Enter y1: ";
  92.     cin >> y1;
  93.     cout << "Enter max: ";
  94.     cin >> max;
  95.  
  96.     ASCIIImage *asciiimage = new ASCIIImage(height, weidth);
  97.  
  98.     for(int w = 0; w < weidth; w++) {
  99.         for(int h = 0; h < height; h++) {
  100.             x = x0 + w*(x1 - x0)/(weidth);
  101.             y = y0 + h*(y1 - y0)/(height);
  102.  
  103.             zr = 0.;
  104.             zi = 0.;
  105.             finish = 1;
  106.             for (int i = 0; i < max; ++i) {
  107.                 // calculate next iteration
  108.                 // For the interested reader: z=zr+i*zi and c=x+i*y are complex numbers
  109.                 // In the next row we calculate z * z + c and check whether |z| > 2
  110.                 double nextzr = zr * zr - zi * zi + x;
  111.                 double nextzi = 2 * zr * zi + y;
  112.                 // are we done?
  113.                 if (nextzr * nextzr + nextzi * nextzi > 4) {
  114.                     // set pixel to i % 70
  115.                     asciiimage->setPix(w, h, (i % 70));
  116.                     break;
  117.                 }
  118.                 zr = nextzr;
  119.                 zi = nextzi;
  120.                 if(i+1 == max){
  121.                    finish = 0;
  122.                 }
  123.             }
  124.             if(finish != 1) {
  125.                 asciiimage->setPix(w, h, max % 70);
  126.             }
  127.         }
  128.     }
  129.  
  130.     asciiimage->show();
  131.  
  132.     return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment