room101/MATH.HPP

82 lines
2 KiB
Text
Raw Normal View History

#ifndef __MATH_HPP
#define __MATH_HPP
#include <math.h>
#define EPSILON 0.01f
/*
** Floating point compare macros
*/
inline int
EpsilonLessThan(const float a, const float b)
{
return (b - a) > EPSILON;
}
inline int
EpsilonGreaterThan(const float a, const float b)
{
return (a - b) > EPSILON;
}
inline int
EpsilonEqual(const float a, const float b)
{
return !EpsilonLessThan(a, b) && !EpsilonGreaterThan(a, b);
}
#define PI 3.141592f /* the venerable pi */
#define PITIMES2 6.283185f /* 2 * pi */
#define PIOVER2 1.570796f /* pi / 2 */
#define E 2.718282f /* the venerable e */
#define SQRT2 1.414214f /* sqrt(2) */
#define SQRT3 1.732051f /* sqrt(3) */
#define GOLDEN 1.618034f /* the golden ratio */
#define DTOR 0.017453f /* convert degrees to radians */
#define RTOD 57.29578f /* convert radians to degrees */
template<class T> const T& Sqr(const T& a) {return a * a;}
template<class T> const T& Abs(const T& a) {return a < 0 ? -a : a;}
template<class T> const T& Min(const T& a, const T& b) {return a < b ? a : b;}
template<class T> const T& Max(const T& a, const T& b) {return a > b ? a : b;}
template<class T> const T& Clamp(const T& a, const T& l, const T& h) {return Max(Min(a,h),l);}
#define SWAP(A,B) {(A) ^= (B); (B) ^= (A); (A) ^= (B);}
inline float
Interpolate(const float a, const float l, const float h)
{
return l + (h - l) * a;
}
#define FLOAT_TO_FIX(A) ((A) * (1 << 16))
#define FIX_TO_FLOAT(A) ((A) / (1 << 16))
#define INT_TO_FIX(A) ((int)(A) << 16)
#define FIX_TO_INT(A) ((int)(A) >> 16)
//#define FIX_TO_INT(A) ((short*)&((int)(A)))[1]
#define BIG_NUM ((double)1.5 * (double)(1 << 26) * (double)(1 << 26))
inline int
Round(double a)
{
a += BIG_NUM;
return *(int*)(&a);
}
inline int
Trunc(double a)
{
a += BIG_NUM - 0.5;
return *(int*)(&a);
}
#endif