Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

mathlib.h

Go to the documentation of this file.
00001 // mathlib.h
00002 // 
00003 // Copyright (C) 2001, Chris Laurel <claurel@shatters.net>
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 
00010 #ifndef _MATHLIB_H_
00011 #define _MATHLIB_H_
00012 
00013 #include <cmath>
00014 #include <stdlib.h>
00015 
00016 #define PI 3.14159265358979323846
00017 
00018 template<class T> class Math
00019 {
00020 public:
00021     static inline void sincos(T, T&, T&);
00022     static inline T frand();
00023     static inline T sfrand();
00024     static inline T lerp(T t, T a, T b);
00025     static inline T clamp(T t);
00026 
00027 private:
00028     // This class is static and should not be instantiated
00029     Math() {};
00030 };
00031 
00032 
00033 typedef Math<float> Mathf;
00034 typedef Math<double> Mathd;
00035 
00036 
00037 template<class T> T degToRad(T d)
00038 {
00039     return d / 180 * static_cast<T>(PI);
00040 }
00041 
00042 template<class T> T radToDeg(T r)
00043 {
00044     return r * 180 / static_cast<T>(PI);
00045 }
00046 
00047 template<class T> T abs(T x)
00048 {
00049     return (x < 0) ? -x : x;
00050 }
00051 
00052 template<class T> T square(T x)
00053 {
00054     return x * x;
00055 }
00056 
00057 template<class T> T cube(T x)
00058 {
00059     return x * x * x;
00060 }
00061 
00062 template<class T> T clamp(T x)
00063 {
00064     if (x < 0)
00065         return 0;
00066     else if (x > 1)
00067         return 1;
00068     else
00069         return x;
00070 }
00071 
00072 template<class T> T sign(T x)
00073 {
00074     if (x < 0)
00075         return -1;
00076     else if (x > 0)
00077         return 1;
00078     else
00079         return 0;
00080 }
00081 
00082 // This function is like fmod except that it always returns
00083 // a positive value in the range [ 0, y )
00084 template<class T> T pfmod(T x, T y)
00085 {
00086     int quotient = (int) abs(x / y);
00087     if (x < 0.0)
00088         return x + (quotient + 1) * y;
00089     else
00090         return x - quotient * y;
00091 }
00092 
00093 template<class T> T circleArea(T r)
00094 {
00095     return (T) PI * r * r;
00096 }
00097 
00098 template<class T> T sphereArea(T r)
00099 {
00100     return 4 * (T) PI * r * r;
00101 }
00102 
00103 template<class T> void Math<T>::sincos(T angle, T& s, T& c)
00104 {
00105     s = (T) sin(angle);
00106     c = (T) cos(angle);
00107 }
00108 
00109 
00110 // return a random float in [0, 1]
00111 template<class T> T Math<T>::frand()
00112 {
00113     return (T) (rand() & 0x7fff) / (T) 32767;
00114 }
00115 
00116 
00117 // return a random float in [-1, 1]
00118 template<class T> T Math<T>::sfrand()
00119 {
00120     return (T) (rand() & 0x7fff) / (T) 32767 * 2 - 1;
00121 }
00122 
00123 
00124 template<class T> T Math<T>::lerp(T t, T a, T b)
00125 {
00126     return a + t * (b - a);
00127 }
00128 
00129 
00130 // return t clamped to [0, 1]
00131 template<class T> T Math<T>::clamp(T t)
00132 {
00133     if (t < 0)
00134         return 0;
00135     else if (t > 1)
00136         return 1;
00137     else
00138         return t;
00139 }
00140 
00141 #endif // _MATHLIB_H_

Generated on Sat Jan 14 22:30:32 2006 for Celestia by  doxygen 1.4.1