1 #include "MathManager.h"
2 
3 #include <cstdlib>
4 #include <ctime>
5 #include <cmath>
6 
7 namespace jvgs
8 {
9     namespace math
10     {
MathManager()11         MathManager::MathManager()
12         {
13             newRandomSeed();
14         }
15 
~MathManager()16         MathManager::~MathManager()
17         {
18         }
19 
getInstance()20         MathManager *MathManager::getInstance()
21         {
22             static MathManager instance;
23             return &instance;
24         }
25 
nearestPowerOfTwo(const int & input) const26         int MathManager::nearestPowerOfTwo(const int &input) const
27         {
28             int value = 1;
29             while(value<input)
30                 value <<= 1;
31             return value;
32         }
33 
newRandomSeed() const34         void MathManager::newRandomSeed() const
35         {
36             srand((unsigned int) time(0));
37         }
38 
randInt(const int & maximum) const39         int MathManager::randInt(const int &maximum) const
40         {
41             return rand()%maximum;
42         }
43 
randInt(const int & minimum,const int & maximum) const44         int MathManager::randInt(const int &minimum, const int &maximum) const
45         {
46             if(minimum == maximum)
47                 return minimum;
48             else
49                 return minimum + rand()%(maximum-minimum);
50         }
51 
randFloat() const52         float MathManager::randFloat() const
53         {
54             return rand()/((float)(RAND_MAX)+1.0f);
55         }
56 
randFloat(const float & maximum) const57         float MathManager::randFloat(const float &maximum) const
58         {
59             return randFloat() * maximum;
60         }
61 
randFloat(const float & minimum,const float & maximum) const62         float MathManager::randFloat(const float &minimum,
63                                      const float &maximum) const
64         {
65             return minimum + randFloat()*(maximum-minimum);
66         }
67 
randBool(const float & chance) const68         bool MathManager::randBool(const float &chance) const
69         {
70             return randFloat() < chance;
71         }
72 
getLowestPositiveRoot(float a,float b,float c,float treshold,float * root) const73         bool MathManager::getLowestPositiveRoot(float a, float b, float c,
74                 float treshold, float *root) const
75         {
76             /* Calculate determinant. */
77             float d = b * b - 4.0f * a * c;
78 
79             /* No solution. */
80             if(d < 0.0f)
81                 return false;
82 
83             /* Calculate the two roots. */
84             float sqrtD = sqrt(d);
85             float x1 = (-b - sqrtD) / (2.0f * a);
86             float x2 = (-b + sqrtD) / (2.0f * a);
87 
88             /* Sort solutions. */
89             if(x1 > x2) {
90                 float tmp = x1;
91                 x1 = x2;
92                 x2 = tmp;
93             }
94 
95             /* x1 is a solution. */
96             if(x1 >= 0 && x1 <= treshold) {
97                 *root = x1;
98                 return true;
99             }
100 
101             /* When x1 < 0 */
102             if(x2 >= 0 && x2 <= treshold) {
103                 *root = x2;
104                 return true;
105             }
106 
107             /* No solution found. */
108             return false;
109         }
110 
clamp(float a,float lower,float upper) const111         float MathManager::clamp(float a, float lower, float upper) const
112         {
113             if(a < lower)
114                 return lower;
115             if(a > upper)
116                 return upper;
117             return a;
118         }
119 
toRadians(float degrees) const120         float MathManager::toRadians(float degrees) const
121         {
122             return degrees * M_PI / 180.0f;
123         }
124     };
125 };
126