-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.c
82 lines (75 loc) · 2.38 KB
/
draw.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cado-car <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/30 19:02:49 by cado-car #+# #+# */
/* Updated: 2021/10/05 22:41:17 by cado-car ### ########.fr */
/* */
/* ************************************************************************** */
#include "../include/fdf.h"
void bresenham(t_fdf *fdf, t_point start, t_point end)
{
float x_step;
float y_step;
int max_steps;
int i_line;
t_color *color;
x_step = end.x - start.x;
y_step = end.y - start.y;
max_steps = (int)max(absolute(x_step), absolute(y_step));
x_step /= max_steps;
y_step /= max_steps;
color = color_init(start, end);
if (!color)
close_all(fdf, 8);
i_line = 0;
while (i_line < max_steps)
{
start.color = get_color(color, i_line++, max_steps);
if (start.x > 0 && start.y > 0 && start.x < WINDOW_WIDTH && start.y < \
WINDOW_HEIGHT)
pixel_to_image(fdf->image, start.x, start.y, start.color);
start.x += x_step;
start.y += y_step;
}
free(color);
}
void pixel_to_image(t_image *image, float x, float y, int color)
{
int pixel;
pixel = ((int)y * image->line_bytes) + ((int)x * 4);
if (image->endian == 1)
{
image->buffer[pixel + 0] = (color >> 24);
image->buffer[pixel + 1] = (color >> 16) & 0xff;
image->buffer[pixel + 2] = (color >> 8) & 0xff;
image->buffer[pixel + 3] = (color) & 0xff;
}
else if (image->endian == 0)
{
image->buffer[pixel + 0] = (color) & 0xff;
image->buffer[pixel + 1] = (color >> 8) & 0xff;
image->buffer[pixel + 2] = (color >> 16) & 0xff;
image->buffer[pixel + 3] = (color >> 24);
}
}
void clear_image(t_image *image, int image_size)
{
int x;
int y;
ft_bzero(image->buffer, image_size);
y = 0;
while (y < WINDOW_HEIGHT)
{
x = 0;
while (x < WINDOW_WIDTH)
{
pixel_to_image(image, x, y, BACKGROUND_DEFAULT);
x++;
}
y++;
}
}