Math.hpp
Go to the documentation of this file.
```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
00035     {
00036         return (angle - 90) * pi / 180;
00037     }
00041     {
00042         return angle * 180 / pi + 90;
00043     }
00044
00048     {
00049         return angle * pi / 180;
00050     }
00054     {
00055         return angle * 180 / pi;
00056     }
00057
00062     double offsetX(double angle, double radius);
00067     double offsetY(double angle, double radius);
00070     double angle(double fromX, double fromY, double toX, double toY,
00071         double def = 0);
00074     double angleDiff(double angle1, double angle2);
00076     double normalizeAngle(double angle);
00077
00079     template<typename T>
00080     T square(T value)
00081     {
00082         return value * value;
00083     }
00084
00087     template<typename T>
00088     T clamp(T value, T min, T max)
00089     {
00090         if (value < min)
00091             return min;
00092         if (value > max)
00093             return max;
00094         return value;
00095     }
00096
00097     // Backward compatibility with 0.7.x
00098     template<typename T>
00099     T boundBy(T value, T min, T max)
00100     {
00101         return clamp(value, min, max);
00102     }
00103
00107     int wrap(int value, int min, int max);
00111     float wrap(float value, float min, float max);
00115     double wrap(double value, double min, double max);
00116
00118     inline double distanceSqr(double x1, double y1, double x2, double y2)
00119     {
00120         return square(x1 - x2) + square(y1 - y2);
00121     }
00122
00124     double distance(double x1, double y1, double x2, double y2);
00125
00128     template<typename T>
00129     T interpolate(T a, T b, double weight = 0.5)
00130     {
00131         return a * (1.0 - weight) + b * weight;
00132     }
00133 }
00134
00135 #endif
```