Advertisement
he_obviously

Untitled

Oct 6th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 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 = 1080, H = 720, MAX_ITERS = 200;
  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. void make_fractal(complex c) {
  88.     for (int i = 0; i < W; ++i) {
  89.         for (int j = 0; j < H; ++j) {
  90.             point p = pixel_to_point({double(i), double(j)});
  91.             complex z = {p.x, p.y};
  92.             int it;
  93.             double r = 5;
  94.             for (it = 0; it < MAX_ITERS; ++it) {
  95.                 if (z.abs() >= r) {
  96.                     break;
  97.                 }
  98.                 z = z.pow(2) + c;
  99.             }
  100.             double len = z.abs();
  101.             int id = 4 * (j * W + i);
  102.             if (it == MAX_ITERS) {
  103.                 pixels[id] = pixels[id + 1] = pixels[id + 2] = 0;
  104.                 pixels[id + 3] = 255;
  105.             }
  106.             else {
  107.                 pixels[id] = 10 * sin(len + 2.0) + 150;
  108.                 pixels[id + 1] = 15 * sin(len + 1.0) + 200;
  109.                 pixels[id + 2] = 20 * sin(len + 1.5) + 100;
  110.                 pixels[id + 3] = 255;
  111.             }
  112.         }
  113.     }
  114. }
  115.  
  116. int main() {
  117.    
  118.     sf::RenderWindow window(sf::VideoMode(W, H), "SFML works!");
  119.  
  120.     sf::Texture texture;
  121.     texture.create(W, H);
  122.     sf::Sprite sprite(texture);
  123.  
  124.     while (window.isOpen())
  125.     {
  126.         sf::Event event;
  127.         while (window.pollEvent(event))
  128.         {
  129.             if (event.type == sf::Event::Closed) {
  130.                 window.close();
  131.             }
  132.             /*else if (event.type == sf::Event::MouseButtonPressed) {
  133.                 pressed = true;
  134.                 mouse_pos = {event.mouseButton.x, event.mouseButton.y};
  135.             }
  136.             else if (event.type == sf::Event::MouseButtonReleased) {
  137.                 pressed = false;
  138.                 pixel current = {event.mouseButton.x, event.mouseButton.y};
  139.  
  140.             }*/
  141.         }
  142.         make_fractal({0.377, -0.248});
  143.         texture.update(pixels);
  144.         window.clear();
  145.         window.draw(sprite);
  146.         window.display();
  147.     }
  148.  
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement