switch from list to 2d array for vectors

This commit is contained in:
Gregory 2017-03-13 10:53:25 +02:00
parent 516477ded7
commit 09fa5a69a9
7 changed files with 102 additions and 190 deletions

View file

@ -55,7 +55,9 @@ LIBFT_FLAGS = -lft -L $(LIBFT_DIR)
MLX_FLAGS = -lmlx -lXext -lX11 -L $(MLX_DIR) MLX_FLAGS = -lmlx -lXext -lX11 -L $(MLX_DIR)
OTHER_FLAGS = -lm OTHER_FLAGS = -lm
CC_FLAGS = -Werror -Wextra -Wall -O3 CC_FLAGS = -Werror -Wextra -Wall -O3
FLAGS := $(CC_FLAGS) $(LIBFT_FLAGS) $(MLX_FLAGS) $(OTHER_FLAGS) DEBUG = -g
FLAGS := $(CC_FLAGS) $(LIBFT_FLAGS) $(MLX_FLAGS) $(OTHER_FLAGS) \
$(DEBUG)
# compiler # compiler
@ -72,7 +74,7 @@ $(NAME): $(OBJ) $(LIBFT_DIR)$(LIBFT) $(MLX_DIR)$(MLX)
$(OBJ_DIR)%.o : $(SRC_DIR)%.c $(OBJ_DIR)%.o : $(SRC_DIR)%.c
@echo "$(CYAN)Compiling object files: $(BLUE)$@$(NORMAL)" @echo "$(CYAN)Compiling object files: $(BLUE)$@$(NORMAL)"
@$(CC) $(CC_FLAGS) $(INC) -c $< -o $@ @$(CC) $(CC_FLAGS) $(DEBUG) $(INC) -c $< -o $@
$(LIBFT_DIR)$(LIBFT): $(LIBFT_DIR)$(LIBFT):
@echo "$(CYAN)Compiling libft library...$(NORMAL)" @echo "$(CYAN)Compiling libft library...$(NORMAL)"

View file

@ -58,7 +58,7 @@ typedef struct s_mw
typedef struct s_map typedef struct s_map
{ {
t_list *lst_map; t_vec **arr_vec;
t_pnt **arr_pnt; t_pnt **arr_pnt;
int x_max; int x_max;
int y_max; int y_max;
@ -123,6 +123,7 @@ void render(t_FDF *FDF);
void free_tab(char **tab); void free_tab(char **tab);
void quit(t_FDF *FDF); void quit(t_FDF *FDF);
void swap_init(t_swap *s); void swap_init(t_swap *s);
void print_help(void);
t_color color_init(int red, int green, int blue); t_color color_init(int red, int green, int blue);
t_color color_lerp(t_color c1, t_color c2, float step); t_color color_lerp(t_color c1, t_color c2, float step);

View file

@ -19,4 +19,19 @@ void swap_init(t_swap *s)
s->swap_coord = 0; s->swap_coord = 0;
s->swap_x = 1; s->swap_x = 1;
s->swap_z = 0; s->swap_z = 0;
}
void print_help(void)
{
ft_putstr(
"Usage: "
"fdf [OPTIONS] MAP_PATH"
"\n\n"
"Opitons:\n"
"--heatmap \"R,G,B - R,G,B\""
"\toptional parameter for specifying color range of\n"
"\t\t\t\theatmap in RGB format. Each color component\n"
"\t\t\t\tshould be between 0 and 255.\n"
"\t\t\t\tPlease, put range in double quotes, for example:\n\n"
"\t\t\t\tfdf --heatmap \"125,255,6 - 250,41,1\" ~/maps/42.fdf\n\n");
} }

View file

@ -7,154 +7,16 @@
#include "fdf.h" #include "fdf.h"
void circle(t_FDF *FDF)
{
t_pnt p1;
t_pnt p2;
double angle;
for (int i = 0; i < 360; i += 1)
{
p1.x = 250;
p1.y = 250;
angle = i * M_PI / 180;
p2.x = p1.x + 250 * sin(angle);
p2.y = p1.y + 250 * cos(angle);
line(p1, p2, FDF);
}
}
void test_line(t_FDF *FDF)
{
t_pnt p1;
t_pnt p2;
p1.x = 0;
p1.y = 0;
p2.x = 500;
for (p2.y = 0; p2.y <= 500; p2.y += 4)
{
line(p1, p2, FDF);
mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0);
}
for (p2.x = 500; p2.x >= 0; p2.x -= 4)
{
line(p1, p2, FDF);
mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0);
}
p1.x = 500;
p1.y = 0;
p2.x = 0;
for (p2.y = 0; p2.y <= 500; p2.y += 4)
{
line(p1, p2, FDF);
mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0);
}
for (p2.x = 0; p2.x <= 500; p2.x += 4)
{
line(p1, p2, FDF);
mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0);
}
p1.x = 500;
p1.y = 500;
p2.x = 500;
p2.y = 0;
for (p2.x = 500; p2.x >= 0; p2.x -= 4)
{
line(p1, p2, FDF);
mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0);
}
for (p2.y = 0; p2.y <= 500; p2.y += 4)
{
line(p1, p2, FDF);
mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0);
}
p1.x = 0;
p1.y = 500;
p2.x = 500;
p2.y = 500;
for (p2.y = 500; p2.y >= 0; p2.y -= 4)
{
line(p1, p2, FDF);
mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0);
}
for (p2.x = 500; p2.x >= 0; p2.x -= 4)
{
line(p1, p2, FDF);
mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0);
}
mlx_clear_window(FDF->mw->mlx, FDF->mw->win);
p1.x = 0;
p1.y = 0;
p2.y = 500;
for (p2.x = 0; p2.x <= 500; p2.x += 1)
{
line(p1, p2, FDF);
mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0);
p1.x += 1;
}
mlx_clear_window(FDF->mw->mlx, FDF->mw->win);
p1.x = 0;
p1.y = 0;
p2.x = 500;
for (p2.y = 0; p2.y <= 500; p2.y += 1)
{
line(p1, p2, FDF);
mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0);
p1.y += 1;
}
mlx_clear_window(FDF->mw->mlx, FDF->mw->win);
p1.x = 500;
p1.y = 0;
p2.x = 500;
for (; p2.x >= 0; p2.x -= 1)
{
line(p1, p2, FDF);
mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0);
p1.x -= 1;
}
mlx_clear_window(FDF->mw->mlx, FDF->mw->win);
p1.x = 0;
p1.y = 500;
p2.x = 500;
for (p2.y = 500; p2.y >= 0; p2.y -= 1)
{
line(p1, p2, FDF);
mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0);
p1.y -= 1;
}
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
t_FDF *FDF; t_FDF *FDF;
// int i;
// int j; if (argc < 2)
// int rgb; {
// t_color c1; print_help();
// t_color c2;
// t_color c3;
if (argc != 2)
return (0); return (0);
}
FDF = FDF_init(argv[1]); FDF = FDF_init(argv[1]);
// i = 0;
// c1 = color_init(225, 13, 14);
// c2 = color_init(20, 177, 4);
// c3 = c1;
// while (i < FDF->w_height)
// {
// j = 0;
// rgb = color_to_int(c3);
// while (j < FDF->w_width)
// {
// *((int *)FDF->image_data + j + i * FDF->w_width) = rgb;
// j++;
// }
// c3 = color_lerp(c1, c2, (float)i / FDF->w_height);
// i++;
// }
// mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0);
render(FDF); render(FDF);
mlx_key_hook(FDF->mw->win, key_hook, FDF); mlx_key_hook(FDF->mw->win, key_hook, FDF);
mlx_loop(FDF->mw->mlx); mlx_loop(FDF->mw->mlx);

View file

@ -44,35 +44,66 @@ char *to_str_map(char *path)
return (0); return (0);
} }
void map_dimensions(t_map *map) int tab_length(char **tab)
{ {
int x; int len;
int y;
int max_z;
int min_z;
t_list *l;
l = map->lst_map; len = 0;
x = 0; while (*tab)
y = 0;
max_z = 0;
min_z = 0;
while (l)
{ {
if (((t_vec *)(l->content))->x > x) tab++;
x = (int)((t_vec *)(l->content))->x; if (*tab)
if (((t_vec *)(l->content))->y > y) len++;
y = (int)((t_vec *)(l->content))->y; }
if (((t_vec *)(l->content))->z > max_z) return (len);
max_z = (int)((t_vec *)(l->content))->z; }
if (((t_vec *)(l->content))->z < min_z)
min_z = (int)((t_vec *)(l->content))->z; int map_width(char **tab)
l = l->next; {
int width;
char **line_tab;
line_tab = ft_strsplit(*tab, ' ');
width = tab_length(line_tab);
free_tab(line_tab);
return (width);
}
void min_max_z(t_map *map, int i, int j)
{
if (map->arr_vec[i][j].z > map->z_max)
map->z_max = map->arr_vec[i][j].z;
if (map->arr_vec[i][j].z < map->z_max)
map->z_min = map->arr_vec[i][j].z;
}
void to_vec_arr(char **tab, t_map *map)
{
char **line;
int i;
int j;
map->y_max = tab_length(tab);
map->x_max = map_width(tab);
map->arr_vec = malloc(sizeof(t_vec *) * (map->y_max + 1));
i = 0;
while (i <= map->y_max)
{
j = 0;
line = ft_strsplit(tab[i], ' ');
map->arr_vec[i] = malloc(sizeof(t_vec) * (map->x_max + 1));
while (j <= map->x_max)
{
map->arr_vec[i][j].z = ft_atoi(line[j]);
map->arr_vec[i][j].x = j;
map->arr_vec[i][j].y = i;
min_max_z(map, i, j);
j++;
}
free_tab(line);
i++;
} }
map->x_max = x;
map->y_max = y;
map->z_max = max_z;
map->z_min = min_z;
} }
t_map *map_init(char *path) t_map *map_init(char *path)
@ -82,13 +113,15 @@ t_map *map_init(char *path)
t_map *map; t_map *map;
map = malloc(sizeof(t_map)); map = malloc(sizeof(t_map));
map->z_min = 0;
map->x_max = 0;
if (!(str_map = to_str_map(path))) if (!(str_map = to_str_map(path)))
{ {
ft_putstr("Wrong path to a map! For help help put \"fdf --help\"");
quit(0); quit(0);
} }
tab_map = ft_strsplit(str_map, '\n'); tab_map = ft_strsplit(str_map, '\n');
map->lst_map = to_lst_map(tab_map); to_vec_arr(tab_map, map);
map_dimensions(map);
free(str_map); free(str_map);
free_tab(tab_map); free_tab(tab_map);
return (map); return (map);

View file

@ -2,24 +2,21 @@
void pnt_init(t_map *map) void pnt_init(t_map *map)
{ {
t_list *lst_map;
int i; int i;
int j; int j;
float z; float z;
lst_map = map->lst_map;
map->arr_pnt = malloc(sizeof(t_pnt *) * (map->y_max + 1)); map->arr_pnt = malloc(sizeof(t_pnt *) * (map->y_max + 1));
i = 0; i = 0;
while (i <= map->y_max && lst_map) while (i <= map->y_max)
{ {
map->arr_pnt[i] = malloc(sizeof(t_pnt) * (map->x_max + 1)); map->arr_pnt[i] = malloc(sizeof(t_pnt) * (map->x_max + 1));
j = 0; j = 0;
while (j <= map->x_max) while (j <= map->x_max)
{ {
z = ((t_vec *)(lst_map->content))->z - map->z_min; z = map->arr_vec[i][j].z - map->z_min;
map->arr_pnt[i][j].z = z; map->arr_pnt[i][j].z = z;
first_color(&map->arr_pnt[i][j], z, map); first_color(&map->arr_pnt[i][j], z, map);
lst_map = lst_map->next;
j++; j++;
} }
i++; i++;

View file

@ -18,19 +18,16 @@ t_trnsf *transfor_mat_init()
void transform(t_mat4 *mat, t_map *map) void transform(t_mat4 *mat, t_map *map)
{ {
t_list *vec_list;
int i; int i;
int j; int j;
vec_list = map->lst_map;
i = 0; i = 0;
while (i <= map->y_max) while (i <= map->y_max)
{ {
j = 0; j = 0;
while (j <= map->x_max) while (j <= map->x_max)
{ {
vec_mat_mult(mat, vec_list->content, &map->arr_pnt[i][j]); vec_mat_mult(mat, &map->arr_vec[i][j], &map->arr_pnt[i][j]);
vec_list = vec_list->next;
j++; j++;
} }
i++; i++;
@ -120,22 +117,27 @@ void draw(t_FDF *FDF)
void center(t_map *map) void center(t_map *map)
{ {
t_list *l;
double d_x; double d_x;
double d_y; double d_y;
double d_z; double d_z;
int i;
int j;
d_x = map->x_max / 2.0f; d_x = map->x_max / 2.0f;
d_y = map->y_max / 2.0f; d_y = map->y_max / 2.0f;
d_z = map->z_max / 2.0f; d_z = map->z_max / 2.0f;
l = map->lst_map; i = 0;
while (l) while (i <= map->y_max)
{ {
((t_vec *)(l->content))->x = ((t_vec *)(l->content))->x - d_x; j = 0;
((t_vec *)(l->content))->y = ((t_vec *)(l->content))->y - d_y; while (j <= map->x_max)
((t_vec *)(l->content))->z = ((t_vec *)(l->content))->z - d_z; {
l = l->next; map->arr_vec[i][j].z -= d_z;
map->arr_vec[i][j].x -= d_x;
map->arr_vec[i][j].y -= d_y;
j++;
}
i++;
} }
} }