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 [Obsolete("Use OpenTK.MathHelper instead.")]
00021 public static class Functions
00022 {
00023 #region NextPowerOfTwo
00024
00030 public static long NextPowerOfTwo(long n)
00031 {
00032 if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
00033 return (long)System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
00034 }
00035
00041 public static int NextPowerOfTwo(int n)
00042 {
00043 if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
00044 return (int)System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
00045 }
00046
00052 public static float NextPowerOfTwo(float n)
00053 {
00054 if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
00055 return (float)System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
00056 }
00057
00063 public static double NextPowerOfTwo(double n)
00064 {
00065 if (n < 0) throw new ArgumentOutOfRangeException("n", "Must be positive.");
00066 return System.Math.Pow(2, System.Math.Ceiling(System.Math.Log((double)n, 2)));
00067 }
00068
00069 #endregion
00070
00071 #region Factorial
00072
00077 public static long Factorial(int n)
00078 {
00079 long result = 1;
00080
00081 for (; n > 1; n--)
00082 result *= n;
00083
00084 return result;
00085 }
00086
00087 #endregion
00088
00089 #region BinomialCoefficient
00090
00097 public static long BinomialCoefficient(int n, int k)
00098 {
00099 return Factorial(n) / (Factorial(k) * Factorial(n - k));
00100 }
00101
00102 #endregion
00103
00104 #region InverseSqrtFast
00105
00117 public static float InverseSqrtFast(float x)
00118 {
00119 unsafe
00120 {
00121 float xhalf = 0.5f * x;
00122 int i = *(int*)&x;
00123 i = 0x5f375a86 - (i >> 1);
00124 x = *(float*)&i;
00125 x = x * (1.5f - xhalf * x * x);
00126 return x;
00127 }
00128 }
00129
00141 public static double InverseSqrtFast(double x)
00142 {
00143 return InverseSqrtFast((float)x);
00144
00145 #if false
00146 unsafe
00147 {
00148 double xhalf = 0.5f * x;
00149 int i = *(int*)&x;
00150 i = 0x5f375a86 - (i >> 1);
00151 x = *(float*)&i;
00152 x = x * (1.5f - xhalf * x * x);
00153 return x;
00154 }
00155 #endif
00156 }
00157
00158 #endregion
00159
00160 #region DegreesToRadians
00161
00167 public static float DegreesToRadians(float degrees)
00168 {
00169 const float degToRad = (float)System.Math.PI / 180.0f;
00170 return degrees * degToRad;
00171 }
00172
00178 public static float RadiansToDegrees(float radians)
00179 {
00180 const float radToDeg = 180.0f / (float)System.Math.PI;
00181 return radians * radToDeg;
00182 }
00183
00184 #endregion
00185
00186 #region Mathematical constants
00187
00191 public static readonly float PIF = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930382f;
00192
00196 public static readonly float RTODF = 180.0f / PIF;
00197
00201 public static readonly float DTORF = PIF / 180.0f;
00202
00206 public static readonly double PI = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930382d;
00207
00211 public static readonly double RTOD = 180.0d / PIF;
00212
00216 public static readonly double DTOR = PIF / 180.0d;
00217
00218 #endregion
00219
00220 #region Swap
00221
00227 public static void Swap(ref double a, ref double b)
00228 {
00229 double temp = a;
00230 a = b;
00231 b = temp;
00232 }
00233
00239 public static void Swap(ref float a, ref float b)
00240 {
00241 float temp = a;
00242 a = b;
00243 b = temp;
00244 }
00245
00246 #endregion
00247 }
00248
00249 #if false
00250 public static partial class Math
00251 {
00252 #region --- Vectors ---
00253
00254 #region --- Addition ---
00255
00261 public static Vector2 Add(Vector2 left, Vector2 right)
00262 {
00263 return new Vector2(left).Add(right);
00264 }
00265
00271 public static Vector3 Add(Vector2 left, Vector3 right)
00272 {
00273 return new Vector3(left).Add(right);
00274 }
00275
00281 public static Vector4 Add(Vector2 left, Vector4 right)
00282 {
00283 return new Vector4(left).Add(right);
00284 }
00285
00291 public static Vector3 Add(Vector3 left, Vector2 right)
00292 {
00293 return new Vector3(left).Add(right);
00294 }
00295
00301 public static Vector3 Add(Vector3 left, Vector3 right)
00302 {
00303 return new Vector3(left).Add(right);
00304 }
00305
00311 public static Vector4 Add(Vector3 left, Vector4 right)
00312 {
00313 return new Vector4(left).Add(right);
00314 }
00315
00321 public static Vector4 Add(Vector4 left, Vector2 right)
00322 {
00323 return new Vector4(left).Add(right);
00324 }
00325
00331 public static Vector4 Add(Vector4 left, Vector3 right)
00332 {
00333 return new Vector4(left).Add(right);
00334 }
00335
00341 public static Vector4 Add(Vector4 left, Vector4 right)
00342 {
00343 return new Vector4(left).Add(right);
00344 }
00345
00346 #endregion
00347
00348 #region --- Subtraction ---
00349
00350
00351
00352 #endregion
00353
00354 #region --- Cross ---
00355
00361 public static Vector3 Cross(Vector3 left, Vector3 right)
00362 {
00363 return new Vector3(left).Cross(right);
00364 }
00365
00366 #endregion
00367
00368 #endregion
00369 }
00370 #endif
00371 }