-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.c
79 lines (71 loc) · 2.17 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sanmetol <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/16 16:48:30 by sanmetol #+# #+# */
/* Updated: 2024/05/30 17:27:30 by sanmetol ### ########.fr */
/* */
/* ************************************************************************** */
#include "fractol.h"
static void my_pixel_put(int x, int y, t_image *img, int color)
{
int offset;
offset = (y * img->line_length) + (x * (img->bits_per_pixel / 8));
*(unsigned int *)(img->addr + offset) = color;
}
static void fractal_select(t_complex *z, t_complex *c, t_fractal *fractal)
{
if (!ft_strncmp(fractal->name, "julia", 5))
{
c->r = fractal->julia_x;
c->i = fractal->julia_y;
}
else
{
c->r = z->r;
c->i = z->i;
}
}
static void handle_pixel(int x, int y, t_complex z, t_fractal *fractal)
{
t_complex c;
int k;
int color;
k = 0;
fractal_select(&z, &c, fractal);
while (k < fractal->iterations)
{
z = sum_complex(z, c, fractal->name);
if ((z.r * z.r + z.i * z.i) > 4)
{
color = scale(k, BLACK, WHITE, fractal->iterations);
my_pixel_put(x, y, &fractal->img, color);
return ;
}
k++;
}
my_pixel_put(x, y, &fractal->img, BLACK);
}
void fractal_render(t_fractal *fractal)
{
int x;
int y;
t_complex z;
y = -1;
while (++y < HEIGHT)
{
x = -1;
z.i = scale(y, 2, -2, HEIGHT) * fractal->zoom + fractal->shift_y;
x = -1;
while (++x < WIDTH)
{
z.r = scale(x, -2, 2, WIDTH) * fractal->zoom + fractal->shift_x;
handle_pixel(x, y, z, fractal);
}
}
mlx_put_image_to_window(fractal->mlx_connection,
fractal->mlx_window, fractal->img.img, 0, 0);
}