Advertisement
he_obviously

Untitled

Oct 6th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <SFML/Graphics.hpp>
  4.  
  5. struct point {
  6.  
  7.     double x, y;
  8.  
  9.     point() {}
  10.     point(double _x, double _y) {
  11.         this->x = _x;
  12.         this->y = _y;
  13.     }
  14.  
  15. };
  16.  
  17. struct pixel {
  18.  
  19.     double x, y;
  20.  
  21.     pixel() {}
  22.     pixel(double _x, double _y) {
  23.         this->x = _x;
  24.         this->y = _y;
  25.     }
  26.  
  27. };
  28.  
  29. struct complex {
  30.  
  31.     double Re, Im;
  32.  
  33.     complex(): Re(0), Im(0) {}
  34.     complex(double x, double y) {
  35.         this->Re = x;
  36.         this->Im = y;
  37.     }
  38.  
  39.     friend complex operator-(const complex &a) {
  40.         return {-a.Re, -a.Im};
  41.     }
  42.  
  43.     friend complex operator+(const complex &a, const complex &b) {
  44.         return {a.Re + b.Re, a.Im + b.Im};
  45.     }
  46.  
  47.     friend complex operator-(const complex &a, const complex &b) {
  48.         return a + (-b);
  49.     }
  50.  
  51.     friend double dot_product(const complex &a, const complex &b) {
  52.         return a.Re * b.Re + a.Im * b.Im;
  53.     }
  54.  
  55.     friend double cross_product(const complex &a, const complex &b) {
  56.         return a.Re * b.Im - a.Im * b.Re;
  57.     }
  58.  
  59.     friend complex operator*(const complex &a, const complex &b) {
  60.         return {a.Re * b.Re - a.Im * b.Im, a.Re * b.Im + a.Im * b.Re};
  61.     }
  62.  
  63.     complex pow(int n) {
  64.         complex ans = {1, 0}, step = {Re, Im};
  65.         while (n > 0) {
  66.             if (n % 2 == 1) {
  67.                 ans = ans * step;
  68.             }
  69.             ans = ans * ans;
  70.             n /= 2;
  71.         }
  72.         return ans;
  73.     }
  74.  
  75.     double abs() {
  76.         return sqrt(Re * Re + Im * Im);
  77.     }
  78.  
  79. };
  80.  
  81. const int W = 1080, H = 720, MAX_ITERS = 200,
  82.     repeat_gradient = 100;
  83. double scale = 200;
  84. pixel centre{W / 2, H / 2};
  85. sf::Uint8* pixels = new sf::Uint8[W * H * 4];
  86. int gradient_size;
  87. const sf::Uint8* gradient;
  88.  
  89. point pixel_to_point(const pixel &a) {
  90.     return {(a.x - centre.x) / scale, (centre.y - a.y) / scale};
  91. }
  92.  
  93. void make_fractal(complex c) {
  94.     for (int i = 0; i < W; ++i) {
  95.         for (int j = 0; j < H; ++j) {
  96.             point p = pixel_to_point({double(i), double(j)});
  97.             complex z = {p.x, p.y};
  98.             int it;
  99.             double r = 5;
  100.             double r2 = r * r;
  101.             for (it = 0; it < MAX_ITERS; ++it) {
  102.                 if (z.abs() * z.abs() >= r2) {
  103.                     break;
  104.                 }
  105.                 z = z.pow(2) + c;
  106.             }
  107.             double len = z.abs();
  108.             double m = 10 + double(it) - log2(log2(len));
  109.             int gradient_id = int(m * repeat_gradient) * 4;
  110.             int id = 4 * (j * W + i);
  111.             if (it == MAX_ITERS) {
  112.                 pixels[id] = pixels[id + 1] = pixels[id + 2] = 0;
  113.                 pixels[id + 3] = 255;
  114.             }
  115.             else {
  116.                 pixels[id] = gradient[gradient_id];
  117.                 pixels[id + 1] = gradient[gradient_id + 1];
  118.                 pixels[id + 2] = gradient[gradient_id + 2];
  119.                 pixels[id + 3] = gradient[gradient_id + 3];
  120.             }
  121.         }
  122.     }
  123. }
  124.  
  125. int main() {
  126.    
  127.     sf::RenderWindow window(sf::VideoMode(W, H), "SFML works!");
  128.  
  129.     sf::Image gradient_image;
  130.     gradient_image.loadFromFile("./1.png");
  131.  
  132.     gradient = gradient_image.getPixelsPtr();
  133.     gradient_size = gradient_image.getSize().x * gradient_image.getSize().y * 4;
  134.  
  135.     sf::Texture texture;
  136.     texture.create(W, H);
  137.     sf::Sprite sprite(texture);
  138.  
  139.     while (window.isOpen())
  140.     {
  141.         sf::Event event;
  142.         while (window.pollEvent(event))
  143.         {
  144.             if (event.type == sf::Event::Closed) {
  145.                 window.close();
  146.             }
  147.             /*else if (event.type == sf::Event::MouseButtonPressed) {
  148.                 pressed = true;
  149.                 mouse_pos = {event.mouseButton.x, event.mouseButton.y};
  150.             }
  151.             else if (event.type == sf::Event::MouseButtonReleased) {
  152.                 pressed = false;
  153.                 pixel current = {event.mouseButton.x, event.mouseButton.y};
  154.  
  155.             }*/
  156.         }
  157.         make_fractal({0.377, -0.248});
  158.         texture.update(pixels);
  159.         window.clear();
  160.         window.draw(sprite);
  161.         window.display();
  162.     }
  163.  
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement