Add tricorn fractal, move fractals to separate file
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 19 Jan 2024 11:02:41 +0000 (12:02 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 19 Jan 2024 11:02:41 +0000 (12:02 +0100)
Makefile
complex.c
complex.h
fractals.c [new file with mode: 0644]
fractals.h [moved from julia.c with 59% similarity]
main.c

index 4b79f1dcd555f481b5e2c2472623a88cef699995..ebb90c0bc232b67121a6b41777ccdd8b60cf7301 100644 (file)
--- 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)
index f8780fdd49bfa63c9da25d4ece2a5c56dc7064d2..ca2acfc7362e356d7bfc9e7d8f95627b5bb7518d 100644 (file)
--- a/complex.c
+++ b/complex.c
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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));
index 3abdf570f425eb046dea1f44401deea6c338316e..c3bfa00ee5e3130176bdcace8f79028f1b8ea450 100644 (file)
--- a/complex.h
+++ b/complex.h
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 (file)
index 0000000..790fa35
--- /dev/null
@@ -0,0 +1,78 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   fractals.c                                         :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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 <math.h>
+
+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.));
+}
similarity index 59%
rename from julia.c
rename to fractals.h
index aa63a9aa5dd64be685a6402510f4ce7c683b1ffc..66a54cbe5159872f2fef4dfa00bcf9a9530bf1cb 100644 (file)
--- a/julia.c
@@ -1,31 +1,23 @@
 /* ************************************************************************** */
 /*                                                                            */
 /*                                                        :::      ::::::::   */
-/*   julia.h                                            :+:      :+:    :+:   */
+/*   fractals.h                                         :+:      :+:    :+:   */
 /*                                                    +:+ +:+         +:+     */
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
-/*   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 <math.h>
-#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 d65be5fd21effca1ed0e2fe61ac5e9be28b4f82f..f66669cf69e80c3a89cda68331ad2d2aa4478633 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,13 +6,14 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 <mlx.h>
@@ -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;