Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <SFML/Window.hpp>
- #include <algorithm>
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <random>
- #include <cstdlib>
- #include <ctime>
- int range = 10;
- //class Complex {
- //private:
- // double re;
- // double im;
- //public:
- //
- // Complex(double re, double im) {
- // this->re = re;
- // this->im = im;
- // }
- // void setre(double re) {
- // this->re = re;
- // }
- // void setim(double im){
- // this->im = im;
- // }
- // double getre(){
- // return this->re;
- // }
- // double getim(){
- // return this->im;
- // }
- // double module() {
- // return sqrt(this->re * this->re + this->im * this->im);
- // }
- //
- // Complex operator+(Complex cn){
- // return Complex(this->re + cn.getre(), this->im + cn.getim());
- // }
- //
- // Complex operator*(Complex cn) {
- // return Complex(this->re * cn.getre() - this->im * cn.getim(), this->im * cn.getre() + this->re * cn.getim());
- // }
- //
- //};
- struct Pixel{
- sf::Vector2i cords;
- sf::Color color;
- Pixel(sf::Vector2i cords, sf::Color color) {
- this->cords = cords;
- this->color = color;
- }
- };
- void ToColor(sf::Image& image, Pixel topLeft, Pixel topRight, Pixel botLeft, Pixel botRight, int iteration = 1024) {
- if (topRight.cords.x - topLeft.cords.x <= 1 || iteration == 1) {
- return;
- }
- sf::Color col1(abs(topLeft.color.r - topRight.color.r) / 2 + std::min(topLeft.color.r, topRight.color.r),
- abs(topLeft.color.g - topRight.color.g) / 2 + std::min(topLeft.color.g, topRight.color.g),
- abs(topLeft.color.b - topRight.color.b) / 2 + std::min(topLeft.color.b, topRight.color.b) );
- sf::Color col2(abs(topLeft.color.r - botLeft.color.r) / 2 + std::min(topLeft.color.r, botLeft.color.r),
- abs(topLeft.color.g - botLeft.color.g) / 2 + std::min(topLeft.color.g, botLeft.color.g),
- abs(topLeft.color.b - botLeft.color.b) / 2 + std::min(topLeft.color.b, botLeft.color.b));
- sf::Color col3(abs(topRight.color.r - botRight.color.r) / 2 + std::min(topRight.color.r, botRight.color.r),
- abs(topRight.color.g - botRight.color.g) / 2 + std::min(topRight.color.g, botRight.color.g),
- abs(topRight.color.b - botRight.color.b) / 2 + std::min(topRight.color.b, botRight.color.b)) ;
- sf::Color col4(abs(botLeft.color.r - botRight.color.r) / 2 + std::min(botLeft.color.r, botRight.color.r),
- abs(botLeft.color.g - botRight.color.g) / 2 + std::min(botLeft.color.g, botRight.color.g),
- abs(botLeft.color.b - botRight.color.b) / 2 + std::min(botLeft.color.b, botRight.color.b));
- Pixel topMid(topLeft.cords + (topRight.cords - topLeft.cords) / 2, col1);
- Pixel leftMid(topLeft.cords + (botLeft.cords - topLeft.cords) / 2, col2);
- Pixel rightMid(topRight.cords + (botRight.cords - topRight.cords) / 2, col3);
- Pixel botMid(botLeft.cords + (botRight.cords - botLeft.cords) / 2, col4);
- int red = abs(col2.r - col3.r) + std::min(col2.r, col3.r) - range + std::rand() % range * 2;
- int green = abs(col2.g - col3.g) + std::min(col2.g, col3.g) - range + std::rand() % range * 2;
- int blue = abs(col2.b - col3.b) + std::min(col2.b, col3.b) - range + std::rand() % range * 2;
- //std::cout << red << " " << green << " " << blue << " ";
- sf::Color midCol(red, green, blue);
- 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);
- image.setPixel(topMid.cords.x, topMid.cords.y, col1);
- image.setPixel(leftMid.cords.x, leftMid.cords.y, col2);
- image.setPixel(rightMid.cords.x, rightMid.cords.y, col3);
- image.setPixel(botMid.cords.x, botMid.cords.y, col4);
- image.setPixel(center.cords.x, center.cords.y, midCol);
- ToColor(image, topLeft, topMid, leftMid, center, iteration / 2);
- ToColor(image, topMid, topRight, center, rightMid, iteration / 2);
- ToColor(image, leftMid, center, botLeft, botMid, iteration / 2);
- ToColor(image, center, rightMid, botMid, botRight, iteration / 2);
- }
- int main() {
- sf::RenderWindow window(sf::VideoMode(1025, 1025), "AA");
- std::srand(std::time(nullptr));
- int iteration = 0;
- sf::Image image;
- image.create(1025, 1025, sf::Color::Green);
- Pixel topLeft(sf::Vector2i(0, 0), sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256));
- Pixel topRight(sf::Vector2i(1024, 0), sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256));
- Pixel botLeft(sf::Vector2i(0, 1024), sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256));
- Pixel botRight(sf::Vector2i(1024, 1024), sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256));
- image.setPixel(topLeft.cords.x, topLeft.cords.y, topLeft.color);
- image.setPixel(topRight.cords.x, topRight.cords.y, topRight.color);
- image.setPixel(botLeft.cords.x, botLeft.cords.y, botLeft.color);
- image.setPixel(botRight.cords.x, botRight.cords.y, botRight.color);
- ToColor(image, topLeft, topRight, botLeft, botRight);
- sf::Texture texture;
- texture.create(1025, 1025);
- texture.update(image);
- sf::Sprite sprite;
- sprite.setTexture(texture);
- while (window.isOpen()) {
- sf::Event event;
- while (window.pollEvent(event)) {
- if (event.type == sf::Event::Closed) {
- window.close();
- }
- if (event.type == sf::Event::KeyPressed) {
- topLeft.color = sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256);
- topRight.color = sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256);
- botLeft.color = sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256);
- botRight.color = sf::Color(std::rand() % 256, std::rand() % 256, std::rand() % 256);
- ToColor(image, topLeft, topRight, botLeft, botRight);
- texture.update(image);
- }
- }
- window.clear(sf::Color::Black);
- window.draw(sprite);
- window.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement