This commit is contained in:
Gregory Tertyshny 2017-03-21 20:50:44 +02:00
parent 50b9a36fae
commit 8e9693a2be
6 changed files with 119 additions and 67 deletions

View file

@ -21,4 +21,5 @@ target_link_libraries(fractol
-lft
-lmlx
"-framework OpenGL"
"-framework AppKit") # linkage
"-framework AppKit"
-lpthread) # linkage

View file

@ -17,10 +17,11 @@
# include "mlx.h"
# include <math.h>
# include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
# 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);

View file

@ -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);
//}

View file

@ -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;

View file

@ -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;
t_complex old;
int i;
t_complex old;
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;
new_and_clear_image(fr);
x = 0;
while (x < fr->w_width)
{
y = 0;
while(y < fr->w_height)
while (y < fr->w_height)
{
julia_computation(x, y, fr->fractals->jul);
put_pixel_to_image(x, y, &fr->fractals->jul->color, fr);
y++;
}
x = 0;
while(x < fr->w_width)
{
fr->fractals->jul->x = x;
fr->fractals->jul->y = y;
julia_computation(fr);
x++;
}
y++;
}
mlx_put_image_to_window(fr->mw->mlx, fr->mw->win, fr->image, 0, 0);
}

View file

@ -11,7 +11,8 @@
/* ************************************************************************** */
#include "fractol.h"
#include <time.h>
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);
}