GigaPasjans
Loading...
Searching...
No Matches
Math.h
1// Wykonane przez Piotra ChudziƄskiego w dniu 11.04.2025
2#pragma once
3
4#include "foundation/DataTypes.h"
5#include <cmath>
6
7template<typename T>
8concept Arithmetic = std::is_arithmetic_v<T>;
9
13class Math
14{
15public:
19 const static float PI;
20
24 const static float Epsilon;
25
29 template<Arithmetic T>
30 constexpr static T Radians(T value)
31 {
32 return value * (PI / 180.0f);
33 }
34
38 template<Arithmetic T>
39 constexpr static T HalfRadians(T value)
40 {
41 return value * ((PI / 180.0f) * 0.5f);
42 }
43
47 template<Arithmetic T>
48 constexpr static int32 Ceil(T value)
49 {
50 int32 intPart = static_cast<int32>(value);
51 if (value == static_cast<T>(intPart))
52 {
53 return intPart;
54 }
55
56 return value > 0 ? intPart + 1 : intPart;
57 }
58
62 template<Arithmetic T>
63 constexpr static T Tan(T value)
64 {
65 return tan(value);
66 }
67
71 template<Arithmetic T>
72 constexpr static T Sin(T value)
73 {
74 return sin(value);
75 }
76
80 template<Arithmetic T>
81 constexpr static T Cos(T value)
82 {
83 return cos(value);
84 }
85
89 template<Arithmetic T>
90 constexpr static T Abs(T value)
91 {
92 return abs(value);
93 }
94
98 template<Arithmetic T>
99 constexpr static T Inverse(T value)
100 {
101 if (value > 0)
102 {
103 return -value;
104 }
105 else if (value < 0)
106 {
107 return Abs(value);
108 }
109 else
110 {
111 return value;
112 }
113 }
114
118 template<Arithmetic T>
119 constexpr static T Sqrt(T value)
120 {
121 return sqrt(value);
122 }
123
127 template<Arithmetic T>
128 constexpr static T Lerp(T from, T to, T amount)
129 {
130 return from + (to - from) * amount;
131 }
132
136 template<Arithmetic T>
137 constexpr static T Atan(T x)
138 {
139 T result = 0.0;
140 T term = x;
141 T x_squared = x * x;
142 int32 n = 1;
143
144 for (int32 i = 0; i < 100; ++i)
145 {
146 if (i % 2 == 0)
147 {
148 result += term / n;
149 }
150 else
151 {
152 result -= term / n;
153 }
154 term *= x_squared;
155 n += 2;
156 }
157
158 return result;
159 };
160
164 template<Arithmetic T>
165 constexpr static T Factorical(int32 n)
166 {
167 if (n == 0 || n == 1)
168 {
169 return 1;
170 }
171 T result = 1;
172 for (int32 i = 2; i <= n; ++i)
173 {
174 result *= i;
175 }
176 return result;
177 };
178
182 template<Arithmetic T>
183 constexpr static T Asin(T x)
184 {
185 if (x < -1.0 || x > 1.0)
186 return 0;
187
188 T result = x;
189 T term = x;
190 int32 n = 1;
191
192 for (int32 i = 1; i < 100; ++i)
193 {
194 term *= x * x * (2 * n - 1) * (2 * n - 1) / (2 * n * (2 * n + 1));
195 result += term;
196 ++n;
197 }
198
199 return result;
200 };
201
205 template<Arithmetic T>
206 constexpr static T Copysign(T magnitude, T sign)
207 {
208 return magnitude * (signbit(sign) ? -1.0 : 1.0);
209 };
210
214 template<Arithmetic T>
215 constexpr static T Atan2(T x, T y)
216 {
217 if (x > 0)
218 {
219 return atan(y / x);
220 }
221 if (x < 0 && y >= 0)
222 {
223 return atan(y / x) + PI;
224 }
225 if (x < 0 && y < 0)
226 {
227 return atan(y / x) - PI;
228 }
229 if (x == 0 && y > 0)
230 {
231 return PI / 2;
232 }
233 if (x == 0 && y < 0)
234 {
235 return -PI / 2;
236 }
237
238 return 0;
239 };
240
244 template<Arithmetic T>
245 constexpr static T Clamp(T value, T min, T max)
246 {
247 const T& t = value < min ? min : value;
248 return t > max ? max : t;
249 };
250};
A utility which contains essential mathematical functions and constants.
Definition Math.h:14
static constexpr T Sin(T value)
Mathematical function that computes the sine of an angle (in radians).
Definition Math.h:72
static constexpr T Atan(T x)
Mathematical function that computes the arctangent of a value using a Taylor series.
Definition Math.h:137
static constexpr T Copysign(T magnitude, T sign)
Mathematical function that returns a value with the magnitude of the first argument and the sign of t...
Definition Math.h:206
static const float Epsilon
Mathematical constant equal to 1e-6f.
Definition Math.h:24
static constexpr T HalfRadians(T value)
Mathematical function that converts degrees to half-radians.
Definition Math.h:39
static constexpr T Lerp(T from, T to, T amount)
Mathematical function that linearly interpolates between two values based on a weight.
Definition Math.h:128
static constexpr T Atan2(T x, T y)
Mathematical function that computes the arctangent of y/x using signs to determine the correct quadra...
Definition Math.h:215
static constexpr T Abs(T value)
Mathematical function that returns the absolute value of the input.
Definition Math.h:90
static constexpr T Tan(T value)
Mathematical function that computes the tangent of an angle (in radians).
Definition Math.h:63
static constexpr T Asin(T x)
Mathematical function that computes the arcsine of a value using a power series.
Definition Math.h:183
static constexpr T Clamp(T value, T min, T max)
Mathematical function that clamps a value between a minimum and maximum.
Definition Math.h:245
static constexpr T Radians(T value)
Mathematical function that converts degrees to radians.
Definition Math.h:30
static constexpr T Cos(T value)
Mathematical function that computes the cosine of an angle (in radians).
Definition Math.h:81
static constexpr T Sqrt(T value)
Mathematical function that computes the square root of the input.
Definition Math.h:119
static constexpr T Inverse(T value)
Mathematical function that returns the value with its sign inverted.
Definition Math.h:99
static constexpr int32 Ceil(T value)
Mathematical function that returns the smallest integer greater than or equal to the input.
Definition Math.h:48
static constexpr T Factorical(int32 n)
Mathematical function that computes the factorial of an integer.
Definition Math.h:165
static const float PI
Mathematical constant approximately equal to 3.14159, that is ratio of a circle's circumference to it...
Definition Math.h:19
Definition Math.h:8