-
Notifications
You must be signed in to change notification settings - Fork 0
/
fdf_bresenham.c
83 lines (75 loc) · 2.01 KB
/
fdf_bresenham.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fdf_bresenham.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jimchoi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/20 11:20:53 by jimchoi #+# #+# */
/* Updated: 2024/04/09 20:52:43 by jimchoi ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
int abs(int num)
{
if (num < 0)
return (-num);
else
return (num);
}
void bhm_drow_w(t_xy xy, t_linedata line, t_data image)
{
int count;
int i;
count = 0;
i = 0;
while (i < line.w)
{
xy.x1 += line.add_x;
count += line.h;
if (count >= line.w)
{
xy.y1 += line.add_y;
count -= line.w;
}
if (xy.x1 < WIDTH && xy.y1 < HEIGHT && xy.x1 >= 0 && xy.y1 >= 0)
my_mlx_pixel_put(&image, xy.x1, xy.y1, WHITE);
i++;
}
}
void bhm_drow_h(t_xy xy, t_linedata line, t_data image)
{
int count;
int i;
count = 0;
i = 0;
while (i < line.h)
{
xy.y1 += line.add_y;
count += line.w;
if (count >= line.h)
{
xy.x1 += line.add_x;
count -= line.h;
}
if (xy.x1 < WIDTH && xy.y1 < HEIGHT && xy.x1 >= 0 && xy.y1 >= 0)
my_mlx_pixel_put(&image, xy.x1, xy.y1, WHITE);
i++;
}
}
void bresenham(t_xy xy, int x2, int y2, t_data *image)
{
t_linedata line;
line.add_x = 1;
line.add_y = 1;
line.w = abs(x2 - xy.x1);
line.h = abs(y2 - xy.y1);
if (x2 - xy.x1 < 0)
line.add_x = -1;
if (y2 - xy.y1 < 0)
line.add_y = -1;
if (line.w >= line.h)
bhm_drow_w(xy, line, *image);
else
bhm_drow_h(xy, line, *image);
}