-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.c
More file actions
81 lines (73 loc) · 2.06 KB
/
draw.c
File metadata and controls
81 lines (73 loc) · 2.06 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apavelko <apavelko@student.unit.ua> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/03/02 17:06:00 by apavelko #+# #+# */
/* Updated: 2018/03/02 17:06:00 by apavelko ### ########.fr */
/* */
/* ************************************************************************** */
#include <math.h>
#include "../includes/fdf.h"
#include "../libft/libft.h"
void ft_swap_doubles(double *x, double *y)
{
double tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
int ft_dx_error(double y0, double y1, double dx, int *error)
{
*error += fabs(y1 - y0) * 2;
if (*error > dx)
{
*error -= dx * 2;
return (y1 > y0 ? 1 : -1);
}
return (0);
}
int ft_steep(t_vector *v0, t_vector *v1)
{
int steep;
steep = 0;
if (fabs(v0->x - v1->x) < fabs(v0->y - v1->y))
{
ft_swap_doubles(&v0->x, &v0->y);
ft_swap_doubles(&v1->x, &v1->y);
steep = 1;
}
if (v0->x > v1->x)
{
ft_swap_doubles(&v0->x, &v1->x);
ft_swap_doubles(&v0->y, &v1->y);
ft_swap(&v0->color, &v1->color);
}
return (steep);
}
void ft_draw_line(t_vector v0, t_vector v1, t_fdf *fdf)
{
int steep;
int error;
double dx;
double x;
double y;
steep = ft_steep(&v0, &v1);
error = 0;
dx = v1.x - v0.x;
x = v0.x;
y = v0.y;
while (x <= v1.x)
{
if (steep)
ft_putpixel(fdf->img, (int)y, (int)x,
ft_lerp(v0.color, v1.color, ft_lerp_p(x, v0.x, v1.x)));
else
ft_putpixel(fdf->img, (int)x, (int)y,
ft_lerp(v0.color, v1.color, ft_lerp_p(x, v0.x, v1.x)));
y += ft_dx_error(v0.y, v1.y, dx, &error);
x++;
}
}