read the point rotate
This commit is contained in:
parent
baf3b58979
commit
021791ae5a
153
fdf.c
153
fdf.c
@ -14,68 +14,177 @@
|
||||
|
||||
|
||||
//# include "MLX42.h"
|
||||
#define WRONG_LINE_LENGTH "Error: wrong line length. Expected %d, got %d\n"
|
||||
#define MALLOC_ERROR "Error: malloc failed\n"
|
||||
#define FILE_ERROR "Error: could not open file %s\n"
|
||||
#define USAGE_ERROR "Usage: %s <filename>\n"
|
||||
|
||||
void rotate_x(t_point **points, double angle, int size)
|
||||
{
|
||||
int i;
|
||||
int previous_y;
|
||||
|
||||
i = 0;
|
||||
while (i < size)
|
||||
{
|
||||
previous_y = (*points)[i].y;
|
||||
(*points)[i].y = previous_y * cos(angle) + (*points)[i].z * sin(angle);
|
||||
(*points)[i].z = -previous_y * sin(angle) + (*points)[i].z * cos(angle);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
void rotate_y(t_point **points, double angle, int size)
|
||||
{
|
||||
int i;
|
||||
int previous_x;
|
||||
|
||||
i = 0;
|
||||
while (i < size)
|
||||
{
|
||||
previous_x = (*points)[i].x;
|
||||
(*points)[i].x = previous_x * cos(angle) + (*points)[i].z * sin(angle);
|
||||
(*points)[i].z = -previous_x * sin(angle) + (*points)[i].z * cos(angle);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
void rotate_z(t_point **points, double angle, int size)
|
||||
{
|
||||
int i;
|
||||
int previous_x;
|
||||
int previous_y;
|
||||
|
||||
i = 0;
|
||||
while (i < size)
|
||||
{
|
||||
previous_x = (*points)[i].x;
|
||||
previous_y = (*points)[i].y;
|
||||
(*points)[i].x = previous_x * cos(angle) - previous_y * sin(angle);
|
||||
(*points)[i].y = previous_x * sin(angle) + previous_y * cos(angle);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int get_map_sizes(char *filename, t_map *map)
|
||||
{
|
||||
int fd;
|
||||
const int fd = open(filename, O_RDONLY);
|
||||
char *line;
|
||||
int width;
|
||||
int height;
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
ft_printf("Error: could not open file %s\n", filename);
|
||||
return (0);
|
||||
}
|
||||
return (ft_printf(FILE_ERROR, filename), 0);
|
||||
width = 0;
|
||||
height = 0;
|
||||
while (true)
|
||||
{
|
||||
line = get_next_line(fd);
|
||||
if (!line)
|
||||
break ;
|
||||
width = ft_count_words(line, ' ');
|
||||
height++;
|
||||
free(line);
|
||||
}
|
||||
close(fd);
|
||||
if (map->width != 0 && map->width != width)
|
||||
return (ft_printf(WRONG_LINE_LENGTH, map->width, width), 0);
|
||||
else if (map->width == 0)
|
||||
map->width = width;
|
||||
map->height = height;
|
||||
return (1);
|
||||
map->height++;
|
||||
}
|
||||
return (close(fd), 1);
|
||||
}
|
||||
|
||||
void free_line_and_split(char **line, char ***split)
|
||||
{
|
||||
char **split_start;
|
||||
|
||||
split_start = *split;
|
||||
free(*line);
|
||||
*line = NULL;
|
||||
while (**split)
|
||||
{
|
||||
free(**split);
|
||||
**split = NULL;
|
||||
(*split)++;
|
||||
}
|
||||
free(split_start);
|
||||
*split = NULL;
|
||||
}
|
||||
|
||||
int read_map(char *filename, t_map *map)
|
||||
{
|
||||
int fd;
|
||||
char *line;
|
||||
int y;
|
||||
int x;
|
||||
char **split;
|
||||
|
||||
|
||||
get_map_sizes(filename, map);
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0)
|
||||
{
|
||||
ft_printf("Error: could not open file %s\n", filename);
|
||||
return (0);
|
||||
}
|
||||
return (ft_printf(FILE_ERROR, filename), 0);
|
||||
y = map->height - 1;
|
||||
while (true)
|
||||
{
|
||||
ft_printf("%s\n", line);
|
||||
free(line);
|
||||
x = 0;
|
||||
line = get_next_line(fd);
|
||||
if (!line)
|
||||
break ;
|
||||
split = ft_split(line, ' ');
|
||||
while (split[x])
|
||||
{
|
||||
map->points[y * map->width + x].x = x - map->width / 2;
|
||||
map->points[y * map->width + x].y = map->height / 2 - y;
|
||||
map->points[y * map->width + x].z = ft_atoi(split[x]);
|
||||
x++;
|
||||
}
|
||||
free_line_and_split(&line, &split);
|
||||
y--;
|
||||
}
|
||||
close(fd);
|
||||
return (1);
|
||||
}
|
||||
|
||||
void print_map(t_map *map)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
y = 0;
|
||||
while (y < map->height)
|
||||
{
|
||||
x = 0;
|
||||
while (x < map->width)
|
||||
{
|
||||
printf("(%.2f, %.2f, %.2f)\n",
|
||||
map->points[y * map->width + x].x,
|
||||
map->points[y * map->width + x].y,
|
||||
map->points[y * map->width + x].z);
|
||||
x++;
|
||||
}
|
||||
ft_printf("\n");
|
||||
y++;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
t_map *map;
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
ft_printf("Usage: %s <filename>\n", argv[0]);
|
||||
ft_printf(USAGE_ERROR, argv[0]);
|
||||
return (EXIT_FAILURE);
|
||||
}
|
||||
if(!read_map(argv[1]))
|
||||
map = malloc(sizeof(t_map));
|
||||
if (!map)
|
||||
return (ft_printf(MALLOC_ERROR), EXIT_FAILURE);
|
||||
get_map_sizes(argv[1], map);
|
||||
map->points = malloc(map->width * map->height * sizeof(t_point));
|
||||
if (!map->points)
|
||||
return (ft_printf(MALLOC_ERROR), EXIT_FAILURE);
|
||||
if (!read_map(argv[1], map))
|
||||
return (EXIT_FAILURE);
|
||||
print_map(map);
|
||||
rotate_x(&map->points, M_PI / 4, map->width * map->height);
|
||||
rotate_z(&map->points, M_PI / 4, map->width * map->height);
|
||||
print_map(map);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
10
inc/fdf.h
10
inc/fdf.h
@ -12,17 +12,20 @@
|
||||
|
||||
#ifndef FDF_H
|
||||
# define FDF_H
|
||||
|
||||
# include "libft.h"
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include <fcntl.h>
|
||||
# include <stdbool.h>
|
||||
# include <math.h>
|
||||
# include <stdio.h>
|
||||
|
||||
typedef struct s_point
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
int z;
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
} t_point;
|
||||
|
||||
typedef struct s_map
|
||||
@ -30,6 +33,7 @@ typedef struct s_map
|
||||
t_point *points;
|
||||
int width;
|
||||
int height;
|
||||
int z_max;
|
||||
} t_map;
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user