diff --git a/CMakeLists.txt b/CMakeLists.txt index c36a3b2..bf22b05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,4 +21,5 @@ target_link_libraries(fractol -lft -lmlx "-framework OpenGL" - "-framework AppKit") # linkage + "-framework AppKit" + -lpthread) # linkage diff --git a/inc/fractol.h b/inc/fractol.h index 50061a2..58bd6ed 100644 --- a/inc/fractol.h +++ b/inc/fractol.h @@ -17,10 +17,11 @@ # include "mlx.h" # include # include +#include #include # define HEIGHT 1200 -# define WIDTH 2500 +# define WIDTH 1200 # define COLOR1 0 # define COLOR2 16777215 # define INIT_X 30 @@ -39,7 +40,9 @@ # define VIOLET 8323327 # define BLACK 0 -# define JUL_MAX_ITER 100 +# define JUL_MAX_ITER 300 + +# define NUM_THREADS 4 typedef struct s_complex { @@ -67,12 +70,20 @@ typedef struct s_move double z; } t_move; +typedef struct s_pixel +{ + int x; + int y; +} t_pixel; + typedef struct s_julia { t_complex jul_const; t_color color; - int max_iterations; t_move mov; + int x; + int y; + int max_itr; } t_julia; typedef struct s_fractals @@ -93,7 +104,6 @@ typedef struct s_fractol int endian; } t_fractol; - int tab_length(char **tab); void free_tab(char **tab); void quit(t_fractol *fractol); diff --git a/src/color.c b/src/color.c index f2770eb..8051c5a 100644 --- a/src/color.c +++ b/src/color.c @@ -52,49 +52,49 @@ t_color int_to_color(int c) new.red = (c >> 16) & 0x0000FF; return (new); } +// +//int rainbow(int step, int max_step) +//{ +// if (step >= 0 && step < max_step / 8) +// return (RED); +// if (step >= max_step / 8 && step <= max_step / 4) +// return (ORANGE); +// if (step >= max_step / 4 && step <= max_step / 8 * 3) +// return (YELLOW); +// if (step >= max_step / 8 * 3 && step <= max_step / 2) +// return (BLUE); +// if (step >= max_step / 2 && step <= max_step / 8 * 5) +// return (VIOLET); +// if (step >= max_step / 8 * 5 && step <= max_step / 8 * 6) +// return (BLACK); +// return (0); +//} int rainbow(int step, int max_step) { if (step >= 0 && step < max_step / 8) - return (RED); - if (step >= max_step / 8 && step <= max_step / 4) - return (ORANGE); - if (step >= max_step / 4 && step <= max_step / 8 * 3) - return (YELLOW); - if (step >= max_step / 8 * 3 && step <= max_step / 2) - return (BLUE); - if (step >= max_step / 2 && step <= max_step / 8 * 5) - return (VIOLET); + return (color_to_int( + color_lerp(int_to_color(RED), int_to_color(ORANGE), + (float)step / (float)max_step))); + if (step >= max_step / 8 && step <= max_step / 8 * 2) + return (color_to_int( + color_lerp(int_to_color(ORANGE), int_to_color(YELLOW), + step / max_step / 8 * 2))); + if (step >= max_step / 8 * 2 && step <= max_step / 8 * 3) + return (color_to_int( + color_lerp(int_to_color(YELLOW), int_to_color(GREEN), + step / max_step / 8 * 3))); + if (step >= max_step / 8 * 3 && step <= max_step / 8 * 4) + return (color_to_int( + color_lerp(int_to_color(GREEN), int_to_color(BLUE), + step / max_step / 8 * 4))); + if (step >= max_step / 8 * 4 && step <= max_step / 8 * 5) + return (color_to_int( + color_lerp(int_to_color(BLUE), int_to_color(VIOLET), + step / max_step / 8 * 5))); if (step >= max_step / 8 * 5 && step <= max_step / 8 * 6) - return (BLACK); + return (color_to_int( + color_lerp(int_to_color(VIOLET), int_to_color(BLACK), + step / max_step / 8 * 6))); return (0); } - -//int rainbow(int step, int max_step) -//{ -// if (step >= 0 && step < max_step / 8) -// return (color_to_int( -// color_lerp(int_to_color(RED), int_to_color(ORANGE), -// step / max_step / 8))); -// if (step >= max_step / 8 && step <= max_step / 8 * 2) -// return (color_to_int( -// color_lerp(int_to_color(ORANGE), int_to_color(YELLOW), -// step / max_step / 8 * 2))); -// if (step >= max_step / 8 * 2 && step <= max_step / 8 * 3) -// return (color_to_int( -// color_lerp(int_to_color(YELLOW), int_to_color(GREEN), -// step / max_step / 8 * 3))); -// if (step >= max_step / 8 * 3 && step <= max_step / 8 * 4) -// return (color_to_int( -// color_lerp(int_to_color(GREEN), int_to_color(BLUE), -// step / max_step / 8 * 4))); -// if (step >= max_step / 8 * 4 && step <= max_step / 8 * 5) -// return (color_to_int( -// color_lerp(int_to_color(BLUE), int_to_color(VIOLET), -// step / max_step / 8 * 5))); -// if (step >= max_step / 8 * 5 && step <= max_step / 8 * 6) -// return (color_to_int( -// color_lerp(int_to_color(VIOLET), int_to_color(BLACK), -// step / max_step / 8 * 6))); -// return (0); -//} diff --git a/src/fractol_init.c b/src/fractol_init.c index a62bfe1..14d6900 100644 --- a/src/fractol_init.c +++ b/src/fractol_init.c @@ -30,7 +30,7 @@ t_julia *jul_init(void) j = malloc(sizeof(t_julia)); complex_equal(-0.7, 0.27015, &j->jul_const); - j->max_iterations = JUL_MAX_ITER; + j->max_itr = JUL_MAX_ITER; j->mov.x = 0; j->mov.y = 0; j->mov.z = 1; diff --git a/src/julia.c b/src/julia.c index c20ab8b..8bfa36d 100644 --- a/src/julia.c +++ b/src/julia.c @@ -1,44 +1,84 @@ #include "fractol.h" -static void julia_computation(int x, int y, t_julia *ju) +int complex_parallel(t_complex new, t_complex com_const) { - t_complex new; + int i; t_complex old; - int i; - new.rl = 1.5 * (x - WIDTH / 2) / (0.5 * ju->mov.z * WIDTH) + ju->mov.x; - new.im = (y - HEIGHT / 2) / (0.5 * ju->mov.z * HEIGHT) + ju->mov.y; i = 0; - while (i < ju->max_iterations) + while (i < JUL_MAX_ITER) { old = new; - new.rl = old.rl * old.rl - old.im * old.im + ju->jul_const.rl; - new.im = 2 * old.rl * old.im + ju->jul_const.im; + new.rl = old.rl * old.rl - old.im * old.im + com_const.rl; + new.im = 2 * old.rl * old.im + com_const.im; if((new.rl * new.rl + new.im * new.im) > 4) break ; i++; } - ju->color = int_to_color(rainbow(i, ju->max_iterations)); + return (i); } +void *julia_computation(void *fr) +{ + t_complex new; + t_julia *ju; + int i; + int x; + int y; + + ju = ((t_fractol *)fr)->fractals->jul; + x = ju->x; + y = ju->y; + new.rl = 1.5 * (x - WIDTH / 2) / (0.5 * ju->mov.z * WIDTH) + ju->mov.x; + new.im = (y - HEIGHT / 2) / (0.5 * ju->mov.z * HEIGHT) + ju->mov.y; + i = complex_parallel(new, ju->jul_const); + ju->color = int_to_color(rainbow(i, ju->max_itr)); + put_pixel_to_image(x, y, &((t_fractol *)fr)->fractals->jul->color, fr); + return (0); +} + +//void julia_parralel(t_fractol *fr) +//{ +// pthread_t threads[NUM_THREADS]; +// t_fractol *fr_tmp; +// int rc; +// int i; +// +// i = 0; +// while (i < NUM_THREADS && fr->fractals->jul->y < fr->w_width) +// { +// fr_tmp = malloc(sizeof(t_fractol)); +// *fr_tmp = *fr; +//// printf("In main: creating thread %d\n", i); +// rc = pthread_create(&threads[i], NULL, julia_computation, (void *)fr_tmp); +// if (rc){ +// printf("ERROR; return code from pthread_create() is %d\n", rc); +// exit(-1); +// } +// free(fr_tmp); +// fr->fractals->jul->y++; +// i++; +// } +//} + void draw_julia(t_fractol *fr) { - int x; - int y; - + int x; + int y; new_and_clear_image(fr); - x = 0; - while (x < fr->w_width) + y = 0; + while (y < fr->w_height) { - y = 0; - while(y < fr->w_height) + x = 0; + while(x < fr->w_width) { - julia_computation(x, y, fr->fractals->jul); - put_pixel_to_image(x, y, &fr->fractals->jul->color, fr); - y++; + fr->fractals->jul->x = x; + fr->fractals->jul->y = y; + julia_computation(fr); + x++; } - x++; + y++; } mlx_put_image_to_window(fr->mw->mlx, fr->mw->win, fr->image, 0, 0); } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 5670bea..35f1a32 100644 --- a/src/main.c +++ b/src/main.c @@ -11,7 +11,8 @@ /* ************************************************************************** */ #include "fractol.h" -#include + + int main(void) { @@ -25,5 +26,5 @@ int main(void) mlx_hook(fr->mw->win, 4, 0, mouse_button_hook, fr); mlx_hook(fr->mw->win, 6, 0, mouse_move_hook, fr); mlx_loop(fr->mw->mlx); - return (0); + pthread_exit(NULL); }