diff --git a/inc/fractol.h b/inc/fractol.h index 0214459..9ef9bab 100644 --- a/inc/fractol.h +++ b/inc/fractol.h @@ -20,7 +20,6 @@ # include # include # include -# include # define HEIGHT 700 # define WIDTH 1300 @@ -115,7 +114,6 @@ void parse_input(int ac, char **av); t_color gradient(float value, t_gradient gradient); t_gradient default_gradient(void); t_gradient random_gradient(void); -int color_to_int(t_color c); t_fractol *fractol_init(void *mlx, int frac_type); diff --git a/src/burning_ship.c b/src/burning_ship.c index 8ca265b..26c3331 100644 --- a/src/burning_ship.c +++ b/src/burning_ship.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* burning_ship.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/24 19:36:06 by gtertysh #+# #+# */ +/* Updated: 2017/03/24 19:36:26 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "fractol.h" int complex_ship(t_complex new, t_complex com_const, t_frac_data *data) @@ -11,7 +23,7 @@ int complex_ship(t_complex new, t_complex com_const, t_frac_data *data) old = new; new.rl = fabsl(old.rl * old.rl - old.im * old.im + com_const.rl); new.im = fabsl(2 * old.rl * old.im + com_const.im); - if((new.rl * new.rl + new.im * new.im) > 4) + if ((new.rl * new.rl + new.im * new.im) > 4) break ; i++; } @@ -21,7 +33,7 @@ int complex_ship(t_complex new, t_complex com_const, t_frac_data *data) t_color ship(t_frac_data *sh, int x, int y) { t_complex new; - int i; + int i; new.rl = 1.5 * (x - WIDTH / 2) / (0.5 * sh->mov.z * WIDTH) + sh->mov.x - 0.5; diff --git a/src/color.c b/src/color.c index 6bc0440..740afbc 100644 --- a/src/color.c +++ b/src/color.c @@ -12,9 +12,9 @@ #include "fractol.h" -t_color int_to_color(int c) +t_color int_to_color(int c) { - t_color new; + t_color new; new.blu = c & 0x0000FF; new.grn = (c >> 8) & 0x0000FF; @@ -22,14 +22,9 @@ t_color int_to_color(int c) return (new); } -int color_to_int(t_color c) +t_gradient default_gradient(void) { - return ((c.red << 16) | (c.grn << 8) | c.blu); -} - -t_gradient default_gradient(void) -{ - t_gradient grad; + t_gradient grad; grad.grd[0] = int_to_color(BLACK); grad.grd[1] = int_to_color(BLUE); @@ -42,10 +37,10 @@ t_gradient default_gradient(void) return (grad); } -t_gradient random_gradient(void) +t_gradient random_gradient(void) { t_gradient grad; - int i; + int i; t_color c; i = 0; @@ -59,28 +54,39 @@ t_gradient random_gradient(void) return (grad); } -t_color gradient(float value, t_gradient grad) +t_color pick_color(t_color c1, t_color c2, float frac) +{ + t_color c; + + c.red = (int)((c2.red - c1.red) * frac + c1.red); + c.blu = (int)((c2.blu - c1.blu) * frac + c1.blu); + c.grn = (int)((c2.grn - c1.grn) * frac + c1.grn); + return (c); +} + +t_color gradient(float value, t_gradient grad) { int id1; int id2; float frac; - t_color c; frac = 0; - if(value <= 0) - id1 = id2 = 0; - else if(value >= 1) - id1 = id2 = NUM_COLORS - 1; + if (value <= 0) + { + id1 = 0; + id2 = 0; + } + else if (value >= 1) + { + id1 = NUM_COLORS - 1; + id2 = NUM_COLORS - 1; + } else { value = value * (NUM_COLORS - 1); - id1 = (int)floor(value); - id2 = id1 + 1; + id1 = (int)floor(value); + id2 = id1 + 1; frac = value - (float)id1; } - c.red = (grad.grd[id2].red - grad.grd[id1].red) * frac + grad.grd[id1].red; - c.blu = (grad.grd[id2].blu - grad.grd[id1].blu) * frac + grad.grd[id1].blu; - c.grn = (grad.grd[id2].grn - grad.grd[id1].grn) * frac + grad.grd[id1].grn; - return (c); + return (pick_color(grad.grd[id1], grad.grd[id2], frac)); } - diff --git a/src/complex.c b/src/complex.c index b02ea80..3896df4 100644 --- a/src/complex.c +++ b/src/complex.c @@ -1,7 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* complex.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/24 19:36:44 by gtertysh #+# #+# */ +/* Updated: 2017/03/24 19:36:47 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "fractol.h" void complex_equal(double real, double imag, t_complex *c) { c->im = imag; c->rl = real; -} \ No newline at end of file +} diff --git a/src/fractal_routine.c b/src/fractal_routine.c index b2435e2..da29187 100644 --- a/src/fractal_routine.c +++ b/src/fractal_routine.c @@ -1,6 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* fractal_routine.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/24 19:37:32 by gtertysh #+# #+# */ +/* Updated: 2017/03/24 19:37:33 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "fractol.h" -void *fractal_routine(void *th_data) +void *fractal_routine(void *th_data) { int x; t_color col; @@ -10,7 +22,7 @@ void *fractal_routine(void *th_data) while (data->y_start < HEIGHT && data->y_start < data->y_end) { x = 0; - while(x < data->fr.w_width) + while (x < data->fr.w_width) { col = data->fractal_func(data->fr.frac, x, data->y_start); put_pixel_to_image(x, data->y_start, col, &data->fr); @@ -22,9 +34,9 @@ void *fractal_routine(void *th_data) pthread_exit(NULL); } -t_frac_data *frac_init(int frac_type) +t_frac_data *frac_init(int frac_type) { - t_frac_data *f; + t_frac_data *f; f = malloc(sizeof(t_frac_data)); f->max_itr = FRAC_MAX_ITR; @@ -42,15 +54,15 @@ t_frac_data *frac_init(int frac_type) return (f); } -void fractal_fork(int frac_type) +void fractal_fork(int frac_type) { - t_fractol *fr; + t_fractol *fr; fr = fractol_init(mlx_init(), frac_type); parallel_fractal(fr); mlx_put_image_to_window(fr->mlx, fr->win, fr->img->ptr, 0, 0); mlx_hook(fr->win, 2, 5, key_hook, fr); - mlx_hook(fr->win, 6, (1L<<6), mouse_move_hook, fr); - mlx_hook(fr->win, 4, (1L<<0), mouse_button_hook, fr); + mlx_hook(fr->win, 6, (1L << 6), mouse_move_hook, fr); + mlx_hook(fr->win, 4, (1L << 0), mouse_button_hook, fr); mlx_loop(fr->mlx); -} \ No newline at end of file +} diff --git a/src/fractol_init.c b/src/fractol_init.c index 58ca081..d8e38ef 100644 --- a/src/fractol_init.c +++ b/src/fractol_init.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* fractol_init.c :+: :+: :+: */ +/* fractol_init.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/16 18:55:39 by gtertysh #+# #+# */ -/* Updated: 2017/03/16 18:56:00 by gtertysh ### ########.fr */ +/* Created: 2017/03/24 19:31:38 by gtertysh #+# #+# */ +/* Updated: 2017/03/24 19:31:42 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/help_func.c b/src/help_func.c index fc8e9bd..54a9e89 100644 --- a/src/help_func.c +++ b/src/help_func.c @@ -27,4 +27,4 @@ void print_help(void) "\n\n" "Fractals:\n" "\033[32mjulia\033[0m \033[34mship \033[36mmandelbrot\033[0m\n"); -} \ No newline at end of file +} diff --git a/src/hooks.c b/src/hooks.c index 6e61d4b..753d15a 100644 --- a/src/hooks.c +++ b/src/hooks.c @@ -14,7 +14,6 @@ int key_hook(int keycode, void *fr) { - printf("%d\n", keycode); if (keycode == 53) quit(fr); if (keycode == 49) @@ -35,18 +34,17 @@ int key_hook(int keycode, void *fr) } /* - * beware!! shitcode on lines 48-49. - */ +** beware!! shitcode on lines 48-49. +*/ -int mouse_move_hook( int x, int y, void *fr) +int mouse_move_hook(int x, int y, void *fr) { - static int start_x = WIDTH / 2; - int to_render; + static int start_x = WIDTH / 2; + int to_render; t_fractol *fr_p; to_render = 0; y = to_render; - printf("x = %d, y = %d\n", x, y); to_render = y; fr_p = (t_fractol *)fr; if (fr_p->frac->com_rl_im_change == 1 && @@ -60,14 +58,12 @@ int mouse_move_hook( int x, int y, void *fr) new_and_clear_image(fr_p); parallel_fractal(fr_p); mlx_put_image_to_window(fr_p->mlx, fr_p->win, fr_p->img->ptr, 0, 0); - } return (0); } -int mouse_button_hook(int btn, int x, int y, void *fr) +int mouse_button_hook(int btn, int x, int y, void *fr) { - printf("x = %d, y = %d, btn = %d\n", x, y, btn); if (btn == 4) zoom_in(fr, x, y); if (btn == 5) diff --git a/src/hooks_funcs.c b/src/hooks_funcs.c index a90bc28..989d607 100644 --- a/src/hooks_funcs.c +++ b/src/hooks_funcs.c @@ -1,6 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hooks_funcs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/24 19:21:45 by gtertysh #+# #+# */ +/* Updated: 2017/03/24 19:21:46 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "fractol.h" -void gradient_hook(t_fractol *fr) +void gradient_hook(t_fractol *fr) { fr->frac->grd = random_gradient(); new_and_clear_image(fr); @@ -8,9 +20,9 @@ void gradient_hook(t_fractol *fr) mlx_put_image_to_window(fr->mlx, fr->win, fr->img->ptr, 0, 0); } -int change_imagianry(t_fractol *fr, int x, int *start_x) +int change_imagianry(t_fractol *fr, int x, int *start_x) { - int to_render; + int to_render; to_render = 0; if (x > *start_x) @@ -34,9 +46,9 @@ int change_imagianry(t_fractol *fr, int x, int *start_x) return (to_render); } -int change_real(t_fractol *fr, int x, int *start_x) +int change_real(t_fractol *fr, int x, int *start_x) { - int to_render; + int to_render; to_render = 0; if (x > *start_x) @@ -58,4 +70,4 @@ int change_real(t_fractol *fr, int x, int *start_x) *start_x = x; } return (to_render); -} \ No newline at end of file +} diff --git a/src/hooks_funcs_1.c b/src/hooks_funcs_1.c index 29dc049..28ef796 100644 --- a/src/hooks_funcs_1.c +++ b/src/hooks_funcs_1.c @@ -1,8 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hooks_funcs_1.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/24 19:29:07 by gtertysh #+# #+# */ +/* Updated: 2017/03/24 19:29:08 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "fractol.h" void move_down(t_fractol *fr) { - printf("z %Lf\n", fr->frac->mov.z); fr->frac->mov.y += 1.f / fr->frac->mov.z * 0.2; new_and_clear_image(fr); parallel_fractal(fr); @@ -11,14 +22,13 @@ void move_down(t_fractol *fr) void move_up(t_fractol *fr) { - printf("z %Lf\n", fr->frac->mov.z); fr->frac->mov.y -= 1.f / fr->frac->mov.z * 0.2; new_and_clear_image(fr); parallel_fractal(fr); mlx_put_image_to_window(fr->mlx, fr->win, fr->img->ptr, 0, 0); } -void reset(t_fractol *fr) +void reset(t_fractol *fr) { fr->frac->mov.x = 0; fr->frac->mov.y = 0; @@ -36,7 +46,7 @@ void reset(t_fractol *fr) void change_limit_up(t_fractol *fr) { - fr->frac->max_itr +=50; + fr->frac->max_itr += 50; new_and_clear_image(fr); parallel_fractal(fr); mlx_put_image_to_window(fr->mlx, fr->win, fr->img->ptr, 0, 0); @@ -51,4 +61,4 @@ void change_limit_down(t_fractol *fr) parallel_fractal(fr); mlx_put_image_to_window(fr->mlx, fr->win, fr->img->ptr, 0, 0); } -} \ No newline at end of file +} diff --git a/src/hooks_funcs_2.c b/src/hooks_funcs_2.c index 5ac874c..6734fd8 100644 --- a/src/hooks_funcs_2.c +++ b/src/hooks_funcs_2.c @@ -1,42 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hooks_funcs_2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/24 19:26:53 by gtertysh #+# #+# */ +/* Updated: 2017/03/24 19:28:31 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "fractol.h" -void zoom_in(t_fractol *fr, int x, int y) +void zoom_in(t_fractol *fr, int x, int y) { int center_x; - int center_y; + int center_y; center_x = WIDTH / 2; center_y = HEIGHT / 2; new_and_clear_image(fr); fr->frac->mov.z *= 1.5; - fr->frac->mov.x += ((float)x - center_x) / (center_x * fr->frac->mov.z) * 0.8; - fr->frac->mov.y += ((float)y - center_y) / (center_y * fr->frac->mov.z) * 0.8; + fr->frac->mov.x += ((float)x - center_x) / (center_x * fr->frac->mov.z) + * 0.8; + fr->frac->mov.y += ((float)y - center_y) / (center_y * fr->frac->mov.z) + * 0.8; parallel_fractal(fr); mlx_put_image_to_window(fr->mlx, fr->win, fr->img->ptr, 0, 0); - } -void zoom_out(t_fractol *fr) +void zoom_out(t_fractol *fr) { new_and_clear_image(fr); fr->frac->mov.z /= 2; parallel_fractal(fr); mlx_put_image_to_window(fr->mlx, fr->win, fr->img->ptr, 0, 0); - } -void move_left(t_fractol *fr) +void move_left(t_fractol *fr) { - printf("z %Lf\n", fr->frac->mov.z); fr->frac->mov.x -= 1.f / fr->frac->mov.z * 0.2; new_and_clear_image(fr); parallel_fractal(fr); mlx_put_image_to_window(fr->mlx, fr->win, fr->img->ptr, 0, 0); } -void move_right(t_fractol *fr) +void move_right(t_fractol *fr) { - printf("z %Lf\n", fr->frac->mov.z); fr->frac->mov.x += 1.f / fr->frac->mov.z * 0.2; new_and_clear_image(fr); parallel_fractal(fr); diff --git a/src/image_routine.c b/src/image_routine.c index b6a31eb..8a07db3 100644 --- a/src/image_routine.c +++ b/src/image_routine.c @@ -27,10 +27,10 @@ void new_and_clear_image(t_fractol *fr) } } -void put_pixel_to_image(int x, int y, t_color col, t_fractol *fr) +void put_pixel_to_image(int x, int y, t_color c, t_fractol *fr) { if (x < 0 || y < 0 || x >= fr->w_width || y >= fr->w_height) return ; *(int *)((fr->img->data + x * fr->img->bpp / 8 + y * fr->img->l_size)) = - color_to_int(col); + (c.red << 16) | (c.grn << 8) | c.blu; } diff --git a/src/julia.c b/src/julia.c index e933095..257d855 100644 --- a/src/julia.c +++ b/src/julia.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* julia.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/24 19:34:44 by gtertysh #+# #+# */ +/* Updated: 2017/03/24 19:34:46 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "fractol.h" int complex_jul(t_complex new, t_complex com_const, t_frac_data *data) @@ -11,7 +23,7 @@ int complex_jul(t_complex new, t_complex com_const, t_frac_data *data) old = new; 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) + if ((new.rl * new.rl + new.im * new.im) > 4) break ; i++; } @@ -21,11 +33,10 @@ int complex_jul(t_complex new, t_complex com_const, t_frac_data *data) t_color julia(t_frac_data *ju, int x, int y) { t_complex new; - int i; + 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 = complex_jul(new, ju->com_const, ju); return (gradient((float)i / ju->max_itr, ju->grd)); } - diff --git a/src/mandelbrot.c b/src/mandelbrot.c index 5a829cb..c000e42 100644 --- a/src/mandelbrot.c +++ b/src/mandelbrot.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mandelbrot.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/24 19:33:30 by gtertysh #+# #+# */ +/* Updated: 2017/03/24 19:33:56 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "fractol.h" int complex_man(t_complex new, t_complex com_const, t_frac_data *data) @@ -11,7 +23,7 @@ int complex_man(t_complex new, t_complex com_const, t_frac_data *data) old = new; 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) + if ((new.rl * new.rl + new.im * new.im) > 4) break ; i++; } @@ -21,12 +33,12 @@ int complex_man(t_complex new, t_complex com_const, t_frac_data *data) t_color mandelbrot(t_frac_data *ma, int x, int y) { t_complex new; - int i; + int i; new.rl = 1.5 * (x - WIDTH / 2) / (0.5 * ma->mov.z * WIDTH) + - ma->mov.x - 0.7; + ma->mov.x - 0.7; new.im = (y - HEIGHT / 2) / (0.5 * ma->mov.z * HEIGHT) + - ma->mov.y; + ma->mov.y; i = complex_man(ma->com_const, new, ma); return (gradient((float)i / ma->max_itr, ma->grd)); } diff --git a/src/parse_input.c b/src/parse_input.c index a8dd5e7..c01821f 100644 --- a/src/parse_input.c +++ b/src/parse_input.c @@ -1,6 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_input.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/24 19:32:10 by gtertysh #+# #+# */ +/* Updated: 2017/03/24 19:32:11 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include -int check_argument(char *arg) +int check_argument(char *arg) { if (!ft_strcmp(arg, "julia")) return (0); @@ -19,9 +31,9 @@ void error(void) void parse_input(int ac, char **av) { - int i; - int frac_type; - int pid; + int i; + int frac_type; + int pid; if (ac < 2) error(); @@ -40,4 +52,4 @@ void parse_input(int ac, char **av) } else error(); -} \ No newline at end of file +} diff --git a/src/threads_routine.c b/src/threads_routine.c index 149b03b..ac004f5 100644 --- a/src/threads_routine.c +++ b/src/threads_routine.c @@ -1,9 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* threads_routine.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/24 19:35:25 by gtertysh #+# #+# */ +/* Updated: 2017/03/24 19:35:26 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "fractol.h" -t_thread_data *thread_data_init(t_fractol *fr, int i) +t_thread_data *thread_data_init(t_fractol *fr, int i) { t_thread_data *data; - int img_strip; + int img_strip; img_strip = (int)ceil(HEIGHT / NUM_THREADS) + 1; data = malloc(sizeof(t_thread_data)); @@ -14,7 +26,7 @@ t_thread_data *thread_data_init(t_fractol *fr, int i) return (data); } -void parallel_fractal(t_fractol *fr) +void parallel_fractal(t_fractol *fr) { pthread_t threads[NUM_THREADS]; pthread_attr_t attr; @@ -34,4 +46,4 @@ void parallel_fractal(t_fractol *fr) i = 0; while (i < NUM_THREADS) pthread_join(threads[i++], NULL); -} \ No newline at end of file +}