-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec_math.c
67 lines (57 loc) · 1.63 KB
/
vec_math.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
62
63
64
65
66
67
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vec_math.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dbrophy <dbrophy@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/21 14:54:26 by dbrophy #+# #+# */
/* Updated: 2020/02/21 14:54:26 by dbrophy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftmath.h"
#include "libft.h"
/*
** Only YOU are responsible for ensuring that your vectors are the right
** size(s). This library assumes you have ensured this, to work faster.
*/
double vec_mag(t_vector *vec)
{
double sum;
double a;
int i;
i = vec->size;
sum = 0.0;
while (i--)
{
a = vec->vals[i];
sum += a * a;
}
return (ft_sqrt(sum));
}
void vec_norm(t_vector *vec)
{
double mag;
int i;
mag = vec_mag(vec);
i = vec->size;
while (i--)
vec->vals[i] /= mag;
}
void vec_muls(t_vector *vec, double scalar)
{
int i;
i = vec->size;
while (i--)
vec->vals[i] *= scalar;
}
double vec_dot(t_vector *a, t_vector *b)
{
double sum;
int i;
sum = 0;
i = a->size;
while (i--)
sum += a->vals[i] * b->vals[i];
return (sum);
}