tgrekov-fdf
HIVE fdf May 2024
Loading...
Searching...
No Matches
project_map.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* project_map.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tgrekov <tgrekov@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/06/15 06:23:14 by tgrekov #+# #+# */
9/* Updated: 2024/06/24 08:54:11 by tgrekov ### ########.fr */
10/* */
11/* ************************************************************************** */
12
20#include <math.h>
21#include "../fdf.h"
22#include "map.h"
23
31static void apply_offset(t_map map, int x_offset, int y_offset)
32{
33 int x;
34 int y;
35
36 y = 0;
37 while (y < map.height)
38 {
39 x = 0;
40 while (x < map.width)
41 {
42 map.point[y][x].projected[0] -= x_offset;
43 map.point[y][x].projected[1] -= y_offset;
44 x++;
45 }
46 y++;
47 }
48}
49
56static void project(t_map map, int scale)
57{
58 static double cs_30[2] = {0, 0};
59 int x;
60 int y;
61 int y_offset;
62
63 if (cs_30[0] == 0)
64 cs_30[0] = cos(30 * M_PI / 180);
65 if (cs_30[1] == 0)
66 cs_30[1] = sin(30 * M_PI / 180);
67 y_offset = 0;
68 y = 0;
69 while (y < map.height)
70 {
71 x = 0;
72 while (x < map.width)
73 {
74 map.point[y][x].projected[0] = (x - y) * scale * cs_30[0];
75 map.point[y][x].projected[1] = (x + y) * scale * cs_30[1]
76 - map.point[y][x].height * scale;
77 if (map.point[y][x++].projected[1] < y_offset)
78 y_offset = map.point[y][x - 1].projected[1];
79 }
80 y++;
81 }
82 apply_offset(map, map.point[map.height - 1][0].projected[0], y_offset);
83}
84
91static void calc_size(t_map map, int *size)
92{
93 int x;
94 int y;
95
96 size[0] = map.point[0][map.width - 1].projected[0]
97 - map.point[map.height - 1][0].projected[0];
98 size[1] = 0;
99 y = 0;
100 while (y < map.height)
101 {
102 x = 0;
103 while (x < map.width)
104 {
105 if (map.point[y][x].projected[1] > size[1])
106 size[1] = map.point[y][x].projected[1];
107 x++;
108 }
109 y++;
110 }
111 size[1] += 1;
112}
113
121int project_map(t_map map, int *size)
122{
123 int viewport[2];
124 int scale;
125
126 mlx_get_monitor_size(0, &viewport[0], &viewport[1]);
127 viewport[0] *= FDF_TARGET_VIEWPORT_PERCENT;
128 viewport[1] *= FDF_TARGET_VIEWPORT_PERCENT;
129 scale = viewport[1];
130 if (viewport[0] < scale)
131 scale = viewport[0];
132 scale = scale
133 / (sqrt(map.width * map.width + map.height * map.height)
134 * atan(sin(30 * M_PI / 180)));
135 if (scale < 1)
136 scale = 1;
137 project(map, scale);
138 calc_size(map, size);
139 while (size[0] > viewport[0] || size[1] > viewport[1])
140 {
141 if (scale == 1)
142 return (1);
143 project(map, --scale);
144 calc_size(map, size);
145 }
146 return (0);
147}
static void calc_size(t_map map, int *size)
Get boundaries of projected map.
Definition project_map.c:91
static void apply_offset(t_map map, int x_offset, int y_offset)
Apply offset to projected coordinates.
Definition project_map.c:31
static void project(t_map map, int scale)
Generate coordinates from map input and scale via isometric projection.
Definition project_map.c:56
int project_map(t_map map, int *size)
Project map and attempt to automatically scale to viewport.
Map data.
Definition map.h:54
int width
Number of columns in the map.
Definition map.h:55
int height
Number of rows in the map.
Definition map.h:56
t_point ** point
2d array of points in [y][x] order
Definition map.h:57
int projected[2]
Projected coordinates for this point.
Definition map.h:38
int height
Height extracted from map file.
Definition map.h:37