-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_draw.c
126 lines (117 loc) · 3.34 KB
/
ft_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_draw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mrudyk <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/02/17 17:44:01 by mrudyk #+# #+# */
/* Updated: 2018/02/20 16:12:39 by mrudyk ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
void my_put_pixel(int x, int y, int col, t_mlx *mlx_lib)
{
if (x < WIDTH && y < HEIGHT && x >= 0 && y >= 0)
{
mlx_lib->data[(x * (mlx_lib->bp / 8)) +
(y * mlx_lib->sl) + BLUE] = col & 0x0000FF;
mlx_lib->data[(x * (mlx_lib->bp / 8)) +
(y * mlx_lib->sl) + GREEN] = (col >> 8) & 0x00FF;
mlx_lib->data[(x * (mlx_lib->bp / 8)) +
(y * mlx_lib->sl) + RED] = col >> 16;
}
}
void ft_draw_line(t_mlx *mlx_lib, t_cord cord)
{
mlx_lib->delta_x = abs(cord.x_2 - cord.x_1);
mlx_lib->delta_y = abs(cord.y_2 - cord.y_1);
mlx_lib->sign_x = cord.x_1 < cord.x_2 ? 1 : -1;
mlx_lib->sign_y = cord.y_1 < cord.y_2 ? 1 : -1;
mlx_lib->error = mlx_lib->delta_x - mlx_lib->delta_y;
my_put_pixel(cord.x_2, cord.y_2, 0x40E0D0, mlx_lib);
while (cord.x_1 != cord.x_2 || cord.y_1 != cord.y_2)
{
my_put_pixel(cord.x_1, cord.y_1, cord.color_1, mlx_lib);
mlx_lib->error_2 = mlx_lib->error * 2;
if (mlx_lib->error_2 > -mlx_lib->delta_y)
{
mlx_lib->error -= mlx_lib->delta_y;
cord.x_1 += mlx_lib->sign_x;
}
if (mlx_lib->error_2 < mlx_lib->delta_x)
{
mlx_lib->error += mlx_lib->delta_x;
cord.y_1 += mlx_lib->sign_y;
}
}
}
void ft_set_middle(t_mlx *mlx_lib)
{
int x;
int y;
x = 0;
while (x < mlx_lib->map_y)
{
y = 0;
while (y < mlx_lib->map_x)
{
mlx_lib->point[x][y].new_pos_x += WC + mlx_lib->off_x;
mlx_lib->point[x][y].new_pos_y += HC + mlx_lib->off_y;
y++;
}
x++;
}
}
void ft_drawing_lines_x(t_mlx *mlx_lib)
{
int w;
int h;
t_cord cord;
h = 0;
while (h < mlx_lib->map_y)
{
w = 0;
while (w < mlx_lib->map_x)
{
cord.x_1 = mlx_lib->point[h][w].new_pos_x;
cord.y_1 = mlx_lib->point[h][w].new_pos_y;
cord.color_1 = mlx_lib->point[h][w].color;
if (w + 1 < mlx_lib->map_x)
{
cord.x_2 = mlx_lib->point[h][w + 1].new_pos_x;
cord.y_2 = mlx_lib->point[h][w + 1].new_pos_y;
cord.color_2 = mlx_lib->point[h][w + 1].color;
ft_draw_line(mlx_lib, cord);
}
w++;
}
h++;
}
}
void ft_drawing_lines_y(t_mlx *mlx_lib)
{
int w;
int h;
t_cord cord;
h = 0;
while (h < mlx_lib->map_y)
{
w = 0;
while (w < mlx_lib->map_x)
{
cord.x_1 = mlx_lib->point[h][w].new_pos_x;
cord.y_1 = mlx_lib->point[h][w].new_pos_y;
cord.color_1 = mlx_lib->point[h][w].color;
if (h + 1 < mlx_lib->map_y)
{
cord.x_2 = mlx_lib->point[h + 1][w].new_pos_x;
cord.y_2 = mlx_lib->point[h + 1][w].new_pos_y;
cord.color_2 = mlx_lib->point[h + 1][w].color;
ft_draw_line(mlx_lib, cord);
}
w++;
}
h++;
}
}