-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_map_utils.c
133 lines (124 loc) · 3.14 KB
/
init_map_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
132
133
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_map_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lbaela <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 10:49:06 by lbaela #+# #+# */
/* Updated: 2021/10/27 14:06:29 by lbaela ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
#include "error_messages.h"
/*
** Calculate colors based on height (z) for each point on map.
*/
void update_colors(t_fdf *fdf)
{
int i;
int j;
i = 0;
while (i < fdf->map_i.map_h)
{
j = 0;
while (j < fdf->map_i.map_w)
{
fdf->map_i.color[i][j] = get_grad_color(COL_RED, COL_YELLOW,
find_percent(fdf->map_i.z_min,
fdf->map_i.z_max,
fdf->map_i.map[i][j]));
j++;
}
i++;
}
}
/*
** Allocate memory and copy color-value to the color-array.
*/
void color_to_arr(t_point *flat_map, t_fdf *fdf)
{
t_point *tmp;
int x;
int y;
y = fdf->map_i.map_h - 1;
tmp = flat_map;
while (y >= 0)
{
x = 0;
fdf->map_i.color[y] = (int *)malloc(sizeof(int) * fdf->map_i.map_w);
if (!fdf->map_i.color[y])
{
free_points(flat_map);
free_arr(fdf->map_i.map, fdf->map_i.map_h);
free_arr(fdf->map_i.color, fdf->map_i.map_h);
exit_on_error(ERR_MEM);
}
while (x < fdf->map_i.map_w && tmp)
{
fdf->map_i.color[y][x] = tmp->color;
tmp = tmp->next;
x++;
}
y--;
}
}
void check_min_max(t_fdf *fdf, int z, int first)
{
if (first)
{
fdf->map_i.z_min = z;
fdf->map_i.z_max = z;
}
if (fdf->map_i.z_min > z)
fdf->map_i.z_min = z;
if (fdf->map_i.z_max < z)
fdf->map_i.z_max = z;
}
/*
** Allocate memory and copy z-value to the map-array.
*/
void map_to_arr(t_point *flat_map, t_fdf *fdf)
{
t_point *tmp;
int x;
int y;
y = fdf->map_i.map_h - 1;
tmp = flat_map;
check_min_max(fdf, tmp->z, 1);
while (y >= 0)
{
x = 0;
fdf->map_i.map[y] = (int *)malloc(sizeof(int) * fdf->map_i.map_w);
if (!fdf->map_i.map[y])
{
free_points(flat_map);
free_arr(fdf->map_i.map, fdf->map_i.map_h);
exit_on_error(ERR_MEM);
}
while (x < fdf->map_i.map_w && tmp)
{
check_min_max(fdf, tmp->z, 0);
fdf->map_i.map[y][x++] = tmp->z;
tmp = tmp->next;
}
y--;
}
}
/*
** Convert t_point list to int-matrix inside fdf-structure.
*/
void list_to_arr(t_point *flat_map, t_fdf *fdf)
{
fdf->map_i.map = (int **)malloc(sizeof(int *) * fdf->map_i.map_h);
fdf->map_i.color = (int **)malloc(sizeof(int *) * fdf->map_i.map_h);
if (!fdf->map_i.map || !fdf->map_i.color)
{
free(fdf->map_i.map);
free(fdf->map_i.color);
free_points(flat_map);
exit_on_error(ERR_MEM);
}
map_to_arr(flat_map, fdf);
color_to_arr(flat_map, fdf);
}