-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_utils.c
131 lines (120 loc) · 3.11 KB
/
draw_utils.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
127
128
129
130
131
/* ************************************************************************** */
/* */
/* :::::::: */
/* draw_utils.c :+: :+: */
/* +:+ */
/* By: jvan-hal <[email protected]> +#+ */
/* +#+ */
/* Created: 2023/02/16 12:56:43 by jvan-hal #+# #+# */
/* Updated: 2023/02/28 13:07:50 by jvan-hal ######## odam.nl */
/* */
/* ************************************************************************** */
#include<math.h>
#include"fdf.h"
static void plot(unsigned int x, unsigned int y, double z, t_fdf *fdf)
{
unsigned int i;
uint8_t *pixels;
x -= fdf->ox;
y -= fdf->oy;
if (x < 0 || y < 0)
return ;
if (x > fdf->texture[0]->width - 1 || y > fdf->texture[0]->height - 1)
return ;
pixels = fdf->texture[0]->pixels;
i = (x + (y * fdf->texture[0]->width)) * 4;
if (z > 50)
pixels[i] = 255;
if (z > 20 && z < 50)
{
pixels[i] = 160;
pixels[i + 1] = 160;
}
if (z >= 0 && (z <= 20 || z >= 50))
pixels[i + 1] = 255;
if (z < 0 || z > 50)
pixels[i + 2] = 255;
if (z > 20 && z < 50)
pixels[i + 2] = 160;
pixels[i + 3] = 255;
}
static double init_difference_low(int d[], t_coord *start, t_coord *end,
int *yi)
{
double zdiff;
d[0] = end->sx - start->sx;
d[1] = end->sy - start->sy;
zdiff = (end->z - start->z) / sqrt(pow(d[0], 2) + pow(d[1], 2));
*yi = 1;
if (d[1] < 0)
{
*yi = -1;
d[1] = -d[1];
}
return (zdiff);
}
void draw_line_low(t_coord *start, t_coord *end, t_fdf *fdf)
{
int d[2];
int delta;
int pos[2];
int yi;
double zdiff;
zdiff = init_difference_low(d, start, end, &yi);
delta = (2 * d[1]) - d[0];
pos[1] = start->sy;
pos[0] = start->sx;
while (pos[0] != end->sx)
{
plot(pos[0], pos[1], start->z + (zdiff * sqrt(pow(pos[0] - start->sx,
2) + pow(pos[1] - start->sy, 2))), fdf);
if (delta > 0)
{
pos[1] += yi;
delta += 2 * (d[1] - d[0]);
}
else
delta += 2 * d[1];
++pos[0];
}
}
static double init_difference_high(int d[], t_coord *start, t_coord *end,
int *xi)
{
double zdiff;
d[0] = end->sx - start->sx;
d[1] = end->sy - start->sy;
zdiff = (end->z - start->z) / sqrt(pow(d[0], 2) + pow(d[1], 2));
*xi = 1;
if (d[0] < 0)
{
*xi = -1;
d[0] = -d[0];
}
return (zdiff);
}
void draw_line_high(t_coord *start, t_coord *end, t_fdf *fdf)
{
int d[2];
int delta;
int pos[2];
int xi;
double zdiff;
zdiff = init_difference_high(d, start, end, &xi);
delta = (2 * d[0]) - d[1];
pos[1] = start->sy;
pos[0] = start->sx;
while (pos[1] != end->sy)
{
plot(pos[0], pos[1], start->z + (zdiff * sqrt(pow(pos[0] - start->sx,
2) + pow(pos[1] - start->sy, 2))), fdf);
if (delta > 0)
{
pos[0] += xi;
delta += 2 * (d[0] - d[1]);
}
else
delta += 2 * d[0];
++pos[1];
}
}