diff options
Diffstat (limited to 'src/math/func.h')
-rw-r--r-- | src/math/func.h | 156 |
1 files changed, 78 insertions, 78 deletions
diff --git a/src/math/func.h b/src/math/func.h index 212f7c1..8f0e4ab 100644 --- a/src/math/func.h +++ b/src/math/func.h @@ -16,7 +16,7 @@ // * along with this program. If not, see http://www.gnu.org/licenses/. /** @defgroup MathFuncModule math/func.h - Contains common math functions. + Contains common math functions. */ #pragma once @@ -36,146 +36,146 @@ namespace Math //! Compares \a a and \a b within \a tolerance inline bool IsEqual(float a, float b, float tolerance = TOLERANCE) { - return fabs(a - b) < tolerance; + return fabs(a - b) < tolerance; } //! Compares \a a to zero within \a tolerance inline bool IsZero(float a, float tolerance = TOLERANCE) { - return IsEqual(a, 0.0f, tolerance); + return IsEqual(a, 0.0f, tolerance); } //! Minimum inline float Min(float a, float b) { - if ( a <= b ) return a; - else return b; + if ( a <= b ) return a; + else return b; } inline float Min(float a, float b, float c) { - return Min( Min(a, b), c ); + return Min( Min(a, b), c ); } inline float Min(float a, float b, float c, float d) { - return Min( Min(a, b), Min(c, d) ); + return Min( Min(a, b), Min(c, d) ); } inline float Min(float a, float b, float c, float d, float e) { - return Min( Min(a, b), Min(c, d), e ); + return Min( Min(a, b), Min(c, d), e ); } //! Maximum inline float Max(float a, float b) { - if ( a >= b ) return a; - else return b; + if ( a >= b ) return a; + else return b; } inline float Max(float a, float b, float c) { - return Max( Max(a, b), c ); + return Max( Max(a, b), c ); } inline float Max(float a, float b, float c, float d) { - return Max( Max(a, b), Max(c, d) ); + return Max( Max(a, b), Max(c, d) ); } inline float Max(float a, float b, float c, float d, float e) { - return Max( Max(a, b), Max(c, d), e ); + return Max( Max(a, b), Max(c, d), e ); } //! Returns the normalized value (0 .. 1) inline float Norm(float a) { - if ( a < 0.0f ) return 0.0f; - if ( a > 1.0f ) return 1.0f; - return a; + if ( a < 0.0f ) return 0.0f; + if ( a > 1.0f ) return 1.0f; + return a; } //! Swaps two integers inline void Swap(int &a, int &b) { - int c = a; - a = b; - b = c; + int c = a; + a = b; + b = c; } //! Swaps two real numbers inline void Swap(float &a, float &b) { - float c = a; - a = b; - b = c; + float c = a; + a = b; + b = c; } //! Returns the modulo of a floating point number /** Mod(8.1, 4) = 0.1 - Mod(n, 1) = fractional part of n */ + Mod(n, 1) = fractional part of n */ inline float Mod(float a, float m) { - return a - ((int)(a/m))*m; + return a - ((int)(a/m))*m; } //! Returns a random value between 0 and 1. inline float Rand() { - return (float)rand()/RAND_MAX; + return (float)rand()/RAND_MAX; } //! Returns a normalized angle, that is in other words between 0 and 2 * PI inline float NormAngle(float angle) { - angle = Mod(angle, PI*2.0f); - if ( angle < 0.0f ) - return PI*2.0f + angle; + angle = Mod(angle, PI*2.0f); + if ( angle < 0.0f ) + return PI*2.0f + angle; - return angle; + return angle; } //! Test if a angle is between two terminals inline bool TestAngle(float angle, float min, float max) { - angle = NormAngle(angle); - min = NormAngle(min); - max = NormAngle(max); + angle = NormAngle(angle); + min = NormAngle(min); + max = NormAngle(max); - if ( min > max ) - return ( angle <= max || angle >= min ); + if ( min > max ) + return ( angle <= max || angle >= min ); - return ( angle >= min && angle <= max ); + return ( angle >= min && angle <= max ); } //! Calculates a value (radians) proportional between a and b (degrees) inline float PropAngle(int a, int b, float p) { - float aa = (float)a * DEG_TO_RAD; - float bb = (float)b * DEG_TO_RAD; + float aa = (float)a * DEG_TO_RAD; + float bb = (float)b * DEG_TO_RAD; - return aa+p*(bb-aa); + return aa+p*(bb-aa); } //! Calculates the angle to rotate the angle \a a to the angle \a g /** A positive angle is counterclockwise (CCW). */ inline float Direction(float a, float g) { - a = NormAngle(a); - g = NormAngle(g); + a = NormAngle(a); + g = NormAngle(g); - if ( a < g ) - { - if ( a+PI*2.0f-g < g-a ) a += PI*2.0f; - } - else - { - if ( g+PI*2.0f-a < a-g ) g += PI*2.0f; - } + if ( a < g ) + { + if ( a+PI*2.0f-g < g-a ) a += PI*2.0f; + } + else + { + if ( g+PI*2.0f-a < a-g ) g += PI*2.0f; + } - return g-a; + return g-a; } //! Managing the dead zone of a joystick. @@ -187,33 +187,33 @@ inline float Direction(float a, float g) out: -1 0 0 1\endverbatim */ inline float Neutral(float value, float dead) { - if ( fabs(value) <= dead ) - { - return 0.0f; - } - else - { - if ( value > 0.0f ) return (value-dead)/(1.0f-dead); - else return (value+dead)/(1.0f-dead); - } + if ( fabs(value) <= dead ) + { + return 0.0f; + } + else + { + if ( value > 0.0f ) return (value-dead)/(1.0f-dead); + else return (value+dead)/(1.0f-dead); + } } //! Gently advances a desired value from its current value /** Over time, the progression is more rapid. */ inline float Smooth(float actual, float hope, float time) { - float future = actual + (hope-actual)*time; + float future = actual + (hope-actual)*time; - if ( hope > actual ) - { - if ( future > hope ) future = hope; - } - if ( hope < actual ) - { - if ( future < hope ) future = hope; - } + if ( hope > actual ) + { + if ( future > hope ) future = hope; + } + if ( hope < actual ) + { + if ( future < hope ) future = hope; + } - return future; + return future; } //! Bounces any movement @@ -230,16 +230,16 @@ inline float Smooth(float actual, float hope, float time) |<---->|middle\endverbatim */ inline float Bounce(float progress, float middle = 0.3f, float bounce = 0.4f) { - if ( progress < middle ) - { - progress = progress/middle; // 0..1 - return 0.5f+sinf(progress*PI-PI/2.0f)/2.0f; - } - else - { - progress = (progress-middle)/(1.0f-middle); // 0..1 - return (1.0f-bounce/2.0f)+sinf((0.5f+progress*2.0f)*PI)*(bounce/2.0f); - } + if ( progress < middle ) + { + progress = progress/middle; // 0..1 + return 0.5f+sinf(progress*PI-PI/2.0f)/2.0f; + } + else + { + progress = (progress-middle)/(1.0f-middle); // 0..1 + return (1.0f-bounce/2.0f)+sinf((0.5f+progress*2.0f)*PI)*(bounce/2.0f); + } } /* @} */ // end of group |