-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.c
83 lines (75 loc) · 2.37 KB
/
render.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ana-lda- <ana-lda-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/06 14:07:26 by ana-lda- #+# #+# */
/* Updated: 2024/08/12 16:01:46 by ana-lda- ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
/** @brief put a pixel in the image buffer*/
static void my_pixel_put(int x, int y, t_img *img, int color)
{
int offset;
offset = (y * img->line_len) + (x * (img->bits_per_pixel / 8));
*(unsigned int *)(img->pixels_ptr + offset) = color;
}
/** @brief alternate between to sets*/
static void mandel_or_julia(t_complex *z, t_complex *c, t_fractal *fractal)
{
if (!ft_strncmp(fractal->name, "julia", 5))
{
c->x = fractal->julia_x;
c->y = fractal->julia_y;
}
else
{
c->x = z->x;
c->y = z->y;
}
}
/** checking if the point escaped, how many times to iterate
* z^2 + c */
static void handle_pixel(int x, int y, t_fractal *fractal)
{
t_complex c;
t_complex z;
int i;
int color;
i = 0;
z.x = (map(x, -2, +2, WIDTH) * fractal->zoom) + fractal->shift_x;
z.y = (map(y, +2, -2, HEIGHT) * fractal->zoom) + fractal->shift_y;
mandel_or_julia(&z, &c, fractal);
while (i < fractal->iterations_definition)
{
z = sum_complex(square_complex(z), c);
if ((z.x * z.x) + (z.y * z.y) > fractal->escape_value)
{
color = map(i, fractal->color_1, fractal->color_2,
fractal->iterations_definition);
my_pixel_put(x, y, &fractal->img, color);
return ;
}
i++;
}
my_pixel_put(x, y, &fractal->img, BLACK);
}
void fractal_render(t_fractal *fractal)
{
int y;
int x;
y = -1;
while (++y < HEIGHT)
{
x = -1;
while (++x < WIDTH)
{
handle_pixel(x, y, fractal);
}
}
mlx_put_image_to_window(fractal->mlx_connection, fractal->mlx_window,
fractal->img.img_ptr, 0, 0);
}