Refactor mandelbrot const vars to setting variable
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 18 Jan 2024 16:18:46 +0000 (17:18 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 18 Jan 2024 16:18:46 +0000 (17:18 +0100)
Add settings (t_set set) as a member to t_session.
It is constructed as a union of settings type of each fractal, which may
prove useless if the settings for mandelbrot (t_set_man)
cover all the needs of other fractals. This seems unlikely however.
Add t_set_jul as a placeholder for the concept to be illustrative.

fractol.h
main.c

index 1d89b7f38af882d47c611e4e1bb0a23b3c54255f..da1a0291b3a9a446a0f037879521ad51645ad573 100644 (file)
--- a/fractol.h
+++ b/fractol.h
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/11/11 18:51:29 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/01/18 10:11:39 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/01/18 17:12:40 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -41,12 +41,35 @@ struct s_view
 };
 typedef struct s_view  t_view;
 
+struct s_set_man
+{
+       int     detail;
+       int     threshold;
+       int     color_stability;
+};
+typedef struct s_set_man       t_set_man;
+
+struct s_set_jul
+{
+       int     detail;
+};
+
+typedef struct s_set_jul       t_set_jul;
+
+union u_set
+{
+       t_set_man       man;
+       t_set_jul       jul;
+};
+typedef union u_set    t_set;
+
 struct s_session
 {
        void    *mlx;
        void    *win;
        t_img   img;
        t_view  view;
+       t_set   set;
 };
 typedef struct s_session       t_session;
 
diff --git a/main.c b/main.c
index 419b81a01b7312c00c337f6c70f7687a0b429833..d65be5fd21effca1ed0e2fe61ac5e9be28b4f82f 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/10/27 14:29:26 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/01/18 10:44:15 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/01/18 17:14:33 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -64,10 +64,8 @@ void free_session(t_session *s)
        return ;
 }
 
-double mandelbrot(double x, double y)
+double mandelbrot(t_set_man *settings, double x, double y)
 {
-       const double    threshold = 1000;
-       const double    detail = 10;
        int                             count;
        t_complex               c;
        t_complex               z;
@@ -78,15 +76,15 @@ double      mandelbrot(double x, double y)
        z.r = 0;
        z.i = 0;
        if (complex_norm(c) > 4)
-               return (0.01);
-       while (complex_norm(z) < threshold && count < 100 * detail)
+               return (0);
+       while (complex_norm(z) < settings->threshold && count < settings->detail)
        {
                z = complex_add(complex_mul(z, z), c);
                ++count;
        }
-       if (count == 100 * detail)
+       if (count == settings->detail)
                return (-1);
-       return (fmod((count + 1 - log(log(complex_norm(z)) / log(2)) / log(2)) / 100., 1.));
+       return (fmod((double)(count + 1 - log(log(complex_norm(z)) / log(2)) / log(2)) / settings->color_stability, 1.));
 }
 
 t_color        get_img_pixel_color(t_img *img, int x, int y)
@@ -118,7 +116,7 @@ t_color     get_color(t_session *s, int x, int y)
 
        if (!*(bool *)ft_mat_access(&s->img.calced, x, y))
        {
-               palette_param = s->view.fractal(
+               palette_param = s->view.fractal(&s->set,
                                s->view.window_coord.x + s->view.pixel_size.x * x,
                                s->view.window_coord.y - s->view.pixel_size.y * y);
                return (s->view.palette(palette_param));
@@ -299,6 +297,9 @@ void        parse_args(int argc, char **argv, t_session *s)
 {
        if (argc == 0)
                free(argv);
+       s->set.man.detail = 1000;
+       s->set.man.threshold = 1000;
+       s->set.man.color_stability = 100;
        s->img.width = 1000;
        s->img.height = 1000;
        s->img.undersample_max = 5;