From aec0734fcc34b77d706a573bd1909e47a9bf1a83 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 18 Jan 2024 17:18:46 +0100 Subject: [PATCH] Refactor mandelbrot const vars to setting variable 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 | 25 ++++++++++++++++++++++++- main.c | 19 ++++++++++--------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/fractol.h b/fractol.h index 1d89b7f..da1a029 100644 --- a/fractol.h +++ b/fractol.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 419b81a..d65be5f 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; -- 2.30.2