-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_angle.c
51 lines (47 loc) · 1.73 KB
/
get_angle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_angle.c :+: :+: */
/* +:+ */
/* By: dnoom <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/04/06 11:25:31 by dnoom #+# #+# */
/* Updated: 2022/04/06 11:25:31 by dnoom ######## odam.nl */
/* */
/* ************************************************************************** */
#include "miniRT.h"
#include <math.h>
t_angle set_correct_angle(t_angle s_angle)
{
if (s_angle.angle < 0)
s_angle.angle = -M_PI - s_angle.angle;
else
s_angle.angle = M_PI - s_angle.angle;
return (s_angle);
}
t_angle get_angle_to(t_vec3f dir, t_vec3f to)
{
t_angle s_angle;
t_vec3f rot_plane;
float len_rot_plane;
s_angle.angle = 0;
s_angle.k = vec3f_init(0, 0, 0);
if (vec3f_equal(dir, vec3f_mul(to, -1)))
{
s_angle.angle = M_PI;
if (to.y == 0 && to.z == 0)
s_angle.k = vec3f_unit(vec3f_cross(dir, vec3f_init(0, 1, 0)));
else
s_angle.k = vec3f_unit(vec3f_cross(dir, vec3f_init(1, 0, 0)));
}
else
{
rot_plane = vec3f_cross(dir, to);
len_rot_plane = vec3f_len(rot_plane);
s_angle.angle = asin(len_rot_plane);
if (vec3f_dot(dir, to) < 0)
s_angle = set_correct_angle(s_angle);
s_angle.k = vec3f_unit(rot_plane);
}
return (s_angle);
}