Untitled
unknown
c_cpp
2 years ago
2.6 kB
24
Indexable
#include <SFML/Graphics.hpp>
#include <iostream>
struct complex {
double x{};
double y{};
complex(double x, double y) {
this->x = x;
this->y = y;
}
complex() {};
};
complex operator*(const complex left, const complex right) {
complex Z;
Z.x = left.x * right.x - left.y * right.y;
Z.y = left.x * right.y + left.y * right.x;
return Z;
}
complex operator+(const complex& left, const complex right) {
return complex(left.x + right.x, left.y + right.y);
}
complex operator-(const complex& left, const complex right) {
return complex(left.x - right.x, left.y - right.y);
}
double pow(const double num, const int power) {
double count = 1;
for (int i = 0; i < power; i++) {
count *= num;
}
return count;
}
complex complex_square(const complex Z) {
return complex(pow(Z.x,2) - pow(Z.y, 2), 2 * Z.x * Z.y);
}
void Mandelbrot(sf::Image& Image, const int Width, const int Height, const int max_iter = 100,const double min_x = -2.5, const double max_x = 1, const double min_y = -1, const double max_y = -1) {
complex Z{};
complex C{};
int iter{};
int color{};
double cf_x{}, cf_y;
for (float x = 0; x < Width; x++) {
for (float y = 0; y < Height; y++) {
color = 255;
cf_x = float(x / float(Width));
cf_y = float(y / float(Height));
C.x = (max_x - min_x) * cf_x + min_x;
C.y = (max_y - min_y) * cf_y + min_y;
Z.x = 0; Z.y = 0;
for (iter = 0; iter < max_iter; iter++) {
Z = { complex_square(Z) + C};
if (Z.x * Z.x + Z.y * Z.y > 2 * 2) {
color = 0;
break;
}
}
Image.setPixel(x, y, sf::Color(color, color, color));
}
}
};
int main()
{
const int Width = 640;
const int Height = 360;
sf::RenderWindow window(sf::VideoMode(Width, Height), "Fractals");
sf::Image Image; Image.create(Width, Height);
sf::Texture texture;
sf::Sprite Fractal;
Mandelbrot(Image, Width, Height);
texture.loadFromImage(Image);
Fractal.setTexture(texture);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(Fractal);
window.display();
}
return 0;
}Editor is loading...
Leave a Comment