forked from sandsmark/kart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
53 lines (42 loc) · 812 Bytes
/
vector.h
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
#ifndef VECTOR_H
#define VECTOR_H
#include <math.h>
#include "defines.h"
inline float vec_dot(vec2 a, vec2 b)
{
return a.x * b.x + a.y * b.y;
}
inline float vec_length(vec2 v)
{
return sqrt(vec_dot(v, v));
}
inline float vec_angle(vec2 a, vec2 b)
{
return (atan2(b.y, b.x) - atan2(a.y, a.x)) * 180 / PI;
}
inline void vec_copy(vec2 in, vec2 *out)
{
out->x = in.x;
out->y = in.y;
}
inline void vec_normalize(vec2 *v)
{
v->x /= vec_length(*v);
v->y /= vec_length(*v);
}
inline void vec_rotate(vec2 *v, float angle)
{
float theta = angle * PI / 180;
float cs = cos(theta);
float sn = sin(theta);
float old_x = v->x;
float old_y = v->y;
v->x = old_x * cs - old_y * sn;
v->y = old_x * sn + old_y * cs;
}
inline void vec_scale(vec2 *v, float s)
{
v->x *= s;
v->y *= s;
}
#endif /*VECTOR_H*/