Advertisement
LegoSosiska

Not working correctly but beautiful plasma fractal v2

Sep 26th, 2022
998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.20 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/Window.hpp>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <vector>
  6. #include <cmath>
  7. #include <random>
  8. #include <cstdlib>
  9. #include <ctime>
  10.  
  11. int range = 10;
  12.  
  13. //class Complex {
  14. //private:
  15. //    double re;
  16. //    double im;
  17. //public:
  18. //
  19. //    Complex(double re, double im) {
  20. //        this->re = re;
  21. //        this->im = im;
  22. //    }
  23. //    void setre(double re) {
  24. //        this->re = re;
  25. //    }
  26. //    void setim(double im){
  27. //        this->im = im;
  28. //    }
  29. //    double getre(){
  30. //        return this->re;
  31. //    }
  32. //    double getim(){
  33. //        return this->im;
  34. //    }
  35. //    double module() {
  36. //        return sqrt(this->re * this->re + this->im * this->im);
  37. //    }
  38. //
  39. //    Complex operator+(Complex cn){
  40. //        return Complex(this->re + cn.getre(), this->im + cn.getim());
  41. //    }
  42. //
  43. //    Complex operator*(Complex cn) {
  44. //        return Complex(this->re * cn.getre() - this->im * cn.getim(), this->im * cn.getre() + this->re * cn.getim());
  45. //    }
  46. //
  47. //};
  48.  
  49. struct Pixel{
  50.     sf::Vector2i cords;
  51.     sf::Color color;
  52.     Pixel(sf::Vector2i cords, sf::Color color) {
  53.         this->cords = cords;
  54.         this->color = color;
  55.     }
  56. };
  57.  
  58.  
  59. void ToColor(sf::Image& image, Pixel topLeft, Pixel topRight, Pixel botLeft, Pixel botRight, int iteration = 1024) {
  60.     if (topRight.cords.x - topLeft.cords.x <= 1 || iteration == 1) {
  61.         return;
  62.     }
  63.     sf::Color col1(abs(topLeft.color.r - topRight.color.r) / 2 + std::min(topLeft.color.r, topRight.color.r),
  64.                    abs(topLeft.color.g - topRight.color.g) / 2 + std::min(topLeft.color.g, topRight.color.g),
  65.                    abs(topLeft.color.b - topRight.color.b) / 2 + std::min(topLeft.color.b, topRight.color.b) );
  66.     sf::Color col2(abs(topLeft.color.r - botLeft.color.r) / 2 + std::min(topLeft.color.r, botLeft.color.r),
  67.                    abs(topLeft.color.g - botLeft.color.g) / 2 + std::min(topLeft.color.g, botLeft.color.g),
  68.                    abs(topLeft.color.b - botLeft.color.b) / 2 + std::min(topLeft.color.b, botLeft.color.b));
  69.     sf::Color col3(abs(topRight.color.r - botRight.color.r) / 2 + std::min(topRight.color.r, botRight.color.r),
  70.                    abs(topRight.color.g - botRight.color.g) / 2 + std::min(topRight.color.g, botRight.color.g),
  71.                    abs(topRight.color.b - botRight.color.b) / 2 + std::min(topRight.color.b, botRight.color.b)) ;
  72.  
  73.     sf::Color col4(abs(botLeft.color.r - botRight.color.r) / 2 + std::min(botLeft.color.r, botRight.color.r),
  74.                    abs(botLeft.color.g - botRight.color.g) / 2 + std::min(botLeft.color.g, botRight.color.g),
  75.                    abs(botLeft.color.b - botRight.color.b) / 2 + std::min(botLeft.color.b, botRight.color.b));
  76.     Pixel topMid(topLeft.cords + (topRight.cords - topLeft.cords) / 2, col1);
  77.     Pixel leftMid(topLeft.cords + (botLeft.cords - topLeft.cords) / 2, col2);
  78.     Pixel rightMid(topRight.cords + (botRight.cords - topRight.cords) / 2, col3);
  79.     Pixel botMid(botLeft.cords + (botRight.cords - botLeft.cords) / 2, col4);
  80.  
  81.     int red = abs(col2.r - col3.r) + std::min(col2.r, col3.r) - range + std::rand() % range * 2;
  82.     int green = abs(col2.g - col3.g) + std::min(col2.g, col3.g) - range + std::rand() % range * 2;
  83.     int blue = abs(col2.b - col3.b) + std::min(col2.b, col3.b) - range + std::rand() % range * 2;
  84.  
  85.     //std::cout << red << " " << green << " " << blue << " ";
  86.  
  87.     sf::Color midCol(red, green, blue);
  88.  
  89.     Pixel center(sf::Vector2i(topLeft.cords.x + (topRight.cords.x - topLeft.cords.x) / 2, topLeft.cords.y + (botLeft.cords.y - topLeft.cords.y) / 2), midCol);
  90.  
  91.     image.setPixel(topMid.cords.x, topMid.cords.y, col1);
  92.     image.setPixel(leftMid.cords.x, leftMid.cords.y, col2);
  93.     image.setPixel(rightMid.cords.x, rightMid.cords.y, col3);
  94.     image.setPixel(botMid.cords.x, botMid.cords.y,  col4);
  95.     image.setPixel(center.cords.x, center.cords.y, midCol);
  96.  
  97.     ToColor(image, topLeft, topMid, leftMid, center, iteration / 2);
  98.     ToColor(image, topMid, topRight, center, rightMid, iteration / 2);
  99.     ToColor(image, leftMid, center, botLeft, botMid, iteration / 2);
  100.     ToColor(image, center, rightMid, botMid, botRight, iteration / 2);
  101.  
  102.  
  103. }
  104.  
  105. int main() {
  106.     sf::RenderWindow window(sf::VideoMode(1025, 1025), "AA");
  107.     std::srand(std::time(nullptr));
  108.  
  109.     int iteration = 0;
  110.  
  111.     sf::Image image;
  112.     image.create(1025, 1025, sf::Color::Green);
  113.  
  114.     Pixel topLeft(sf::Vector2i(0, 0), sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256));
  115.     Pixel topRight(sf::Vector2i(1024, 0), sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256));
  116.     Pixel botLeft(sf::Vector2i(0, 1024), sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256));
  117.     Pixel botRight(sf::Vector2i(1024, 1024), sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256));
  118.     image.setPixel(topLeft.cords.x, topLeft.cords.y, topLeft.color);
  119.     image.setPixel(topRight.cords.x, topRight.cords.y, topRight.color);
  120.     image.setPixel(botLeft.cords.x, botLeft.cords.y, botLeft.color);
  121.     image.setPixel(botRight.cords.x, botRight.cords.y, botRight.color);
  122.     ToColor(image, topLeft, topRight, botLeft, botRight);
  123.  
  124.     sf::Texture texture;
  125.     texture.create(1025, 1025);
  126.     texture.update(image);
  127.     sf::Sprite sprite;
  128.     sprite.setTexture(texture);
  129.  
  130.     while (window.isOpen()) {
  131.         sf::Event event;
  132.         while (window.pollEvent(event)) {
  133.             if (event.type == sf::Event::Closed) {
  134.                 window.close();
  135.             }
  136.             if (event.type == sf::Event::KeyPressed) {
  137.                 topLeft.color = sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256);
  138.                 topRight.color = sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256);
  139.                 botLeft.color = sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256);
  140.                 botRight.color = sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256);
  141.                 ToColor(image, topLeft, topRight, botLeft, botRight);
  142.                 texture.update(image);
  143.             }
  144.         }
  145.         window.clear(sf::Color::Black);
  146.  
  147.         window.draw(sprite);
  148.         window.display();
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement