diff --git a/Makefile b/Makefile index 2268aa3..c5aa78b 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ MLX42 = $(MLX42_PATH)/build/libmlx42.a OBJ_PATH = obj VPATH = src -SOURCES = fdf.c +SOURCES = fdf.c fdf_rotations.c OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) diff --git a/inc/fdf.h b/inc/fdf.h index 413d106..be76a11 100644 --- a/inc/fdf.h +++ b/inc/fdf.h @@ -13,13 +13,22 @@ #ifndef FDF_H # define FDF_H -# include "libft.h" # include # include # include # include # include # include +# include "libft.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 \n" + +#define WIDTH 1024 +#define HEIGHT 1024 typedef struct s_point { @@ -36,5 +45,14 @@ typedef struct s_map int z_max; } t_map; +int get_map_sizes(char *filename, t_map *map); +void free_line_and_split(char **line, char ***split); +int read_map(char *filename, t_map *map); +t_map *parse_map(char *filename); +void print_map(t_map *map); +void rotate_x(t_point **points, double angle, int size); +void rotate_y(t_point **points, double angle, int size); +void rotate_z(t_point **points, double angle, int size); + #endif diff --git a/fdf.c b/src/fdf.c similarity index 67% rename from fdf.c rename to src/fdf.c index f8e8870..d90e900 100644 --- a/fdf.c +++ b/src/fdf.c @@ -12,60 +12,6 @@ #include "fdf.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 \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) { const int fd = open(filename, O_RDONLY); @@ -142,10 +88,10 @@ int read_map(char *filename, t_map *map) return (1); } -void print_map(t_map *map) +void print_map(t_map *map) { - int x; - int y; + int x; + int y; y = 0; while (y < map->height) @@ -164,6 +110,67 @@ void print_map(t_map *map) } } +t_map *parse_map(char *filename) +{ + t_map *map; + + map = malloc(sizeof(t_map)); + if (!map) + return (ft_printf(MALLOC_ERROR), NULL); + if (!get_map_sizes(filename, map)) + return (free(map), NULL); + map->points = malloc(map->width * map->height * sizeof(t_point)); + if (!map->points) + return (free(map), ft_printf(MALLOC_ERROR), NULL); + if (!read_map(filename, map)) + return (free(map->points), free(map), NULL); + return (map); +} + +int draw_map(t_map *map) +{ + mlx_t* mlx; + mlx_image_t* img; + int i; + int x; + int y; + + mlx = mlx_init(WIDTH, HEIGHT, "FdF", false); + if (!mlx) + return (EXIT_FAILURE); + img = mlx_new_image(mlx, WIDTH, HEIGHT); + i = 0; + if (!img || (mlx_image_to_window(mlx, img, 0, 0) < 0)) + return (EXIT_FAILURE); + x = 0; + + + while (x < WIDTH) + { + y = 0; + while (y < HEIGHT) + { + mlx_put_pixel(img, x, y, 0x000000FF); + y++; + } + x++; + } + + while (i < map->width * map->height) + { + x = WIDTH / 2 + map->points[i].x * WIDTH / 2.0 / map->width; + y = HEIGHT / 2 + map->points[i].y * HEIGHT / 2.0 / map->height; + printf("x: %.2f, y: %.2f\n", x, y); + if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT) + mlx_put_pixel(img, WIDTH - x, HEIGHT - y, 0xFFFFFFFF); + i++; + } + mlx_loop(mlx); + mlx_terminate(mlx); + return (EXIT_SUCCESS); +} + + int main(int argc, char *argv[]) { t_map *map; @@ -173,18 +180,12 @@ int main(int argc, char *argv[]) ft_printf(USAGE_ERROR, argv[0]); return (EXIT_FAILURE); } - map = malloc(sizeof(t_map)); + map = parse_map(argv[1]); 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); + rotate_x(&map->points, M_PI / 8, map->width * map->height); + draw_map(map); return (EXIT_SUCCESS); } diff --git a/src/fdf_rotations.c b/src/fdf_rotations.c new file mode 100644 index 0000000..4848d91 --- /dev/null +++ b/src/fdf_rotations.c @@ -0,0 +1,46 @@ +#include "fdf.h" + +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++; + } +}