-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathlib.c
More file actions
43 lines (35 loc) · 676 Bytes
/
mathlib.c
File metadata and controls
43 lines (35 loc) · 676 Bytes
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
#include "mathlib.h"
#define RAD_2_DEG (180.0 / PI)
#define DEG_2_RAD (PI / 180.0)
double lerp(double a, double b, double t) {
return (1.0-t)*a + t*b;
}
double sign(double a) {
return a >= 0 ? 1.0 : -1.0;
}
Vec2 rotate(Vec2 v, double a) {
a *= DEG_2_RAD;
return (Vec2){
.x = v.x*cos(a) - v.y*sin(a),
.y = v.y*cos(a) + v.x*sin(a)
};
}
double magnitude(Vec2 v) {
return sqrt(v.x*v.x + v.y*v.y);
}
double sqr_magnitude(Vec2 v) {
return v.x*v.x + v.y*v.y;
}
Vec2 normalize(Vec2 v) {
double mag = magnitude(v);
return (Vec2){
.x = v.x / mag,
.y = v.y / mag
};
}
Vec2 invert(Vec2 v) {
return (Vec2){
.x = -v.x,
.y = -v.y
};
}