00001 #region --- License ---
00002
00003
00004
00005
00006
00007
00008
00009 #endregion
00010
00011 using System;
00012 using System.Collections.Generic;
00013 using System.Text;
00014
00015 namespace OpenTK
00016 {
00020 public static class MathHelper
00021 {
00022 #region Fields
00023
00027 public const float Pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930382f;
00028
00032 public const float PiOver2 = Pi / 2;
00033
00037 public const float PiOver3 = Pi / 3;
00038
00042 public const float PiOver4 = Pi / 4;
00043
00047 public const float PiOver6 = Pi / 6;
00048
00052 public const float TwoPi = 2 * Pi;
00053
00057 public const float ThreePiOver2 = 3 * Pi / 2;
00058
00062 public const float E = 2.71828182845904523536f;
00063
00067 public const float Log10E = 0.434294482f;
00068
00072 public const float Log2E = 1.442695041f;
00073
00074 #endregion
00075
00076 #region Public Members
00077
00078 #region NextPowerOfTwo
00079
00085 public static long NextPowerOfTwo(long n)
00086 {
00087 if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
00088 return (long)System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
00089 }
00090
00096 public static int NextPowerOfTwo(int n)
00097 {
00098 if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
00099 return (int)System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
00100 }
00101
00107 public static float NextPowerOfTwo(float n)
00108 {
00109 if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
00110 return (float)System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
00111 }
00112
00118 public static double NextPowerOfTwo(double n)
00119 {
00120 if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
00121 return System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
00122 }
00123
00124 #endregion
00125
00126 #region Factorial
00127
00132 public static long Factorial(int n)
00133 {
00134 long result = 1;
00135
00136 for (; n > 1; n--)
00137 result *= n;
00138
00139 return result;
00140 }
00141
00142 #endregion
00143
00144 #region BinomialCoefficient
00145
00152 public static long BinomialCoefficient(int n, int k)
00153 {
00154 return Factorial(n) / (Factorial(k) * Factorial(n - k));
00155 }
00156
00157 #endregion
00158
00159 #region InverseSqrtFast
00160
00172 public static float InverseSqrtFast(float x)
00173 {
00174 unsafe
00175 {
00176 float xhalf = 0.5f * x;
00177 int i = *(int*)&x;
00178 i = 0x5f375a86 - (i >> 1);
00179 x = *(float*)&i;
00180 x = x * (1.5f - xhalf * x * x);
00181 return x;
00182 }
00183 }
00184
00196 public static double InverseSqrtFast(double x)
00197 {
00198 return InverseSqrtFast((float)x);
00199
00200 #if false
00201 unsafe
00202 {
00203 double xhalf = 0.5f * x;
00204 int i = *(int*)&x;
00205 i = 0x5f375a86 - (i >> 1);
00206 x = *(float*)&i;
00207 x = x * (1.5f - xhalf * x * x);
00208 return x;
00209 }
00210 #endif
00211 }
00212
00213 #endregion
00214
00215 #region DegreesToRadians
00216
00222 public static float DegreesToRadians(float degrees)
00223 {
00224 const float degToRad = (float)System.Math.PI / 180.0f;
00225 return degrees * degToRad;
00226 }
00227
00233 public static float RadiansToDegrees(float radians)
00234 {
00235 const float radToDeg = 180.0f / (float)System.Math.PI;
00236 return radians * radToDeg;
00237 }
00238
00239 #endregion
00240
00241 #region Swap
00242
00248 public static void Swap(ref double a, ref double b)
00249 {
00250 double temp = a;
00251 a = b;
00252 b = temp;
00253 }
00254
00260 public static void Swap(ref float a, ref float b)
00261 {
00262 float temp = a;
00263 a = b;
00264 b = temp;
00265 }
00266
00267 #endregion
00268
00269 #endregion
00270 }
00271 }