Apjjm

ATDD Scripting: Math & Math-related

Jun 6th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.94 KB | None | 0 0
  1. //Begin Math
  2. //+ Constants
  3. const float PI = 3.1415926535f;
  4. const float HPI = PI * 0.5f;
  5. const float PI2 = PI * 2.0f;
  6. //-
  7. //+ Sign Of
  8. int sign(float &in x) { if(x==0.0f) return 0; return (x>0.0f)?1:-1; }
  9. int sign(int &in x)   { if(x==0)    return 0; return (x>0)?1:-1; }
  10. //-
  11. //+ Abs
  12. float abs(float &in x) { return (x>0.0f)?x:-x; }
  13. int abs(int &in x)     { return (x>0)?x:-x; }
  14. //-
  15. //+ Approx
  16. //Determines if two numbers are approximately equal (Differ by no more than epsilon)
  17. bool approx(float &in x, float &in y, float &in epsilon) {
  18. float delta = x-y; return ((delta>0?delta:-delta) <= (epsilon>0?epsilon:-epsilon)); }
  19. //-
  20. //+ Sqrt
  21. //Prereq: Approx
  22. //Returns the squareroot of x
  23. const uint32 _SQRTITERATIONS=16; //Maximum number of iterations for sqrt computation
  24. const float  _SQRTDELTA=0.00001f; //Margin of error allowable if complete before iteration ceiling
  25. float sqrt(float &in x) {
  26.   if(x<=0) return 0; //Early out - not valid input.
  27.   uint32 i = 0; float o = x * 0.5f;
  28.   while( i<_SQRTITERATIONS && !approx(o*o,x,_SQRTDELTA) && o != 0)
  29.     { o = 0.5f * (o + x/o); i++; }
  30.   return o;             }
  31. //-
  32. //+ Max/Min
  33. //Max: Returns largest of two numbers
  34. //Min: Returns smallest of two numbers
  35. float max(float &in x, float &in y) { return x>y?x:y; }
  36. int max(int &in x, int &in y) { return x>y?x:y; }
  37. float min(float &in x, float &in y) { return x<y?x:y; }
  38. int min(int &in x, int &in y) { return x<y?x:y; }
  39. float clamp(float &in x, float &in upper, float &in lower) { return (x<lower?lower:(x<upper?x:upper)); }
  40. int clamp(int &in x, int &in upper, int &in lower) { return (x<lower?lower:(x<upper?x:upper)); }
  41. //-
  42. //+ Factorial
  43. uint32 fact(uint32 &in x) { if(x==0) return 1;
  44.     uint32 y = x; for(uint32 i=x-1; i>0; i--) y*=i; return y; }
  45. int fact(int &in x)       { if(x<=0) return 1;
  46.     int y = x; for(int i=x-1; i>0; i--) y*=i; return y; }
  47. //-
  48. //+ Conversions
  49. //Degrees to radians
  50. float degtorad(float &in x) { return x * 0.0174532925f; }
  51. //Radians to degrees
  52. float radtodeg(float &in x) { return x * 57.2957795f; }
  53. //-
  54. //+ Trig Functions
  55. //+ ..Helper
  56. const float  _TRIGARCTANLIM=128.0f; //Value when x exceeds 90 is returned
  57. const uint32 _TRIGITERATIONS=32; //Maximum number of iterations for sqrt computation
  58. const float  _TRIGDELTA=0.00001f; //Smallest diff for trig vals
  59. //Constrains X : -PI<X<PI
  60. float trigClamp(float &in x) {
  61. if(x> PI || x <-PI)
  62.   { int t = x/PI2; return x-t*PI2; }
  63. else return x;
  64. }
  65. //-
  66. //+ ..Sin/Cos/Tan
  67. //COSINE
  68. float cos(float &in y) {
  69.   float x = trigClamp(y);
  70.   float t,s; t=1.0f; s = 1.0f; uint32 p=0;
  71.   while( p<_TRIGITERATIONS)
  72.    { p++; t = (-t * x * x) / ((2 * p - 1) * (2 * p)); s+=t; }
  73.   return s;
  74. }
  75. //SINE
  76. float sin(float &in y) {
  77.   return cos(y-HPI);
  78. }
  79. //TAN (Tan(90 deg) Returns 0xFFFFFFFF [Use isInf()])
  80. float tan(float &in y) {
  81.   float x = trigClamp(y);
  82.   float c = cos(x);
  83.   if(c> -_TRIGDELTA/2 && c <_TRIGDELTA/2) return float(0xFFFFFFFF); //Return #+INF                                        
  84.   return cos(x-HPI)/c;
  85. }
  86. //-
  87. //+ ..ARC Sin/Cos/Tan [APPROX]
  88. float arccos(float &in x) {
  89. return trigClamp((-0.69813170079773212f * x * x - 0.87266462599716477f) * x + 1.5707963267948966f); }
  90. float arcsin(float &in x) { return trigClamp(HPI-arccos(x)); }
  91. float arctan(float &in x) {  if(x> _TRIGARCTANLIM) return HPI;
  92. else if(x <-_TRIGARCTANLIM) return -HPI; else return trigClamp(HPI-arccos(x/sqrt(x*x+1.1f))); }
  93. float arctan2(float &in x, float &in y) {
  94. float c1 = HPI * 0.5f;    float c2 = 3.0f * c1; float abs_y = abs(y); float angle;    
  95. if (x>= 0) { float r = (x - abs_y) / (x + abs_y);    angle = c1 - c1 * r;    }
  96. else { float r = (x + abs_y) / (abs_y - x);    angle = c2 - c1 * r;    }    
  97. return y<0?-angle:angle; }
  98. //Can somebody actually get a good expansion to work here?
  99. //-
  100. //-
  101. //+ Rounding
  102. int floor(float &in x) { return int(x); }
  103. int ceil(float &in x)  { int y = int(x); return (y==x?y:y+1); }
  104. int round(float &in x) { int y = int(x); return (y+0.5f>x)?y:(y+1);}
  105. //-
  106. //End
Advertisement
Add Comment
Please, Sign In to add comment