diff --git a/lessons/0031_mandelbrot_colors/images/screen.png b/lessons/0031_mandelbrot_colors/images/screen.png new file mode 100644 index 0000000..e61992b Binary files /dev/null and b/lessons/0031_mandelbrot_colors/images/screen.png differ diff --git a/lessons/0031_mandelbrot_colors/main.cpp b/lessons/0031_mandelbrot_colors/main.cpp new file mode 100644 index 0000000..b2fa7df --- /dev/null +++ b/lessons/0031_mandelbrot_colors/main.cpp @@ -0,0 +1,85 @@ +#include +#include +#include "../../vsgl2.h" + +using namespace std; +using namespace vsgl2; +using namespace vsgl2::general; +using namespace vsgl2::video; +using namespace vsgl2::utils; + +const int RADIUS = 10E12; +const int MAX_ITERATIONS = 50; + +const double X_BEGIN = -2; +const double Y_BEGIN = -1.25; +const double X_END= 0.5; +const double Y_END= 1.25; +const double X_RANGE = X_END - X_BEGIN; +const double Y_RANGE = Y_END - Y_BEGIN; + + +struct Complex{ + double real, imm; +}; + +Complex sum(Complex r, Complex s) +{ + Complex result; + result.real = r.real + s.real; + result.imm= r.imm + s.imm; + return result; +} + +Complex multiply(Complex a, Complex b) +{ + Complex result; + result.real = a.real*b.real - a.imm*b.imm; + result.imm = a.real*b.imm + a.imm*b.real; + return result; +} + +double mod(Complex c) +{ + return sqrt(c.real*c.real + c.imm*c.imm); +} + +int diverge(Complex C) +{ + int i; + Complex Z; + Z.real = 0; + Z.imm = 0; + for (i = 0; i < MAX_ITERATIONS && mod(Z) < RADIUS; i++) + Z = sum(multiply(Z,Z),C); + return i; +} + +int main(int argc, char* argv[]) { + int i, j; + init(); + //create the window and show it + set_window(640,640,"Vsgl2 Mandelbrot"); + //Iterate on a region of the plane to + //draw the Mandelbrot set + for (i = 0; i < get_window_height() ; i++) + for (j = 0; j < get_window_width() ; j++) + { + Complex c; + c.real = i*X_RANGE/get_window_width() + X_BEGIN; + c.imm = j*Y_RANGE/get_window_height() + Y_BEGIN; + int n = diverge(c); + draw_point(i,j, + Color(128 + n*128/MAX_ITERATIONS , + n*128/MAX_ITERATIONS, + n*768/MAX_ITERATIONS%256, + 255)); + } + //update the screen + update(); + //the program waits for any button to be pressed + wait_for_button_pressed(); + //close the library and clean up everything + close(); + return 0; +} diff --git a/lessons/0031_mandelbrot_colors/readme.md b/lessons/0031_mandelbrot_colors/readme.md new file mode 100644 index 0000000..a84d8c3 --- /dev/null +++ b/lessons/0031_mandelbrot_colors/readme.md @@ -0,0 +1,14 @@ +# Mandelbrot set with colors +This example extends the one about Mandelbrot set adding colors to the image, obtaining the following result + +![Mandelbrot set colored](./images/screen.png) + +The coloring is obtained changing the returned value from the ```diverge``` function: this time the number of iterations where Z becomes too large (greater than RADIUS) will be returned and this number will be turned into a color using the following formula: +```c +int n = diverge(c); +draw_point(i,j, + Color(128 + n*128/MAX_ITERATIONS , + n*128/MAX_ITERATIONS, + n*768/MAX_ITERATIONS%256, + 255)); +``` diff --git a/lessons/0031_mandelbrot_colors/vsgl2.cbp b/lessons/0031_mandelbrot_colors/vsgl2.cbp new file mode 100644 index 0000000..f1b184c --- /dev/null +++ b/lessons/0031_mandelbrot_colors/vsgl2.cbp @@ -0,0 +1,74 @@ + + + + + +