-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector2.c
More file actions
60 lines (57 loc) · 1 KB
/
vector2.c
File metadata and controls
60 lines (57 loc) · 1 KB
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
#include "vector2.h"
#include <math.h>
vector2 v2Add(vector2 a, vector2 b) {
vector2 c;
c.x = a.x + b.x;
c.y = a.y + b.y;
return c;
}
vector2 v2Sub(vector2 a, vector2 b) {
vector2 c;
c.x = a.x - b.x;
c.y = a.y - b.y;
return c;
}
vector2 v2Neg(vector2 a) {
a.x = -a.x;
a.y = -a.y;
return a;
}
vector2 v2Unit(vector2 v) {
vector2 a;
a.x = v.x/v2Len(v);
a.y = v.y/v2Len(v);
return a;
}
vector2 v2Rotate(float ang, vector2 a){
vector2 r;
r.x = a.x*cos(ang) - a.y*sin(ang);
r.y = a.x*sin(ang) + a.y*cos(ang);
return r;
}
vector2 v2sMul(float f, vector2 v) {
v.x = f*v.x;
v.y = f*v.y;
return v;
}
vector2 v2Polar(float len, float angle) {
vector2 a;
a.x = len*cos(angle);
a.y = len*sin(angle);
return a;
}
float v2Dot(vector2 a, vector2 b) {
return a.x*b.x + a.y*b.y;
}
float v2Cross(vector2 a, vector2 b) {
return a.x*b.y - a.y*b.x;
}
float v2Len(vector2 v) {
return sqrt(v.x*v.x + v.y*v.y);
}
float v2SPow(vector2 v) {
return v.x*v.x + v.y*v.y;
}
float v2Arg(vector2 v) {
return atan2(v.y,v.x);
}