RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
mathhelper.hpp
Go to the documentation of this file.
1 //--------------------------------------------------------------- @License begins
2 // RioEngine: The late night Coke -without whores- debugging sessions
3 // 2012-2015 Leopoldo Lomas Flores. Torreon, Coahuila. MEXICO
4 // leopoldolomas [at] gmail
5 // www.rioengine.com
6 // www.leopoldolomas.info
7 // "You have a problem, you face it like a man."
8 //
9 // This is free and unencumbered software released into the public domain.
10 //
11 // Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
12 // software, either in source code form or as a compiled binary, for any purpose,
13 // commercial or non-commercial, and by any means.
14 //
15 // In jurisdictions that recognize copyright laws, the author or authors of this
16 // software dedicate any and all copyright interest in the software to the public
17 // domain. We make this dedication for the benefit of the public at large and to
18 // the detriment of our heirs and successors. We intend this dedication to be
19 // an overt act of relinquishment in perpetuity of all present and future
20 // rights to this software under copyright law.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS
24 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //--------------------------------------------------------------- @License ends
29 
30 #ifndef RIOENGINE_MISC_MATHHELPER_H_
31 #define RIOENGINE_MISC_MATHHELPER_H_
32 
33 #include <math.h>
34 
35 class MathHelper {
36 public:
37  static int clampi(int value, int min, int max) {
38  return clamp<int>(value, min, max);
39  }
40 
41  //-----------------------------------------------------------------------------
42 
43  static float clampf(float value, float min, float max) {
44  return clamp<float>(value, min, max);
45  }
46 
47  //-----------------------------------------------------------------------------
48 
49  static float roundCeil(float value, int decimal_places) {
50  int factor = getFactorFromDecimalPlaces(decimal_places);
51  float val = ceilf(value * factor) / factor;
52 
53  if (val == -0.0f) { // Wat?
54  val = 0.0f;
55  }
56 
57  return val;
58  }
59 
60  //-----------------------------------------------------------------------------
61 
62  static float roundNearest(float value, int decimal_places) {
63  int factor = getFactorFromDecimalPlaces(decimal_places);
64  return floorf(value * (float)factor + 0.5f) / (float)factor;
65  }
66 
67  //-----------------------------------------------------------------------------
68 
69  static float roundFloor(float value, int decimal_places) {
70  int factor = getFactorFromDecimalPlaces(decimal_places);
71  return floorf(value * (float)factor) / (float)factor;
72  }
73 
74 private:
75  template<class T>
76  static T clamp(T _value, T min, T max) {
77  T value = _value;
78 
79  if (value < min) {
80  value = min;
81  } else if (value > max) {
82  value = max;
83  }
84 
85  return value;
86  }
87 
88  //-----------------------------------------------------------------------------
89 
90  static int getFactorFromDecimalPlaces(int decimal_places) {
91  int factor = 1;
92 
93  for (int i=0; i < decimal_places; i++) {
94  factor *= 10;
95  }
96 
97  return factor;
98  }
99 };
100 
101 #endif // RIOENGINE_MISC_MATHHELPER_H_
static float roundCeil(float value, int decimal_places)
Definition: mathhelper.hpp:49
static float clampf(float value, float min, float max)
Definition: mathhelper.hpp:43
static float roundNearest(float value, int decimal_places)
Definition: mathhelper.hpp:62
static int clampi(int value, int min, int max)
Definition: mathhelper.hpp:37
static float roundFloor(float value, int decimal_places)
Definition: mathhelper.hpp:69