diff options
| -rw-r--r-- | Makefile | 7 | ||||
| -rw-r--r-- | include/fractol.h | 16 | ||||
| -rw-r--r-- | src/event.c | 38 | ||||
| -rw-r--r-- | src/fractals/julia.c | 41 | ||||
| -rw-r--r-- | src/fractals/mandelbrot.c | 7 | ||||
| -rw-r--r-- | src/helper.c | 22 | ||||
| -rw-r--r-- | src/main.c | 7 | ||||
| -rw-r--r-- | src/render.c | 37 | ||||
| -rw-r--r-- | src/state.c | 43 | ||||
| -rw-r--r-- | subject.pdf | bin | 0 -> 2439865 bytes |
10 files changed, 142 insertions, 76 deletions
@@ -6,7 +6,7 @@ # By: cacharle <marvin@42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2020/02/24 09:19:49 by cacharle #+# #+# # -# Updated: 2020/02/24 12:05:19 by cacharle ### ########.fr # +# Updated: 2020/02/24 15:22:40 by cacharle ### ########.fr # # # # **************************************************************************** # @@ -35,7 +35,8 @@ SRC_FILES = main.c \ event.c \ state.c \ helper.c \ - fractals/mandelbrot.c + fractals/mandelbrot.c \ + fractals/julia.c SRC = $(addprefix $(SRC_DIR)/,$(SRC_FILES)) OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) @@ -51,7 +52,7 @@ prebuild: $(NAME): $(OBJ) $(CC) -o $@ $^ $(LDFLAGS) -$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(CC) $(CCFLAGS) -c -o $@ $< clean: libft_clean minilibx_clean diff --git a/include/fractol.h b/include/fractol.h index c41231b..e315e6b 100644 --- a/include/fractol.h +++ b/include/fractol.h @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:28:16 by cacharle #+# #+# */ -/* Updated: 2020/02/24 13:59:01 by cacharle ### ########.fr */ +/* Updated: 2020/02/24 15:29:50 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,19 +71,22 @@ typedef struct double i; } t_complex; -typedef int (*t_func_fractal)(t_complex z); +struct s_state; -typedef struct +typedef int (*t_func_fractal)(struct s_state *state, t_complex z); + +typedef struct s_state { bool running; + bool updated; void *mlx_ptr; void *window_ptr; t_image window; - t_complex window_complex[WINDOW_HEIGHT * WINDOW_WIDTH]; t_color palette[PALETTE_SIZE]; t_func_fractal func; t_complex center; t_complex plane; + t_complex julia_const; } t_state; /* @@ -98,7 +101,6 @@ int state_destroy(t_state *state); */ int render_update(t_state *state); -void render_update_window_complex(t_state *state); /* ** event.c @@ -107,12 +109,14 @@ void render_update_window_complex(t_state *state); int event_quit(t_state *state); int event_keydown(int key, t_state *state); int event_mouse(int button, int x, int y, t_state *state); +int event_mouse_motion(int x, int y, t_state *state); /* ** fractals/ */ -int mandelbrot(t_complex z); +int mandelbrot(t_state *state, t_complex z); +int julia(t_state *state, t_complex z); /* ** helper.c diff --git a/src/event.c b/src/event.c index 3f878f8..30b5c34 100644 --- a/src/event.c +++ b/src/event.c @@ -6,14 +6,14 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:52:35 by cacharle #+# #+# */ -/* Updated: 2020/02/24 14:08:20 by cacharle ### ########.fr */ +/* Updated: 2020/02/24 15:38:06 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "fractol.h" -#define MOVE_SPEED 0.2 -#define ZOOM_SPEED 1.1 +#define MOVE_SPEED 0.1 +#define ZOOM_SPEED 1.2 int event_quit(t_state *state) { @@ -23,7 +23,7 @@ int event_quit(t_state *state) int event_keydown(int key, t_state *state) { - printf("%d\n", key); + /* printf("%d\n", key); */ if (key == MLXK_ESC) state->running = false; else if (key == MLXK_UP) @@ -36,7 +36,7 @@ int event_keydown(int key, t_state *state) state->center.r += state->plane.r * MOVE_SPEED; else return (0); - render_update_window_complex(state); + state->updated = false; return (0); } @@ -45,21 +45,41 @@ int event_mouse(int button, int x, int y, t_state *state) (void)x; (void)y; - /* printf("%d\n", button); */ - /* printf("%d\n", x); */ - /* printf("%d\n", y); */ if (button == MLX_MOUSE_SCROLL_UP) { state->plane.r /= ZOOM_SPEED; state->plane.i /= ZOOM_SPEED; + /* state->center.r += MOVE_SPEED * (double)(x - WINDOW_WIDTH / 2) / WINDOW_WIDTH; */ + /* state->center.i += MOVE_SPEED * (double)(y - WINDOW_HEIGHT / 2) / WINDOW_HEIGHT; */ + } else if (button == MLX_MOUSE_SCROLL_DOWN) { state->plane.r *= ZOOM_SPEED; state->plane.i *= ZOOM_SPEED; + /* state->center.r -= MOVE_SPEED * (double)(x - WINDOW_WIDTH / 2) / WINDOW_WIDTH; */ + /* state->center.i -= MOVE_SPEED * (double)(y - WINDOW_HEIGHT / 2) / WINDOW_HEIGHT; */ } else return (0); - render_update_window_complex(state); + state->updated = false; + return (0); +} + +int event_mouse_motion(int x, int y, t_state *state) +{ + if (state->julia_const.r == NAN) + return (0); + if (x < 0) + x = 0; + if (x > WINDOW_WIDTH - 1) + x = WINDOW_WIDTH - 1; + if (y < 0) + y = 0; + if (y > WINDOW_HEIGHT - 1) + y = WINDOW_HEIGHT - 1; + state->julia_const.r = ((double)x / (double)WINDOW_WIDTH) * 4.0 - 2.0; + state->julia_const.i = ((double)y / (double)WINDOW_HEIGHT) * 4.0 - 2.0; + state->updated = false; return (0); } diff --git a/src/fractals/julia.c b/src/fractals/julia.c index e69de29..d1afb46 100644 --- a/src/fractals/julia.c +++ b/src/fractals/julia.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* julia.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/24 15:17:38 by cacharle #+# #+# */ +/* Updated: 2020/02/24 15:36:04 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fractol.h" + +#define JULIA_MAX_ITERATION 20 +#define JULIA_ESCAPE_RADIUS_SQUARED 100 + +int julia(t_state *state, t_complex z) +{ + int n; + double zr; + double zi; + double zr_square; + double zi_square; + + zr = z.r; + zi = z.i; + n = -1; + while (++n < JULIA_MAX_ITERATION) + { + zi_square = zi * zi; + zr_square = zr * zr; + if (zr_square + zi_square > JULIA_ESCAPE_RADIUS_SQUARED) + break; + zi = 2.0 * zr * zi; + zr = zr_square - zi_square; + zi += state->julia_const.i; + zr += state->julia_const.r; + } + return (n); +} diff --git a/src/fractals/mandelbrot.c b/src/fractals/mandelbrot.c index 4211cfa..5283fb5 100644 --- a/src/fractals/mandelbrot.c +++ b/src/fractals/mandelbrot.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 11:07:41 by cacharle #+# #+# */ -/* Updated: 2020/02/24 13:46:22 by cacharle ### ########.fr */ +/* Updated: 2020/02/24 15:25:17 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ #define MANDEL_MAX_ITERATION 20 #define MANDEL_ESCAPE_RADIUS_SQUARED 100 -int mandelbrot(t_complex z) +int mandelbrot(t_state *state, t_complex z) { int n; double zr; @@ -23,6 +23,7 @@ int mandelbrot(t_complex z) double zr_square; double zi_square; + (void)state; zr = z.r; zi = z.i; n = -1; @@ -31,7 +32,7 @@ int mandelbrot(t_complex z) zi_square = zi * zi; zr_square = zr * zr; if (zr_square + zi_square > MANDEL_ESCAPE_RADIUS_SQUARED) - return (n); + break; zi = 2.0 * zr * zi; zr = zr_square - zi_square; zi += z.i; diff --git a/src/helper.c b/src/helper.c index f3b040b..8f03bed 100644 --- a/src/helper.c +++ b/src/helper.c @@ -6,19 +6,19 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 11:54:57 by cacharle #+# #+# */ -/* Updated: 2020/02/24 12:17:08 by cacharle ### ########.fr */ +/* Updated: 2020/02/24 14:14:43 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "fractol.h" -void h_offset_to_complex(t_state *state, t_complex *z, int offset) -{ - int y; // unoptimized - int x; - - y = offset / state->window.width; - x = offset % state->window.width; - z->a = y / state->window.height * state->plane.a; - z->b = x / state->window.width * state->plane.b; -} +/* void h_offset_to_complex(t_state *state, t_complex *z, int offset) */ +/* { */ +/* int y; // unoptimized */ +/* int x; */ +/* */ +/* y = offset / state->window.width; */ +/* x = offset % state->window.width; */ +/* z->a = y / state->window.height * state->plane.a; */ +/* z->b = x / state->window.width * state->plane.b; */ +/* } */ @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:27:44 by cacharle #+# #+# */ -/* Updated: 2020/02/24 13:55:02 by cacharle ### ########.fr */ +/* Updated: 2020/02/24 15:28:40 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,15 +16,14 @@ int main(int argc, char **argv) { t_state state; - if (argc != 2) + if (argc != 2 || state_init(&state, argv[1]) < 0) { ft_putstr("mandelbrot\njulia\n"); return (0); } - if (state_init(&state, argv[1]) < 0) - return (1); mlx_hook(state.window_ptr, 17, 0, event_quit, (void*)&state); mlx_hook(state.window_ptr, 2, 2, event_keydown, (void*)&state); + mlx_hook(state.window_ptr, 6, 64, event_mouse_motion, (void*)&state); mlx_mouse_hook(state.window_ptr, event_mouse, (void*)&state); mlx_loop_hook(state.mlx_ptr, render_update, (void*)&state); mlx_loop(state.mlx_ptr); diff --git a/src/render.c b/src/render.c index f4f3fed..1b93c3a 100644 --- a/src/render.c +++ b/src/render.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:54:59 by cacharle #+# #+# */ -/* Updated: 2020/02/24 14:01:43 by cacharle ### ########.fr */ +/* Updated: 2020/02/24 15:20:39 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,17 +18,19 @@ static void st_render_fractal(t_state *state) int i; int j; t_color color; + t_complex z; color.hexcode = 0xffffff; offset = 0; i = -1; - while (++i < state->window.height) + while (++i < WINDOW_HEIGHT) { j = -1; - while (++j < state->window.width) + while (++j < WINDOW_WIDTH) { - ((t_color*)state->window.data)[offset] = - state->palette[state->func(state->window_complex[offset])]; + z.r = ((double)j / (double)WINDOW_WIDTH) * state->plane.r - (state->plane.r / 2.0) + state->center.r; + z.i = ((double)i / (double)WINDOW_HEIGHT) * state->plane.i - (state->plane.i / 2.0) + state->center.i; + ((t_color*)state->window.data)[offset] = state->palette[state->func(state, z)]; offset++; } } @@ -41,29 +43,10 @@ int render_update(t_state *state) state_destroy(state); exit(EXIT_SUCCESS); } + if (state->updated) + return (0); st_render_fractal(state); mlx_put_image_to_window(state->mlx_ptr, state->window_ptr, state->window.id, 0, 0); + state->updated = true; return (0); } - -void render_update_window_complex(t_state *state) -{ - int i; - int j; - int offset; - - i = -1; - offset = 0; - while (++i < WINDOW_HEIGHT) - { - j = -1; - while (++j < WINDOW_WIDTH) - { - state->window_complex[offset].r = - ((double)j / (double)WINDOW_WIDTH) * state->plane.r - (state->plane.r / 2.0) + state->center.r; - state->window_complex[offset].i = - ((double)i / (double)WINDOW_HEIGHT) * state->plane.i - (state->plane.i / 2.0) + state->center.i; - offset++; - } - } -} diff --git a/src/state.c b/src/state.c index 33aa98b..40f3d00 100644 --- a/src/state.c +++ b/src/state.c @@ -6,16 +6,16 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/24 09:58:01 by cacharle #+# #+# */ -/* Updated: 2020/02/24 12:34:50 by cacharle ### ########.fr */ +/* Updated: 2020/02/24 15:26:58 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "fractol.h" -/* #define PALETTE_START 0x000022 */ -/* #define PALETTE_END 0xd62f2f */ -#define PALETTE_START 0x000000 -#define PALETTE_END 0xffffff +#define PALETTE_START 0x000022 +#define PALETTE_END 0xd62f2f +/* #define PALETTE_START 0x000000 */ +/* #define PALETTE_END 0xffffff */ static void st_state_init_palette(t_state *state) { @@ -39,10 +39,28 @@ static void st_state_init_palette(t_state *state) } } -int state_init(t_state *state, char *fractal_name) +static int st_state_dispatch_func(t_state *state, char *fractal_name) { - (void)fractal_name; + if (ft_strcmp(fractal_name, "mandelbrot") == 0) + state->func = &mandelbrot; + else if (ft_strcmp(fractal_name, "julia") == 0) + { + state->func = &julia; + state->julia_const.r = 0.0; + state->julia_const.i = 0.0; + return (0); + } + else + return (-1); + state->julia_const.r = NAN; + state->julia_const.i = NAN; + return (0); +} +int state_init(t_state *state, char *fractal_name) +{ + if (st_state_dispatch_func(state, fractal_name) < 0) + return (-1); if ((state->mlx_ptr = mlx_init()) == NULL) return (-1); if ((state->window_ptr = mlx_new_window(state->mlx_ptr, 640, 480, WINDOW_TITLE)) == NULL) @@ -54,13 +72,12 @@ int state_init(t_state *state, char *fractal_name) state->window.data = mlx_get_data_addr(state->window.id, &state->window.depth, &state->window.size_line, &state->window.endian); state->running = true; - state->func = &mandelbrot; - state->center.a = 0.0; - state->center.b = 0.0; - state->plane.a = 4.0; - state->plane.b = 4.0; - render_update_window_complex(state); + state->center.r = 0.0; + state->center.i = 0.0; + state->plane.r = 4.0; + state->plane.i = 4.0; st_state_init_palette(state); + state->updated = false; return (0); } diff --git a/subject.pdf b/subject.pdf Binary files differnew file mode 100644 index 0000000..f263d32 --- /dev/null +++ b/subject.pdf |
