Split event handling from main. Add color smoothing.
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 5 Dec 2023 18:52:17 +0000 (19:52 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 5 Dec 2023 18:52:17 +0000 (19:52 +0100)
Move event handling (and no_event_handle) functions to
event_handling.c.
Add smoothing to mandelbrot output.

Makefile
event_handling.c [new file with mode: 0644]
fractol.h
main.c

index 0601b7c1305d71160b3bdbfd6cac3d49cd73c684..4cfd9bac32c796d69be39e7bd408779cfc8f51ad 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
+SRCS := main.c complex.c color.c event_handling.c
 OBJS := $(SRCS:%.c=%.o)
 
 all : $(NAME)
diff --git a/event_handling.c b/event_handling.c
new file mode 100644 (file)
index 0000000..b4609d6
--- /dev/null
@@ -0,0 +1,57 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   hooked.c                                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2023/12/05 19:35:01 by ljiriste          #+#    #+#             */
+/*   Updated: 2023/12/05 19:46:30 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <X11/X.h>
+#include <X11/keysym.h>
+#include <mlx.h>
+#include "fractol.h"
+#include "vect2.h"
+
+int    handle_key_press(int keycode, t_session *s)
+{
+       if (keycode == XK_Escape)
+               close_win(s);
+       return (0);
+}
+
+int    handle_mouse_press(int button, int x, int y, t_session *s)
+{
+       double  delta_zoom;
+
+       if (button == Button4)
+               delta_zoom = 0.5;
+       else if (button == Button5)
+               delta_zoom = 2;
+       if (button == Button4 || button == Button5)
+       {
+               change_zoom(&s->view, (t_vect2){.x = x, .y = y}, delta_zoom);
+               draw_fractal(s);
+       }
+       return (0);
+}
+
+/*     valgrind indicates an issue when the draw_fractal is in proccess
+ *     while and ESC is pressed thus ending the program.
+ *
+ *     Setting the condition to less than 1 basically turns this redrawing
+ *     off.
+ */
+int    no_event_handle(t_session *s)
+{
+       if (s->view.resolution < 1)
+       {
+               s->view.draw_whole = 0;
+               s->view.resolution *= 2;
+               draw_fractal(s);
+       }
+       return (0);
+}
index c1ddea72f760f4bca39f4da22fecefc39a6b8b1a..ae0e8774efec8ecbf22b72605a171ce3fdc4c237 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: 2023/11/11 18:51:32 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/12/05 19:47:36 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -49,5 +49,11 @@ struct s_session
 };
 typedef struct s_session       t_session;
 
+int    handle_key_press(int keycode, t_session *s);
+int    handle_mouse_press(int button, int x, int y, t_session *s);
+int    no_event_handle(t_session *s);
+int    draw_fractal(t_session *s);
+void   change_zoom(t_view *view, t_vect2 invariant, double d_zoom);
+int    close_win(t_session *s);
 
 #endif
diff --git a/main.c b/main.c
index 07a295ce7e8fbd8612c2ca8f8a9d566c78865943..ed5126956d67eb0bcbce85a220811e9e662cab77 100644 (file)
--- a/main.c
+++ b/main.c
@@ -6,16 +6,16 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/10/27 14:29:26 by ljiriste          #+#    #+#             */
-/*   Updated: 2023/11/11 18:50:45 by ljiriste         ###   ########.fr       */
+/*   Updated: 2023/12/05 19:51:07 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include <X11/X.h>
 #include <X11/keysym.h>
+#include <mlx.h>
 #include <limits.h>
 #include <stdlib.h>
 #include <math.h>
-#include <mlx.h>
 #include "libft.h"
 #include "fractol.h"
 #include "complex.h"
@@ -68,33 +68,27 @@ void        free_session(t_session *s)
 
 double mandelbrot(double x, double y, double resolution)
 {
-       double                  threshold;
-       const double    min_threshold = 2.5;
-       double                  max_count;
+       const double    threshold = 1000;
+       const double    log2 = log(2);
        int                             count;
        t_complex               c;
        t_complex               z;
 
        c.r = x;
        c.i = y;
-       max_count = 100 * resolution;
-       threshold = 3;
-       if (threshold < min_threshold)
-               threshold = min_threshold;
        count = 0;
        z.r = 0;
        z.i = 0;
-       while (complex_norm(z) < threshold && count < max_count)
+       while (complex_norm(z) < threshold && count < 100 * resolution)
        {
                z = complex_add(complex_mul(z, z), c);
                ++count;
        }
-       if (count == max_count)
+       if (count == 100 * resolution)
                return (-1);
-       return (fmod(count / 100., 1.));
+       return (fmod((count + 1 - log(log(complex_norm(z)) / log2) / log2) / 100., 1.));
 }
 
-
 int    draw_fractal(t_session *s)
 {
        int             x;
@@ -131,51 +125,11 @@ void      change_zoom(t_view *view, t_vect2 invariant, double d_zoom)
        view->pixel_size.y /= d_zoom;
        view->window_coord.x += view->pixel_size.x * invariant.x * (d_zoom - 1);
        view->window_coord.y -= view->pixel_size.y * invariant.y * (d_zoom - 1);
-       view->resolution = 1;
+       //view->resolution = 1;
        view->draw_whole = 1;
        return ;
 }
 
-int    handle_key_press(int keycode, t_session *s)
-{
-       if (keycode == XK_Escape)
-               close_win(s);
-       return (0);
-}
-
-int    handle_mouse_press(int button, int x, int y, t_session *s)
-{
-       double  delta_zoom;
-
-       if (button == Button4)
-               delta_zoom = 0.5;
-       else if (button == Button5)
-               delta_zoom = 2;
-       if (button == Button4 || button == Button5)
-       {
-               change_zoom(&s->view, (t_vect2){.x = x, .y = y}, delta_zoom);
-               draw_fractal(s);
-       }
-       return (0);
-}
-
-/*     valgrind indicates an issue when the draw_fractal is in proccess
- *     while and ESC is pressed thus ending the program.
- *
- *     Setting the condition to less than 1 basically turns this redrawing
- *     off.
- */
-int    no_event_handle(t_session *s)
-{
-       if (s->view.resolution < 1)
-       {
-               s->view.draw_whole = 0;
-               s->view.resolution *= 2;
-               draw_fractal(s);
-       }
-       return (0);
-}
-
 void   init_view(t_session *s)
 {
        s->view.fractal = mandelbrot;
@@ -184,7 +138,7 @@ void        init_view(t_session *s)
        s->view.pixel_size.y = 0.01;
        s->view.window_coord.x = -s->img.width / 2 * s->view.pixel_size.x;
        s->view.window_coord.y = s->img.height / 2 * s->view.pixel_size.y;
-       s->view.resolution = 1;
+       s->view.resolution = 10;
        s->view.draw_whole = 1;
        return ;
 }
@@ -193,8 +147,8 @@ void        parse_args(int argc, char **argv, t_session *s)
 {
        if (argc == 0)
                free(argv);
-       s->img.width = 200;
-       s->img.height = 200;
+       s->img.width = 500;
+       s->img.height = 500;
        init_view(s);
        return ;
 }