Advertisement
he_obviously

Untitled

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