diff --git a/Fractal.cpp b/Fractal.cpp index 5ec3d69..b4bbb5e 100644 --- a/Fractal.cpp +++ b/Fractal.cpp @@ -3,7 +3,7 @@ #include #include -Fractal::Fractal(int image_width, int image_height, Domain domain, std::function (std::complex, std::complex)> fractal_function, std::function render_function) : domain(domain) { +Fractal::Fractal(int image_width, int image_height, Domain domain, std::function, int)> fractal_function, std::function render_function) : domain(domain) { this->fractal_function = fractal_function; this->render_function = render_function; this->image_height = image_height; @@ -12,7 +12,7 @@ Fractal::Fractal(int image_width, int image_height, Domain domain, std::function this->hasChanged = true; } -void Fractal::setFractalFunction(std::function (std::complex, std::complex)> fractal_function) { +void Fractal::setFractalFunction(std::function, int)> fractal_function) { this->fractal_function = fractal_function; this->hasChanged = true; } @@ -22,20 +22,6 @@ void Fractal::setRenderFunction(std::functionhasChanged = true; } -int Fractal::compute_point(std::complex point, int max_iterations) { - - std::complex z(0); - int iter = 0; - - while (abs(z) < 2.0 && iter < max_iterations) { - z = this->fractal_function(z, point); - iter++; - } - - return iter; - -} - std::complex Fractal::scale_point(std::complex point) { std::complex aux(point.real() / (double)this->image_width * this->domain.width() + this->domain.x_min, point.imag() / (double)this->image_height * this->domain.height() + domain.y_min); return aux; @@ -54,7 +40,7 @@ sf::Image Fractal::getFrame(){ for(int x = 0; x < this->image_width; x++) { std::complex point(x, y); point = scale_point(point); - int iterations = compute_point(point, max_iterations); + int iterations = this->fractal_function(point, max_iterations); sf::Color color = this->render_function(iterations, max_iterations); this->frame.setPixel(x, y, color); } diff --git a/Fractal.hpp b/Fractal.hpp index 225031c..6e42eb0 100644 --- a/Fractal.hpp +++ b/Fractal.hpp @@ -83,14 +83,13 @@ class Fractal { private: int image_width, image_height; - std::function(std::complex, std::complex)> fractal_function; - std::function render_function; + std::function, int)> fractal_function; + std::function render_function; sf::Image frame; bool hasChanged; Domain domain; std::complex scale_point(std::complex point); - int compute_point(std::complex point, int max_iterations); int compute_max_iterations(int window_width, double domain_width); public: @@ -98,13 +97,13 @@ public: Fractal(int image_width, int image_height, Domain domain, - std::function(std::complex, std::complex)> fractal_function = fractal_mandelbrot, - std::function render_function = render_smooth + std::function, int)> fractal_function = fractal_function_template, + std::function render_function = render_smooth ); - void setFractalFunction(std::function(std::complex, std::complex)> fractal_function); + void setFractalFunction(std::function, int)> fractal_function); - void setRenderFunction(std::function render_function); + void setRenderFunction(std::function render_function); void moveTo(int x, int y); diff --git a/functions.hpp b/functions.hpp index ac24e62..9486cb4 100644 --- a/functions.hpp +++ b/functions.hpp @@ -4,6 +4,23 @@ #include #include +typedef std::complex (*fractal_function)(std::complex, std::complex); + +template +int fractal_function_template(std::complex point, int max_iterations) { + + std::complex z(0); + int iter = 0; + + while (abs(z) < 2.0 && iter < max_iterations) { + z = F(z, point); + iter++; + } + + return iter; + +} + inline std::complex fractal_mandelbrot(std::complex z, std::complex c) { return (z * z) + c; } diff --git a/main.cpp b/main.cpp index 0a61694..a856f22 100644 --- a/main.cpp +++ b/main.cpp @@ -192,22 +192,22 @@ int main(int argc, char **argv) { // Fractal selection case sf::Keyboard::Key::Num1: std::cerr << "Selected mandelbrot algorithm" << std::endl; - mandelbrot.setFractalFunction(fractal_mandelbrot); + mandelbrot.setFractalFunction(fractal_function_template); break; case sf::Keyboard::Key::Num2: std::cerr << "Selected triple mandelbrot algorithm" << std::endl; - mandelbrot.setFractalFunction(fractal_triple_mandelbrot); + mandelbrot.setFractalFunction(fractal_function_template); break; case sf::Keyboard::Key::Num3: std::cerr << "Selected quadruple mandelbrot algorithm" << std::endl; - mandelbrot.setFractalFunction(fractal_quadruple_mandelbrot); + mandelbrot.setFractalFunction(fractal_function_template); break; case sf::Keyboard::Key::Num4: std::cerr << "Selected quintuple mandelbrot algorithm" << std::endl; - mandelbrot.setFractalFunction(fractal_quintuple_mandelbrot); + mandelbrot.setFractalFunction(fractal_function_template); break; default: