set.c
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* set.c :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: gychoi <gychoi@student.42seoul.kr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2022/12/31 23:35:06 by gychoi #+# #+# */ 9 /* Updated: 2023/01/04 15:24:18 by gychoi ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #include "fdf.h" 14 15 static int set_color(int z, t_map map) 16 { 17 double ratio; 18 19 ratio = get_ratio(map.z_min * 2, map.z_max * 2, z * -1); 20 if (ratio < 0.2) 21 return (0x0000FF00); 22 else if (ratio < 0.5) 23 return (0x0088FF00); 24 else if (ratio < 0.8) 25 return (0x00FFFF00); 26 else 27 return (0x00FFFFFF); 28 } 29 30 static double get_z_margin(t_fdf *fdf) 31 { 32 double adj_x; 33 double adj_y; 34 double x_margin; 35 double y_margin; 36 37 adj_x = 10; 38 x_margin = fdf->map.x_margin; 39 while (x_margin < fdf->map.width) 40 { 41 x_margin *= 10; 42 adj_x *= 0.1; 43 } 44 adj_y = 10; 45 y_margin = fdf->map.y_margin; 46 while (y_margin < fdf->map.height) 47 { 48 y_margin *= 10; 49 adj_y *= 0.1; 50 } 51 return ((adj_x + adj_y) / 2); 52 } 53 54 void set_coord_margin(t_fdf *fdf) 55 { 56 fdf->map.x_margin = (double)SCREEN_WIDTH / fdf->map.width / 2; 57 if (fdf->map.x_margin < 1) 58 { 59 while (fdf->map.x_margin < 1) 60 fdf->map.x_margin += 0.1; 61 } 62 fdf->map.y_margin = (double)SCREEN_HEIGHT / fdf->map.height / 2; 63 if (fdf->map.y_margin < 1) 64 { 65 while (fdf->map.y_margin < 1) 66 fdf->map.y_margin += 0.1; 67 } 68 fdf->map.z_margin = get_z_margin(fdf); 69 fdf->map.z_max *= fdf->map.z_margin; 70 fdf->map.z_min *= fdf->map.z_margin; 71 } 72 73 static t_point set_point(t_coord coord, t_fdf *fdf) 74 { 75 t_point new; 76 double bending; 77 78 new.x = coord.x * fdf->map.x_margin; 79 new.y = coord.y * fdf->map.y_margin; 80 new.z = coord.z * -1 * fdf->offset.z * fdf->map.z_margin; 81 bending = (new.x * new.x * fdf->offset.bend) \ 82 + (new.y * new.y * fdf->offset.bend); 83 new.z += bending; 84 new.color = set_color(new.z, fdf->map); 85 new.x *= fdf->offset.zoom; 86 new.y *= fdf->offset.zoom; 87 new.z *= fdf->offset.zoom; 88 rotate_x(&new, fdf->angle.alpha); 89 rotate_y(&new, fdf->angle.beta); 90 rotate_z(&new, fdf->angle.gamma); 91 new.x += SCREEN_WIDTH / 2 + fdf->offset.x; 92 new.y += SCREEN_HEIGHT / 2 + fdf->offset.y; 93 new.x = (int)new.x; 94 new.y = (int)new.y; 95 new.z = (int)new.z; 96 return (new); 97 } 98 99 t_point **set_points(t_fdf *fdf) 100 { 101 t_point **points; 102 t_point *point; 103 int y; 104 int x; 105 106 points = fdf_malloc(sizeof(t_point *) * fdf->map.height); 107 y = 0; 108 while (y < fdf->map.height) 109 { 110 x = 0; 111 point = fdf_malloc(sizeof(t_point) * fdf->map.width); 112 while (x < fdf->map.width) 113 { 114 point[x] = set_point(fdf->coords[y][x], fdf); 115 x++; 116 } 117 points[y] = point; 118 y++; 119 } 120 return (points); 121 }