-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathUtilities.h
94 lines (74 loc) · 1.83 KB
/
MathUtilities.h
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
#ifndef MATH_UTILITIES_H
#define MATH_UTILITIES_H
#include <cmath>
#include <cstdint>
#define PI 3.14159f
typedef float real;
typedef int32_t int32;
typedef bool boolean;
namespace math
{
int32 ToNearestInt(real x);
template <typename T>
T Max(T const &a, T const &b)
{
return a > b ? a: b;
}
template <typename T>
T Max(T const &a, T const &b, T const &c)
{
return Max(Max(a, b), c);
}
template <typename T>
T Max(T const &a, T const &b, T const &c, T const &d)
{
return Max(Max(a, b, c), d);
}
template <typename T>
T Min(T const &a, T const &b)
{
return a < b ? a: b;
}
template <typename T>
T Min(T const &a, T const &b, T const &c)
{
return Min(Min(a, b), c);
}
template <typename T>
T Min(T const &a, T const &b, T const &c, T const &d)
{
return Min(Min(a, b, c), d);
}
template <typename T>
T Clamp(T x, T lo, T hi)
{
if(hi < lo) {
T tmp = lo;
lo = hi;
hi = tmp;
}
if(x < lo) x = lo;
if(x > hi) x = hi;
return x;
}
/*
// Precise method
http://en.wikipedia.org/wiki/Linear_int32erpolation
lo + t * (hi - lo)
lo + t * hi - t * lo
(lo - t * lo) + t * hi
(1 - t) * lo + t * hi
*/
template <typename T>
T Lerp(T const &lo, T const &hi, real t)
{
return (1 - t) * lo + t * hi;
}
real ToDegrees(real radians);
real ToRadians(real degrees);
real CalcAngularDistanceInRadians(real angle1InRadians, real angle2InRadians);
real CalcAngularDistanceInDegrees(real angle1InDegrees, real angle2InDegrees);
boolean IsPowerOf2(int32 x);
boolean IsNear(real lhs, real rhs, real epsilon = 0.001);
}
#endif