-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgebra.cpp
76 lines (65 loc) · 1.38 KB
/
algebra.cpp
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
#ifndef ALGEBRA
#define ALGEBRA
#include <SFML/Graphics.hpp>
#include <math.h>
template<typename F>
F rad2deg(F angle) {
return angle * 180.0 / M_PI;
}
template<typename F>
F sign(F val){
if (val > 0){
return 1;
}
else if (val == 0){
return 0;
}
return -1;
}
template<typename F>
F sign(F val, F eps){
if (val-eps > 0){
return 1;
}
else if (val+eps < 0){
return -1;
}
return 0;
}
template<typename F>
F clip(F val, F low, F high){
if (val > high){
return high;
}
else if (val < low){
return low;
}
return val;
}
float dot(const sf::Vector2f &a, const sf::Vector2f &b) {
return a.x * b.x + a.y * b.y;
}
float distance(const sf::Vector2f &a, const sf::Vector2f &b) {
sf::Vector2f diff = a - b;
return hypotf(diff.x, diff.y);
}
sf::Vector2f rotateToShape(const sf::Vector2f &v, const sf::Transformable &t) {
sf::Vector2f diffRotated =
t.getPosition() - t.getTransform().transformPoint(t.getOrigin() + v);
return diffRotated;
}
float vectorRotation(sf::Vector2f &v) {
if (v.y >= 0 and v.x >= 0) {
return rad2deg(atan(v.x / v.y));
} // quadrant 1
else if (v.y < 0 and v.x >= 0) {
return 180 - rad2deg(atan(v.x / -v.y));
} // quadrant 2
else if (v.y < 0 and v.x < 0) {
return 180 + rad2deg(atan(-v.x / -v.y));
} // quadrant 3
else {
return 360 - rad2deg(atan(-v.x / v.y));
} // quadrant 4
}
#endif