mirror of
https://github.com/Andreabont/OpenMandelbrot.git
synced 2024-11-24 00:07:57 +00:00
Ottimizzazione
This commit is contained in:
parent
2fd5cb5d36
commit
936f7a9d29
20
Fractal.cpp
20
Fractal.cpp
@ -3,7 +3,7 @@
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
Fractal::Fractal(int image_width, int image_height, Domain domain, std::function<std::complex<double> (std::complex<double>, std::complex<double>)> fractal_function, std::function<sf::Color(int iteration_number, int max_iterations)> render_function) : domain(domain) {
|
||||
Fractal::Fractal(int image_width, int image_height, Domain domain, std::function<int (std::complex<double>, int)> fractal_function, std::function<sf::Color(int, int)> 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<double> (std::complex<double>, std::complex<double>)> fractal_function) {
|
||||
void Fractal::setFractalFunction(std::function<int (std::complex<double>, int)> fractal_function) {
|
||||
this->fractal_function = fractal_function;
|
||||
this->hasChanged = true;
|
||||
}
|
||||
@ -22,20 +22,6 @@ void Fractal::setRenderFunction(std::function<sf::Color (int iteration_number, i
|
||||
this->hasChanged = true;
|
||||
}
|
||||
|
||||
int Fractal::compute_point(std::complex<double> point, int max_iterations) {
|
||||
|
||||
std::complex<double> z(0);
|
||||
int iter = 0;
|
||||
|
||||
while (abs(z) < 2.0 && iter < max_iterations) {
|
||||
z = this->fractal_function(z, point);
|
||||
iter++;
|
||||
}
|
||||
|
||||
return iter;
|
||||
|
||||
}
|
||||
|
||||
std::complex<double> Fractal::scale_point(std::complex<double> point) {
|
||||
std::complex<double> 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<double> 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);
|
||||
}
|
||||
|
13
Fractal.hpp
13
Fractal.hpp
@ -83,14 +83,13 @@ class Fractal {
|
||||
private:
|
||||
|
||||
int image_width, image_height;
|
||||
std::function<std::complex<double>(std::complex<double>, std::complex<double>)> fractal_function;
|
||||
std::function<sf::Color(int iteration_number, int max_iterations)> render_function;
|
||||
std::function<int (std::complex<double>, int)> fractal_function;
|
||||
std::function<sf::Color(int, int)> render_function;
|
||||
sf::Image frame;
|
||||
bool hasChanged;
|
||||
Domain domain;
|
||||
|
||||
std::complex<double> scale_point(std::complex<double> point);
|
||||
int compute_point(std::complex<double> 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<double>(std::complex<double>, std::complex<double>)> fractal_function = fractal_mandelbrot,
|
||||
std::function<sf::Color(int iteration_number, int max_iterations)> render_function = render_smooth
|
||||
std::function<int (std::complex<double>, int)> fractal_function = fractal_function_template<fractal_mandelbrot>,
|
||||
std::function<sf::Color(int, int)> render_function = render_smooth
|
||||
);
|
||||
|
||||
void setFractalFunction(std::function<std::complex<double>(std::complex<double>, std::complex<double>)> fractal_function);
|
||||
void setFractalFunction(std::function<int (std::complex<double>, int)> fractal_function);
|
||||
|
||||
void setRenderFunction(std::function<sf::Color(int iteration_number, int max_iterations)> render_function);
|
||||
void setRenderFunction(std::function<sf::Color(int, int)> render_function);
|
||||
|
||||
void moveTo(int x, int y);
|
||||
|
||||
|
@ -4,6 +4,23 @@
|
||||
#include <complex>
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
typedef std::complex<double> (*fractal_function)(std::complex<double>, std::complex<double>);
|
||||
|
||||
template <fractal_function F>
|
||||
int fractal_function_template(std::complex<double> point, int max_iterations) {
|
||||
|
||||
std::complex<double> z(0);
|
||||
int iter = 0;
|
||||
|
||||
while (abs(z) < 2.0 && iter < max_iterations) {
|
||||
z = F(z, point);
|
||||
iter++;
|
||||
}
|
||||
|
||||
return iter;
|
||||
|
||||
}
|
||||
|
||||
inline std::complex<double> fractal_mandelbrot(std::complex<double> z, std::complex<double> c) {
|
||||
return (z * z) + c;
|
||||
}
|
||||
|
8
main.cpp
8
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<fractal_mandelbrot>);
|
||||
break;
|
||||
|
||||
case sf::Keyboard::Key::Num2:
|
||||
std::cerr << "Selected triple mandelbrot algorithm" << std::endl;
|
||||
mandelbrot.setFractalFunction(fractal_triple_mandelbrot);
|
||||
mandelbrot.setFractalFunction(fractal_function_template<fractal_triple_mandelbrot>);
|
||||
break;
|
||||
|
||||
case sf::Keyboard::Key::Num3:
|
||||
std::cerr << "Selected quadruple mandelbrot algorithm" << std::endl;
|
||||
mandelbrot.setFractalFunction(fractal_quadruple_mandelbrot);
|
||||
mandelbrot.setFractalFunction(fractal_function_template<fractal_quadruple_mandelbrot>);
|
||||
break;
|
||||
|
||||
case sf::Keyboard::Key::Num4:
|
||||
std::cerr << "Selected quintuple mandelbrot algorithm" << std::endl;
|
||||
mandelbrot.setFractalFunction(fractal_quintuple_mandelbrot);
|
||||
mandelbrot.setFractalFunction(fractal_function_template<fractal_quintuple_mandelbrot>);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
Loading…
Reference in New Issue
Block a user