mandelbrot

simple mandelbrot visualizer
Index Commits Files Refs README LICENSE
commit efc9734e1448d509126d60a1ab383eb463c2377f
parent e510ebeee0b7e99669ae6a86e9b4c8aece0c60c5
Author: mjkloeckner <martin.cachari@gmail.com>
Date:   Sun, 25 Dec 2022 21:48:37 -0300

adds function get_iteration() and removes unnecessary dependency

Diffstat:
M.gitignore | 1+
Mmain.c | 38+++++++++++++++++++++++++-------------
Msdl_extra.c | 4----
Msdl_extra.h | 1-
4 files changed, 26 insertions(+), 18 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,3 +1,4 @@
 .gitignore
 mandelbrot
 screenshot.png
+tags
diff --git a/main.c b/main.c
@@ -3,7 +3,7 @@
 #include <complex.h>
 
 #define BG_COLOR    0x191919FF
-#define MAX_IT        100
+#define MAX_IT        500
 
 #define DEFAULT_WIN_WIDTH     1024
 #define DEFAULT_WIN_HEIGHT     720
@@ -24,28 +24,40 @@ 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;
 }
 
+size_t get_iterations(float complex z0) {
+    size_t it;
+    float complex z = 0.0 + 0.0 * I;
+
+    for(it = 0; (it <= MAX_IT) && (Re(z)*Re(z) + Im(z)*Im(z) <= 4); it++)
+        z = Re(z)*Re(z) - Im(z)*Im(z) + Re(z0) + (2*Re(z)*Im(z) + Im(z0)) * I;
+
+    return it;
+}
+
 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;
+    float complex z0;
+    float fg;
     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) && (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;
-            }
+            z0 = map(i, 0, width, -2.50, 1.50) + map(j, 0, height, -1.00, 1.00) * I;
+
+            it = get_iterations(z0);
 
             fg = map(it, 0, MAX_IT, 0, 1);
             uint8_t b = map(sqrt(fg), 0, 1, 0, 255);
 
-            if(it == MAX_IT) b = 0;
-
             uint32_t color = (b << 24) + (b << 16) + (b << 8) + 0xFF;
+
+            if(it == MAX_IT) {
+                color = (0xFF << 24) + (0x00 << 16) + (0x00 << 8) + 0x00;
+                printf("MAX_IT reached\n");
+            }
+
             SDL_SetRenderDrawColor(rend, UNHEX(color));
-            SDL_RenderDrawPoint(rend, (win_w / 2) - (width / 2) + i, (win_h / 2) - (height / 2) + j);
+            SDL_RenderDrawPoint(rend,
+                    (win_w / 2) - (width / 2) + i,
+                    (win_h / 2) - (height / 2) + j);
         }
     }
 }
diff --git a/sdl_extra.c b/sdl_extra.c
@@ -18,9 +18,6 @@ status_t SDL_setup(SDL_Window **win, SDL_Renderer **rend) {
     if ((*win == NULL) || (*rend == NULL))
         return ERROR_NULL_POINTER;
 
-    if(TTF_Init() == -1)
-        return ERROR_SDL_FONT_INIT;
-
     return OK;
 }
 
@@ -28,7 +25,6 @@ status_t SDL_cleanup(SDL_Window *win, SDL_Renderer *rend) {
     if((win == NULL) || (rend == NULL))
         return ERROR_NULL_POINTER;
 
-    TTF_Quit();
     SDL_DestroyRenderer(rend);
     SDL_DestroyWindow(win);
     SDL_Quit();
diff --git a/sdl_extra.h b/sdl_extra.h
@@ -4,7 +4,6 @@
 #include "status.h"
 
 #include <SDL2/SDL.h>
-#include <SDL2/SDL_ttf.h>
 
 #define WIN_TITLE "mandelbrot visualizer"