fix isometric projection using constants

This commit is contained in:
Willem Haffmans 2024-12-08 21:40:17 +01:00
parent 391f1ba069
commit e25eec251f
2 changed files with 15 additions and 8 deletions

View File

@ -28,8 +28,11 @@
# define FILE_ERROR "Error: could not open file %s\n"
# define USAGE_ERROR "Usage: %s <filename>\n"
# define WIDTH 1500
# define HEIGHT 1500
# define SQRT2 1.41421356237309514547
# define SQRT6 2.44948974278317788134
# define WIDTH 1024
# define HEIGHT 640
typedef struct s_point_3d
{

View File

@ -78,7 +78,7 @@ int read_map(char *filename, t_map *map)
{
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]) * 0.04;
map->points[y * map->width + x].z = ft_atoi(split[x]) * 0.08;
map->points[y * map->width + x].color = 0xFFFFFFFF;
x++;
}
@ -150,6 +150,7 @@ void fdf_project_isometric(t_map *map)
int i;
double x;
double y;
const int scale = fmax(map->height, map->width) * SQRT2;
i = 0;
map->projected = malloc(map->width * map->height * sizeof(t_point_2d));
@ -157,10 +158,13 @@ void fdf_project_isometric(t_map *map)
return ;
while (i < map->width * map->height)
{
x = (map->points[i].x - map->points[i].y) * cos(0.523599);
y = -map->points[i].z + (map->points[i].x + map->points[i].y) * sin(0.523599);
map->projected[i].x = x * 0.5 * WIDTH * 0.01 + WIDTH / 2;
map->projected[i].y = y * 0.5 * HEIGHT * 0.01 + HEIGHT / 2;
// x = (map->points[i].x - map->points[i].y) * cos(0.523599);
// y = -map->points[i].z + (map->points[i].x + map->points[i].y) * sin(0.523599);
x = (map->points[i].x - map->points[i].y) / SQRT2;
y = (map->points[i].x + map->points[i].y - 2 * map->points[i].z) / SQRT6;
map->projected[i].x = x * WIDTH / scale + WIDTH / 2;
map->projected[i].y = y * HEIGHT / scale + HEIGHT / 2;
map->projected[i].color = map->points[i].color;
i++;
}
@ -175,7 +179,7 @@ int draw_map(t_map *map)
mlx = mlx_init(WIDTH, HEIGHT, "FdF", false);
if (!mlx)
return (EXIT_FAILURE);
img = mlx_new_image(mlx, WIDTH, HEIGHT);
img = mlx_new_image(mlx, mlx->width, mlx->height);
i = 0;
if (!img || (mlx_image_to_window(mlx, img, 0, 0) < 0))
return (EXIT_FAILURE);