-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from alessandro-bugatti/Lesson_31
Add Mandelbrot with colors lesson
- Loading branch information
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include <iostream> | ||
#include <algorithm> | ||
#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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> | ||
<CodeBlocks_project_file> | ||
<FileVersion major="1" minor="6" /> | ||
<Project> | ||
<Option title="vsgl2_mandelbrot" /> | ||
<Option pch_mode="2" /> | ||
<Option compiler="gcc" /> | ||
<Build> | ||
<Target title="Win32 Debug"> | ||
<Option output="win32/bin/Debug/$(PROJECT_NAME)" prefix_auto="1" extension_auto="1" /> | ||
<Option object_output="win32/obj/Debug/" /> | ||
<Option type="1" /> | ||
<Option compiler="gcc" /> | ||
<Compiler> | ||
<Add option="-g" /> | ||
</Compiler> | ||
<Linker> | ||
<Add option="-lmingw32 -lSDL2main -lSDL2 -lSDL2_net -lSDL2_image -lSDL2_mixer -lSDL2_ttf" /> | ||
</Linker> | ||
</Target> | ||
<Target title="Win32 Release"> | ||
<Option output="win32/bin/Release/$(PROJECTNAME)" prefix_auto="1" extension_auto="1" /> | ||
<Option object_output="win32/obj/Release/" /> | ||
<Option type="0" /> | ||
<Option compiler="gcc" /> | ||
<Compiler> | ||
<Add option="-O2" /> | ||
</Compiler> | ||
<Linker> | ||
<Add option="-s" /> | ||
<Add option="-lmingw32 -lSDL2main -lSDL2 -lSDL2_net -lSDL2_image -lSDL2_mixer -lSDL2_ttf" /> | ||
</Linker> | ||
</Target> | ||
<Target title="Linux Debug"> | ||
<Option output="linux/bin/Debug/$(PROJECTNAME)" prefix_auto="1" extension_auto="0" /> | ||
<Option object_output="linux/obj/Debug/" /> | ||
<Option type="1" /> | ||
<Option compiler="gcc" /> | ||
<Compiler> | ||
<Add option="-g" /> | ||
</Compiler> | ||
<Linker> | ||
<Add option="-lSDL2main -lSDL2 -lSDL2_net -lSDL2_image -lSDL2_mixer -lSDL2_ttf" /> | ||
</Linker> | ||
</Target> | ||
<Target title="Linux Release"> | ||
<Option output="linux/bin/Release/$(PROJECTNAME)" prefix_auto="1" extension_auto="0" /> | ||
<Option object_output="linux/obj/Release/" /> | ||
<Option type="0" /> | ||
<Option compiler="gcc" /> | ||
<Compiler> | ||
<Add option="-O2" /> | ||
</Compiler> | ||
<Linker> | ||
<Add option="-s" /> | ||
<Add option="-lSDL2main -lSDL2 -lSDL2_net -lSDL2_image -lSDL2_mixer -lSDL2_ttf" /> | ||
</Linker> | ||
</Target> | ||
</Build> | ||
<Compiler> | ||
<Add option="-Wall" /> | ||
<Add option="-std=c++11" /> | ||
</Compiler> | ||
<Unit filename="../../vsgl2.cpp" /> | ||
<Unit filename="../../vsgl2.h" /> | ||
<Unit filename="main.cpp" /> | ||
<Extensions> | ||
<code_completion /> | ||
<envvars /> | ||
<debugger /> | ||
<lib_finder disable_auto="1" /> | ||
</Extensions> | ||
</Project> | ||
</CodeBlocks_project_file> |