-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathHelpers.cpp
More file actions
137 lines (109 loc) · 5.42 KB
/
Copy pathMathHelpers.cpp
File metadata and controls
137 lines (109 loc) · 5.42 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "MathHelpers.h"
#include <cmath>
#include <limits>
#include <vector>
namespace mat {
constexpr float TO_DEGREES{ 57.2957795f };
constexpr float TO_RADIANS{ 0.0174533f };
constexpr float PI{ 3.1415926f };
////////////////////////////////////////////////////////////
float lerp(const float& t_a, const float& t_b, const float& t_f, bool t_clamp) {
if (t_clamp) {
if (t_f <= 0) { return t_a; }
if (t_f >= 1) { return t_b; }
}
return t_a + t_f * (t_b - t_a);
}
////////////////////////////////////////////////////////////
float midPoint(const float& t_a, const float& t_b) {
return lerp(t_a, t_b, 0.5f);
}
////////////////////////////////////////////////////////////
float toDegrees(const float& t_rad) { return t_rad * TO_DEGREES; }
////////////////////////////////////////////////////////////
float toRadians(const float& t_deg) { return t_deg * TO_RADIANS; }
////////////////////////////////////////////////////////////
float normalizeAngle(const float& t_deg) { // [0 360)
if (t_deg >= 0 && t_deg < 360) { return t_deg; }
float angle{ std::fmod(t_deg, 360.f) };
return (angle >= 0 ? angle : angle + 360.f);
}
////////////////////////////////////////////////////////////
float vec2d_magnitude(const float& t_i, const float& t_j) { return std::sqrt(t_i * t_i + t_j * t_j); }
////////////////////////////////////////////////////////////
float vec2d_magnitude(const sf::Vector2f& t_vec) { return vec2d_magnitude(t_vec.x, t_vec.y); }
////////////////////////////////////////////////////////////
template <typename T>
float vec2d_magnitude(const sf::Vector2<T> t_vec) { return std::sqrtf(t_vec.x * t_vec.x + t_vec.y * t_vec.y); }
////////////////////////////////////////////////////////////
void vec2d_unitary(const float& t_i, const float& t_j, float& t_out_i, float& t_out_j) {
float mag{ vec2d_magnitude(t_i, t_j) };
t_out_i = t_i / mag;
t_out_j = t_j / mag;
}
////////////////////////////////////////////////////////////
float vec2d_dotProduct(const float& t_i1, const float& t_j1, const float& t_i2, const float& t_j2) {
return t_i1 * t_i2 + t_j1 * t_j2;
}
////////////////////////////////////////////////////////////
float vec2d_dotProduct(const sf::Vector2f& t_vec1, const sf::Vector2f& t_vec2) {
return vec2d_dotProduct(t_vec1.x, t_vec1.y, t_vec2.x, t_vec2.y);
}
////////////////////////////////////////////////////////////
float vec2d_crossProduct(const float& t_i1, const float& t_j1, const float& t_i2, const float& t_j2) {
return t_i1 * t_j2 - t_j1 * t_i2;
}
////////////////////////////////////////////////////////////
float vec2d_crossProduct(const sf::Vector2f& t_vec1, const sf::Vector2f& t_vec2) {
return vec2d_crossProduct(t_vec1.x, t_vec1.y, t_vec2.x, t_vec2.y);
}
////////////////////////////////////////////////////////////
float vec2d_angleBetweenVectors(const float& t_i1, const float& t_j1, const float& t_i2, const float& t_j2) {
return std::acos(vec2d_dotProduct(t_i1, t_j1, t_i2, t_j2) / (vec2d_magnitude(t_i1, t_j1) * vec2d_magnitude(t_i2, t_j2))) * TO_DEGREES;
}
////////////////////////////////////////////////////////////
float vec2d_angleBetweenVectors(const sf::Vector2f& t_vec1, const sf::Vector2f& t_vec2) {
return vec2d_angleBetweenVectors(t_vec1.x, t_vec1.y, t_vec2.x, t_vec2.y);
}
////////////////////////////////////////////////////////////
sf::Vector2f vec2d_unitary(const sf::Vector2f& t_vec) {
float mag{ vec2d_magnitude(t_vec.x, t_vec.y) };
return { t_vec.x / mag ,t_vec.y / mag };
}
////////////////////////////////////////////////////////////
void vec2d_lerp(const float& t_i1, const float& t_j1, const float& t_i2, const float& t_j2, const float& t_f, float& t_out_i, float& t_out_j) {
t_out_i = lerp(t_i1, t_i2, t_f);
t_out_j = lerp(t_j1, t_j2, t_f);
}
////////////////////////////////////////////////////////////
void vec2d_midPoint(const float& t_i1, const float& t_j1, const float& t_i2, const float& t_j2, float& t_out_i, float& t_out_j) {
t_out_i = lerp(t_i1, t_i2, 0.5f);
t_out_j = lerp(t_j1, t_j2, 0.5f);
}
////////////////////////////////////////////////////////////
sf::Vector2f vec2d_lerp(const sf::Vector2f& t_vec1, const sf::Vector2f& t_vec2, const float& t_f) {
return { lerp(t_vec1.x, t_vec2.x, t_f) ,lerp(t_vec1.y, t_vec2.y, t_f) };
}
////////////////////////////////////////////////////////////
sf::Vector2f vec2d_midPoint(const sf::Vector2f& t_vec1, const sf::Vector2f& t_vec2) {
return { lerp(t_vec1.x, t_vec2.x, 0.5f) ,lerp(t_vec1.y, t_vec2.y, 0.5f) };
}
////////////////////////////////////////////////////////////
void to_polar(const float& t_i, const float& t_j, float& t_out_radius, float& t_out_degrees) {
t_out_radius = std::sqrtf(t_i * t_i + t_j * t_j);
t_out_degrees = std::atanf(t_i / t_j) * TO_DEGREES;
}
////////////////////////////////////////////////////////////
void to_cartesian(const float& t_radius, const float& t_degrees, float& t_out_i, float& t_out_j) {
t_out_i = t_radius * std::cos(t_degrees * TO_RADIANS);
t_out_j = t_radius * std::sin(t_degrees * TO_RADIANS);
}
////////////////////////////////////////////////////////////
float distance(const float& t_x1, const float& t_y1, const float& t_x2, const float& t_y2) {
return std::sqrtf((t_x1 - t_x2) * (t_x1 - t_x2) + (t_y1 - t_y2) * (t_y1 - t_y2));
}
////////////////////////////////////////////////////////////
float distance(const sf::Vector2f& t_pos1, const sf::Vector2f& t_pos2) {
return distance(t_pos1.x, t_pos1.y, t_pos2.x, t_pos2.y);
}
}