-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_vec2.c
61 lines (52 loc) · 1.87 KB
/
utils_vec2.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
52
53
54
55
56
57
58
59
60
61
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_vec2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khlavaty <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/13 15:48:39 by fvonsovs #+# #+# */
/* Updated: 2024/09/12 20:54:12 by khlavaty ### ########.fr */
/* */
/* ************************************************************************** */
#include "minirt.h"
// calculates dot product of two vectors
// dot product = two vectors return a single scalar value
// measures how much two vectors are aligned with each other
float vec_dot(t_float_3 a, t_float_3 b)
{
return (a.x * b.x + a.y * b.y + a.z * b.z);
}
// normalizes a vector
// division of its components by its magnitude
t_float_3 vec_normalize(t_float_3 v)
{
float length;
length = vec_length(v);
v.x /= length;
v.y /= length;
v.z /= length;
return (v);
}
// calculates cross product
// takes two vectors, returns a vector that is perpendicular to the first two
t_float_3 vec_cross(t_float_3 v1, t_float_3 v2)
{
t_float_3 res;
res.x = v1.y * v2.z - v1.z * v2.y;
res.y = v1.z * v2.x - v1.x * v2.z;
res.z = v1.x * v2.y - v1.y * v2.x;
return (res);
}
int is_zero_vector(t_float_3 vec)
{
return (vec.x == 0.0f && vec.y == 0.0f && vec.z == 0.0f);
}
float vec_cos(t_float_3 a, t_float_3 b)
{
float dot;
float t;
dot = vec_dot(a, b);
t = vec_length(a) * vec_length(b);
return (dot / t);
}