SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Math.hpp
Go to the documentation of this file.
1#ifndef _MATH_H_
2#define _MATH_H_
3
4#include <cmath>
5#include <Math/Vector.hpp>
6
7#define PI 3.14159
8#define D2R PI / 180.0
9
10namespace Sleak {
11namespace Math {
12
13/// Clamps value into [min, max].
14/// Returns by value: min and max are parameters, so returning a reference
15/// would hand back a dangling one whenever clamping engages.
16template <typename T>
17constexpr T Clamp(const T& value, T min, T max) {
18 return (value < min) ? min : (value > max) ? max : value;
19}
20
21/// Clamps value into [range.x, range.y].
22template <typename T>
23constexpr T Clamp(const T& value, Vector2D range) {
24 return (value < range.GetX()) ? static_cast<T>(range.GetX())
25 : (value > range.GetY()) ? static_cast<T>(range.GetY())
26 : value;
27}
28
29template <typename T>
30constexpr const T& Min(const T& value1, const T& value2) {
31 return (value1 < value2) ? value1 : value2;
32}
33
34template <typename T>
35constexpr const T& Max(const T& value1, const T& value2) {
36 return (value1 > value2) ? value1 : value2;
37}
38
39/// Linear interpolation from start to end; t is clamped to [0,1].
40template <typename T>
41constexpr T Lerp(const T& start, const T& end, float t) {
42 return start + (end - start) * Clamp(t, 0.0f, 1.0f);
43}
44
45} // namespace Math
46} // namespace Sleak
47
48#endif // _MATH_H_
constexpr float GetY() const
Definition Vector.hpp:218
constexpr float GetX() const
Definition Vector.hpp:217
constexpr const T & Min(const T &value1, const T &value2)
Definition Math.hpp:30
constexpr const T & Max(const T &value1, const T &value2)
Definition Math.hpp:35
constexpr T Lerp(const T &start, const T &end, float t)
Linear interpolation from start to end; t is clamped to [0,1].
Definition Math.hpp:41
constexpr T Clamp(const T &value, T min, T max)
Definition Math.hpp:17
Root namespace for everything the engine exposes.
Definition Camera.hpp:10