00001 00002 00003 00004 #ifndef GOSU_MATH_HPP 00005 #define GOSU_MATH_HPP 00006 00007 namespace Gosu 00008 { 00010 const double pi = 3.1415926536; 00011 00014 inline long trunc(double value) 00015 { 00016 return static_cast<long>(value); 00017 } 00018 00020 inline long round(double value) 00021 { 00022 if (value >= 0) 00023 return static_cast<long>(value + 0.5); 00024 else 00025 return static_cast<long>(value - 0.5); 00026 } 00027 00030 double random(double min, double max); 00031 00033 inline double gosuToRadians(double angle) 00034 { 00035 return (angle - 90) * pi / 180; 00036 } 00038 inline double radiansToGosu(double angle) 00039 { 00040 return angle * 180 / pi + 90; 00041 } 00042 00045 inline double gosuToRadianScale(double angle) 00046 { 00047 return angle * pi / 180; 00048 } 00051 inline double radiansScaleToGosu(double angle) 00052 { 00053 return angle * 180 / pi; 00054 } 00055 00060 double offsetX(double angle, double radius); 00065 double offsetY(double angle, double radius); 00068 double angle(double fromX, double fromY, double toX, double toY, 00069 double def = 0); 00072 double angleDiff(double angle1, double angle2); 00074 double normalizeAngle(double angle); 00075 00077 template<typename T> 00078 T square(T value) 00079 { 00080 return value * value; 00081 } 00082 00085 template<typename T> 00086 T clamp(T value, T min, T max) 00087 { 00088 if (value < min) 00089 return min; 00090 if (value > max) 00091 return max; 00092 return value; 00093 } 00094 00095 // Backward compatibility with 0.7.x 00096 template<typename T> 00097 T boundBy(T value, T min, T max) 00098 { 00099 return clamp(value, min, max); 00100 } 00101 00103 inline double distanceSqr(double x1, double y1, double x2, double y2) 00104 { 00105 return square(x1 - x2) + square(y1 - y2); 00106 } 00107 00109 double distance(double x1, double y1, double x2, double y2); 00110 00113 template<typename T> 00114 T interpolate(T a, T b, double weight = 0.5) 00115 { 00116 return a * (1.0 - weight) + b * weight; 00117 } 00118 } 00119 00120 #endif
Documentation not clear enough? Please go to one of the places listed on http://www.libgosu.org/ and leave feedback. Thanks!