-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline_utils.c
More file actions
101 lines (92 loc) · 2.71 KB
/
line_utils.c
File metadata and controls
101 lines (92 loc) · 2.71 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvalient <mvalient@student.42urduliz.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/18 22:46:06 by mvalient #+# #+# */
/* Updated: 2022/12/21 13:33:01 by mvalient ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
static void ft_put_line(t_img *data, t_line *line)
{
int pixels;
double delta_x;
double delta_y;
double pixel_x;
double pixel_y;
if (!line)
return ;
delta_x = line->endx - line->beginx;
delta_y = line->endy - line->beginy;
pixels = (int) sqrt(pow(delta_x, 2) + pow(delta_y, 2));
delta_x = delta_x / pixels;
delta_y = delta_y / pixels;
pixel_x = line->beginx;
pixel_y = line->beginy;
while (pixels)
{
ft_put_pixel(data, (int) pixel_x, (int) pixel_y, line->color);
pixel_x += delta_x;
pixel_y += delta_y;
pixels--;
}
free(line);
}
static t_line *ft_horizontal_line(t_point *point)
{
t_line *line;
t_pixel pixel;
if ((point && point->next) && point->y == point->next->y)
{
line = malloc(sizeof(t_line));
pixel = ft_point_to_pixel(point);
line->beginx = pixel.x;
line->beginy = pixel.y;
pixel = ft_point_to_pixel(point->next);
line->endx = pixel.x;
line->endy = pixel.y;
line->color = pixel.color;
return (line);
}
return (NULL);
}
static t_line *ft_vertical_line(t_point *point)
{
t_point *next_point;
t_line *line;
t_pixel pixel;
if (!point->next)
return (NULL);
next_point = point->next;
while (point->x != next_point->x)
{
if (next_point->next)
next_point = next_point->next;
else
break ;
}
line = malloc(sizeof(t_line));
pixel = ft_point_to_pixel(point);
line->beginx = pixel.x;
line->beginy = pixel.y;
pixel = ft_point_to_pixel(next_point);
line->endx = pixel.x;
line->endy = pixel.y;
line->color = pixel.color;
return (line);
}
void ft_put_lines(t_img *data, t_mlx *mlx)
{
t_point *current_point;
current_point = mlx->points;
while (current_point)
{
ft_put_line(data, ft_horizontal_line(current_point));
ft_put_line(data, ft_vertical_line(current_point));
mlx_put_image_to_window(mlx->mlx, mlx->win, data->img, 0, 0);
current_point = current_point->next;
}
}