-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathutils.c
86 lines (73 loc) · 1.58 KB
/
mathutils.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <math.h>
#include <stdlib.h>
#include "mathutils.h"
float lenDirX(float len, float dir){
return len * cos(dir*PI/180.0);
}
float lenDirY(float len, float dir){
return len * -sin(dir*PI/180.0);
}
int8_t sign(float val){
return val == 0 ? 0 : val > 0 ? 1 : -1;
}
float distancePoint(float x, float y, float xx, float yy){
x -= xx;
y -= yy;
x *= x;
y *= y;
return sqrt(x+y);
}
float distancePointV2(v2 a, v2 b){
return distancePoint(a.x, a.y, b.x, b.y);
}
float directionPoint(float x, float y, float xx, float yy){
float res = (atan2(yy-y, xx-x)*180)/PI;
return (yy > y) ? (360-res) : res;
}
float directionPointV2(v2 a, v2 b){
return directionPoint(a.x, a.y, b.x, b.y);
}
uint8_t pointInRect(float x, float y, v4 r){
if (r.w < 0){
r.w *= -1;
r.x -= r.w;
}
if (r.h < 0){
r.h *= -1;
r.y -= r.w;
}
return (x >= r.x && x <= r.x+r.w) &&
(y >= r.y && y <= r.y+r.h);
}
uint8_t pointInRectV2(v2 a, v4 b){
return pointInRect(a.x, a.y, b);
}
uint8_t pointInRectB(float x, float y, v4 r){
if (r.x > r.w){
float temp = r.x;
r.x = r.w;
r.w = temp;
}
if (r.y > r.h){
float temp = r.y;
r.y = r.h;
r.h = temp;
}
return (x >= r.x && x <= r.w) &&
(y >= r.y && y <= r.h);
}
uint8_t pointInRectV2B(v2 a, v4 b){
return pointInRectB(a.x, a.y, b);
}
void approachZero(int32_t* val, int32_t amount){
uint8_t s = (*val>0);
int8_t flip = s-(s==0);
*val += flip * (amount*(*val!=0));
*val *= (s == (*val>0));
}
void approachZeroF(float* val, float amount){
uint8_t s = (*val>0);
int8_t flip = s-(s==0);
*val -= flip * (amount*(*val!=0));
*val *= (s == (*val>0));
}