From 678500b45c001653e4d1b15c7a9cb0d9cd754f72 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 19 Jan 2024 12:02:41 +0100 Subject: [PATCH] Add tricorn fractal, move fractals to separate file --- Makefile | 2 +- complex.c | 8 ++++- complex.h | 3 +- fractals.c | 78 +++++++++++++++++++++++++++++++++++++++++++ julia.c => fractals.h | 30 ++++++----------- main.c | 26 ++------------- 6 files changed, 101 insertions(+), 46 deletions(-) create mode 100644 fractals.c rename julia.c => fractals.h (59%) diff --git a/Makefile b/Makefile index 4b79f1d..ebb90c0 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ MLX := $(MLXDIR)libmlx_Linux.a LFTDIR := Libft/ LFT := $(LFTDIR)libft.a -SRCS := main.c complex.c color.c event_handling.c +SRCS := main.c complex.c color.c event_handling.c fractals.c OBJS := $(SRCS:%.c=%.o) all : $(NAME) diff --git a/complex.c b/complex.c index f8780fd..ca2acfc 100644 --- a/complex.c +++ b/complex.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/11 18:50:39 by ljiriste #+# #+# */ -/* Updated: 2023/11/11 18:50:41 by ljiriste ### ########.fr */ +/* Updated: 2024/01/19 10:56:22 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,12 @@ t_complex complex_mul(t_complex x, t_complex y) return (res); } +t_complex complex_conj(t_complex z) +{ + z.i = -z.i; + return (z); +} + double complex_norm(t_complex x) { return (sqrt(x.r * x.r + x.i * x.i)); diff --git a/complex.h b/complex.h index 3abdf57..c3bfa00 100644 --- a/complex.h +++ b/complex.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/11 18:51:27 by ljiriste #+# #+# */ -/* Updated: 2023/11/11 18:51:28 by ljiriste ### ########.fr */ +/* Updated: 2024/01/19 10:56:11 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,7 @@ typedef struct s_complex t_complex; t_complex to_complex(double x, double y); t_complex complex_add(t_complex x, t_complex y); t_complex complex_mul(t_complex x, t_complex y); +t_complex complex_conj(t_complex z); double complex_norm(t_complex x); #endif diff --git a/fractals.c b/fractals.c new file mode 100644 index 0000000..790fa35 --- /dev/null +++ b/fractals.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* fractals.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/05 19:57:50 by ljiriste #+# #+# */ +/* Updated: 2024/01/19 11:30:08 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "vect2.h" +#include "complex.h" +#include "fractol.h" +#include + +double general_julia(void *zeroth, double resolution, + void (*tested_f)(void *), int (*is_over_thresh)(void *)) +{ + int count; + + count = 0; + while (!is_over_thresh(zeroth) && count < 100 * resolution) + { + tested_f(zeroth); + ++count; + } + if (count == 100 * resolution) + return (-1); + return (fmod(count / 100., 1.)); +} + +double mandelbrot(t_set_man *settings, double x, double y) +{ + int count; + t_complex c; + t_complex z; + + c.r = x; + c.i = y; + count = 0; + z.r = 0; + z.i = 0; + if (complex_norm(c) > 4) + return (0); + while (complex_norm(z) < settings->threshold && count < settings->detail) + { + z = complex_add(complex_mul(z, z), c); + ++count; + } + if (count == settings->detail) + return (-1); + return (fmod((double)(count + 1 - log(log(complex_norm(z)) / log(2)) / log(2)) / settings->color_stability, 1.)); +} + +double tricorn(t_set_man *settings, double x, double y) +{ + int count; + t_complex c; + t_complex z; + + c.r = x; + c.i = y; + count = 0; + z.r = 0; + z.i = 0; + if (complex_norm(c) > 4) + return (0); + while (complex_norm(z) < settings->threshold && count < settings->detail) + { + z = complex_add(complex_mul(complex_conj(z), complex_conj(z)), c); + ++count; + } + if (count == settings->detail) + return (-1); + return (fmod((double)(count + 1 - log(log(complex_norm(z)) / log(2)) / log(2)) / settings->color_stability, 1.)); +} diff --git a/julia.c b/fractals.h similarity index 59% rename from julia.c rename to fractals.h index aa63a9a..66a54cb 100644 --- a/julia.c +++ b/fractals.h @@ -1,31 +1,23 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* julia.h :+: :+: :+: */ +/* fractals.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/12/05 19:57:50 by ljiriste #+# #+# */ -/* Updated: 2023/12/05 20:16:54 by ljiriste ### ########.fr */ +/* Created: 2024/01/19 10:56:55 by ljiriste #+# #+# */ +/* Updated: 2024/01/19 11:01:53 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ -#include -#include "vect2.h" -#include "complex.h" +#ifndef FRACTALS_H +# define FRACTALS_H + +# include "fractol.h" double general_julia(void *zeroth, double resolution, - void (*tested_f)(void *), int (*is_over_thresh)(void *)) -{ - int count; + void (*tested_f)(void *), int (*is_over_thresh)(void *)); +double mandelbrot(t_set_man *settings, double x, double y); +double tricorn(t_set_man *settings, double x, double y); - count = 0; - while (!is_over_thresh(zeroth) && count < 100 * resolution) - { - tested_f(zeroth); - ++count; - } - if (count == 100 * resolution) - return (-1); - return (fmod(count / 100., 1.)); -} +#endif //FRACTALS_H diff --git a/main.c b/main.c index d65be5f..f66669c 100644 --- a/main.c +++ b/main.c @@ -6,13 +6,14 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/10/27 14:29:26 by ljiriste #+# #+# */ -/* Updated: 2024/01/18 17:14:33 by ljiriste ### ########.fr */ +/* Updated: 2024/01/19 11:21:04 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include "complex.h" #include "color.h" #include "vect2.h" +#include "fractals.h" #include "fractol.h" #include "libft.h" #include @@ -64,29 +65,6 @@ void free_session(t_session *s) return ; } -double mandelbrot(t_set_man *settings, double x, double y) -{ - int count; - t_complex c; - t_complex z; - - c.r = x; - c.i = y; - count = 0; - z.r = 0; - z.i = 0; - if (complex_norm(c) > 4) - return (0); - while (complex_norm(z) < settings->threshold && count < settings->detail) - { - z = complex_add(complex_mul(z, z), c); - ++count; - } - if (count == settings->detail) - return (-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) { char *px_addr; -- 2.30.2