commit fa4ba3a035835da522d53a21275696ebd11452f1
parent 91e0e06fc00293af9438eb3adc81ea66e601cae1
Author: mjkloeckner <martin.cachari@gmail.com>
Date: Fri, 23 Dec 2022 13:29:00 -0300
make mandelbrot set centerer on window and keep aspect ratio
Diffstat:
2 files changed, 27 insertions(+), 20 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1 +1,3 @@
+.gitignore
mandelbrot
+screenshot.png
diff --git a/main.c b/main.c
@@ -5,31 +5,36 @@
#define BG_COLOR 0x191919FF
#define MAX_IT 100
+#define DEFAULT_WIN_WIDTH 1024
+#define DEFAULT_WIN_HEIGHT 720
+
+#define WIDTH 640
+#define HEIGHT 480
+
#define UNHEX(color) \
((color) >> (8 * 3)) & 0xFF, \
((color) >> (8 * 2)) & 0xFF, \
((color) >> (8 * 1)) & 0xFF, \
((color) >> (8 * 0)) & 0xFF
-float map(float x, float in_min, float in_max, float out_min, float out_max);
-void render_mandelbrot(int win_w, int win_h, SDL_Renderer *rend);
+#define Re(z) creal(z)
+#define Im(z) cimag(z)
float map(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
-void render_mandelbrot(int win_w, int win_h, SDL_Renderer *rend) {
+void render_mandelbrot(int win_w, int win_h, int width, int height, SDL_Renderer *rend) {
size_t i, j, it;
float complex z0, z;
float real, imag, fg;
- for(i = 0; i < win_w; i++) {
- for(j = 0; j < win_w; j++) {
- /* z0 = map(i, 0, win_w, -2.00, 0.47) + map(j, 0, win_h, -1.12, 1.12) * I; */
- z0 = map(i, 0, win_w, -2.00, 1.00) + map(j, 0, win_h, -1.50, 1.50) * I;
+ for(i = 0; i < width; i++) {
+ for(j = 0; j < height; j++) {
+ z0 = map(i, 0, width, -5.50, 2.50) + map(j, 0, height, -2.00, 2.00) * I;
z = 0.0 + 0.0 * I;
- for(it = 0; (it <= MAX_IT) && (creal(z)*creal(z) + cimag(z)*cimag(z) <= 2*2); it++) {
- real = creal(z)*creal(z) - cimag(z)*cimag(z) + creal(z0);
- imag = 2*creal(z)*cimag(z) + cimag(z0);
+ for(it = 0; (it <= MAX_IT) && (Re(z)*Re(z) + Im(z)*Im(z) <= 4); it++) {
+ real = Re(z)*Re(z) - Im(z)*Im(z) + Re(z0);
+ imag = 2*Re(z)*Im(z) + Im(z0);
z = real + imag * I;
}
@@ -40,7 +45,7 @@ void render_mandelbrot(int win_w, int win_h, SDL_Renderer *rend) {
uint32_t color = (b << 24) + (b << 16) + (b << 8) + 0xFF;
SDL_SetRenderDrawColor(rend, UNHEX(color));
- SDL_RenderDrawPoint(rend, i, j);
+ SDL_RenderDrawPoint(rend, (win_w / 2) - (width / 2) + i, (win_h / 2) - (height / 2) + j);
}
}
}
@@ -49,17 +54,16 @@ int main (int argc, char *argv[]) {
SDL_Window *win;
SDL_Renderer *rend;
SDL_Event event;
+
status_t st;
bool run = true;
- int win_w, win_h;
+ int win_w = DEFAULT_WIN_WIDTH, win_h = DEFAULT_WIN_HEIGHT;
if((st = SDL_setup(&win, &rend)) != OK) {
fprintf(stderr, "%s: couldn't initialize SDL2", argv[0]);
return st;
}
- win_w = 800;
- win_h = 600;
while(run) {
while(SDL_PollEvent(&event)) {
switch(event.type) {
@@ -71,6 +75,13 @@ int main (int argc, char *argv[]) {
case SDL_WINDOWEVENT_RESIZED:
win_w = event.window.data1;
win_h = event.window.data2;
+
+ SDL_SetRenderDrawColor(rend, UNHEX(BG_COLOR));
+ SDL_RenderClear(rend);
+
+ render_mandelbrot(win_w, win_h, 1600, 800, rend);
+
+ SDL_RenderPresent(rend);
break;
default: break;
}
@@ -78,12 +89,6 @@ int main (int argc, char *argv[]) {
}
}
- SDL_SetRenderDrawColor(rend, UNHEX(BG_COLOR));
- SDL_RenderClear(rend);
-
- render_mandelbrot(win_w, win_h, rend);
-
- SDL_RenderPresent(rend);
}
SDL_cleanup(win, rend);