WEEKEND
This commit is contained in:
parent
37eeb3a045
commit
c1cf3e4ce7
2
Makefile
2
Makefile
@ -27,7 +27,7 @@ MLX42 = $(MLX42_PATH)/build/libmlx42.a
|
|||||||
OBJ_PATH = obj
|
OBJ_PATH = obj
|
||||||
|
|
||||||
VPATH = src
|
VPATH = src
|
||||||
SOURCES = fdf.c
|
SOURCES = fdf.c fdf_rotations.c
|
||||||
|
|
||||||
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
|
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))
|
||||||
|
|
||||||
|
|||||||
20
inc/fdf.h
20
inc/fdf.h
@ -13,13 +13,22 @@
|
|||||||
#ifndef FDF_H
|
#ifndef FDF_H
|
||||||
# define FDF_H
|
# define FDF_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 <math.h>
|
||||||
# include <stdio.h>
|
# include <stdio.h>
|
||||||
|
# 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 <filename>\n"
|
||||||
|
|
||||||
|
#define WIDTH 1024
|
||||||
|
#define HEIGHT 1024
|
||||||
|
|
||||||
typedef struct s_point
|
typedef struct s_point
|
||||||
{
|
{
|
||||||
@ -36,5 +45,14 @@ typedef struct s_map
|
|||||||
int z_max;
|
int z_max;
|
||||||
} t_map;
|
} 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
|
#endif
|
||||||
|
|||||||
@ -12,60 +12,6 @@
|
|||||||
|
|
||||||
#include "fdf.h"
|
#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 <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)
|
||||||
{
|
{
|
||||||
const int fd = open(filename, O_RDONLY);
|
const int fd = open(filename, O_RDONLY);
|
||||||
@ -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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
t_map *map;
|
t_map *map;
|
||||||
@ -173,18 +180,12 @@ int main(int argc, char *argv[])
|
|||||||
ft_printf(USAGE_ERROR, argv[0]);
|
ft_printf(USAGE_ERROR, argv[0]);
|
||||||
return (EXIT_FAILURE);
|
return (EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
map = malloc(sizeof(t_map));
|
map = parse_map(argv[1]);
|
||||||
if (!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);
|
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);
|
return (EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
46
src/fdf_rotations.c
Normal file
46
src/fdf_rotations.c
Normal file
@ -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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user