read the point rotate

This commit is contained in:
whaffman 2024-12-06 14:37:53 +01:00
parent baf3b58979
commit 021791ae5a
2 changed files with 140 additions and 27 deletions

157
fdf.c
View File

@ -14,68 +14,177 @@
//# include "MLX42.h" //# 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 get_map_sizes(char *filename, t_map *map)
{ {
int fd; const int fd = open(filename, O_RDONLY);
char *line; char *line;
int width; int width;
int height;
fd = open(filename, O_RDONLY);
if (fd < 0) if (fd < 0)
{ return (ft_printf(FILE_ERROR, filename), 0);
ft_printf("Error: could not open file %s\n", filename);
return (0);
}
width = 0; width = 0;
height = 0;
while (true) while (true)
{ {
line = get_next_line(fd); line = get_next_line(fd);
if (!line) if (!line)
break ; break ;
width = ft_count_words(line, ' '); width = ft_count_words(line, ' ');
height++;
free(line); free(line);
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++;
} }
close(fd); return (close(fd), 1);
map->width = width; }
map->height = height;
return (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 read_map(char *filename, t_map *map)
{ {
int fd; int fd;
char *line; char *line;
int y;
int x;
char **split;
get_map_sizes(filename, map);
fd = open(filename, O_RDONLY); fd = open(filename, O_RDONLY);
if (fd < 0) if (fd < 0)
{ return (ft_printf(FILE_ERROR, filename), 0);
ft_printf("Error: could not open file %s\n", filename); y = map->height - 1;
return (0);
}
while (true) while (true)
{ {
ft_printf("%s\n", line); x = 0;
free(line);
line = get_next_line(fd); 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); close(fd);
return (1); 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[]) int main(int argc, char *argv[])
{ {
t_map *map;
if (argc != 2) if (argc != 2)
{ {
ft_printf("Usage: %s <filename>\n", argv[0]); ft_printf(USAGE_ERROR, argv[0]);
return (EXIT_FAILURE); 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); 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); return (EXIT_SUCCESS);
} }

View File

@ -12,17 +12,20 @@
#ifndef FDF_H #ifndef FDF_H
# define FDF_H # define FDF_H
# include "libft.h" # include "libft.h"
# include <stdlib.h> # include <stdlib.h>
# include <unistd.h> # include <unistd.h>
# include <fcntl.h> # include <fcntl.h>
# include <stdbool.h> # include <stdbool.h>
# include <math.h>
# include <stdio.h>
typedef struct s_point typedef struct s_point
{ {
int x; float x;
int y; float y;
int z; float z;
} t_point; } t_point;
typedef struct s_map typedef struct s_map
@ -30,6 +33,7 @@ typedef struct s_map
t_point *points; t_point *points;
int width; int width;
int height; int height;
int z_max;
} t_map; } t_map;