-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.c
72 lines (65 loc) · 2.04 KB
/
algorithm.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* algorithm.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: junyojeo <junyojeo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/15 17:21:18 by junyojeo #+# #+# */
/* Updated: 2023/05/19 02:11:59 by junyojeo ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
static void set_sign(t_point *sign, t_point start, t_point end)
{
if (start.x < end.x)
sign->x = 1;
else
sign->x = -1;
if (start.y < end.y)
sign->y = 1;
else
sign->y = -1;
}
static void set_delta(t_point *delta, t_point start, t_point end)
{
delta->x = (int)fabs((double)(end.x - start.x));
delta->y = (int)fabs((double)(end.y - start.y));
}
static void my_mlx_pixel_put(t_mlx *mlx, int x, int y, unsigned int color)
{
int i;
if (0 <= x && x < SCRN_WIDTH && y >= 0 && y < SCRN_HEIGHT)
{
i = (x * mlx->bits_per_pixel / 8) + (y * mlx->line_length);
mlx->addr[i] = color;
mlx->addr[++i] = color >> 8;
mlx->addr[++i] = color >> 16;
}
}
void bresenham(t_mlx *mlx, t_point start, t_point end)
{
t_point delta;
t_point sign;
t_point cur;
int error[2];
set_delta(&delta, start, end);
set_sign(&sign, start, end);
error[0] = delta.x - delta.y;
cur = start;
while (cur.x != end.x || cur.y != end.y)
{
my_mlx_pixel_put(mlx, cur.x, cur.y, cur.color);
error[1] = error[0] * 2;
if (error[1] > -delta.y)
{
error[0] -= delta.y;
cur.x += sign.x;
}
if (error[1] < delta.x)
{
error[0] += delta.x;
cur.y += sign.y;
}
}
}