Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <SFML/Graphics.hpp>
- struct point {
- double x, y;
- point() {}
- point(double _x, double _y) {
- this->x = _x;
- this->y = _y;
- }
- };
- struct pixel {
- double x, y;
- pixel() {}
- pixel(double _x, double _y) {
- this->x = _x;
- this->y = _y;
- }
- };
- struct complex {
- double Re, Im;
- complex(): Re(0), Im(0) {}
- complex(double x, double y) {
- this->Re = x;
- this->Im = y;
- }
- friend complex operator-(const complex &a) {
- return {-a.Re, -a.Im};
- }
- friend complex operator+(const complex &a, const complex &b) {
- return {a.Re + b.Re, a.Im + b.Im};
- }
- friend complex operator-(const complex &a, const complex &b) {
- return a + (-b);
- }
- friend double dot_product(const complex &a, const complex &b) {
- return a.Re * b.Re + a.Im * b.Im;
- }
- friend double cross_product(const complex &a, const complex &b) {
- return a.Re * b.Im - a.Im * b.Re;
- }
- friend complex operator*(const complex &a, const complex &b) {
- return {a.Re * b.Re - a.Im * b.Im, a.Re * b.Im + a.Im * b.Re};
- }
- complex pow(int n) {
- complex ans = {1, 0}, step = {Re, Im};
- while (n > 0) {
- if (n % 2 == 1) {
- ans = ans * step;
- }
- ans = ans * ans;
- n /= 2;
- }
- return ans;
- }
- double abs() {
- return sqrt(Re * Re + Im * Im);
- }
- };
- const int W = 1080, H = 720, MAX_ITERS = 200,
- repeat_gradient = 100;
- double scale = 200;
- pixel centre{W / 2, H / 2};
- sf::Uint8* pixels = new sf::Uint8[W * H * 4];
- int gradient_size;
- const sf::Uint8* gradient;
- point pixel_to_point(const pixel &a) {
- return {(a.x - centre.x) / scale, (centre.y - a.y) / scale};
- }
- void make_fractal(complex c) {
- for (int i = 0; i < W; ++i) {
- for (int j = 0; j < H; ++j) {
- point p = pixel_to_point({double(i), double(j)});
- complex z = {p.x, p.y};
- int it;
- double r = 5;
- double r2 = r * r;
- for (it = 0; it < MAX_ITERS; ++it) {
- if (z.abs() * z.abs() >= r2) {
- break;
- }
- z = z.pow(2) + c;
- }
- double len = z.abs();
- double m = 10 + double(it) - log2(log2(len));
- int gradient_id = int(m * repeat_gradient) * 4;
- int id = 4 * (j * W + i);
- if (it == MAX_ITERS) {
- pixels[id] = pixels[id + 1] = pixels[id + 2] = 0;
- pixels[id + 3] = 255;
- }
- else {
- pixels[id] = gradient[gradient_id];
- pixels[id + 1] = gradient[gradient_id + 1];
- pixels[id + 2] = gradient[gradient_id + 2];
- pixels[id + 3] = gradient[gradient_id + 3];
- }
- }
- }
- }
- int main() {
- sf::RenderWindow window(sf::VideoMode(W, H), "SFML works!");
- sf::Image gradient_image;
- gradient_image.loadFromFile("./1.png");
- gradient = gradient_image.getPixelsPtr();
- gradient_size = gradient_image.getSize().x * gradient_image.getSize().y * 4;
- sf::Texture texture;
- texture.create(W, H);
- sf::Sprite sprite(texture);
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed) {
- window.close();
- }
- /*else if (event.type == sf::Event::MouseButtonPressed) {
- pressed = true;
- mouse_pos = {event.mouseButton.x, event.mouseButton.y};
- }
- else if (event.type == sf::Event::MouseButtonReleased) {
- pressed = false;
- pixel current = {event.mouseButton.x, event.mouseButton.y};
- }*/
- }
- make_fractal({0.377, -0.248});
- texture.update(pixels);
- window.clear();
- window.draw(sprite);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement