Math.hpp
Go to the documentation of this file.
1 
2 
3 
4 #ifndef GOSU_MATH_HPP
5 #define GOSU_MATH_HPP
6 
7 namespace Gosu
8 {
10  const double pi = 3.1415926536;
11 
14  inline long trunc(double value)
15  {
16  return static_cast<long>(value);
17  }
18 
20  inline long round(double value)
21  {
22  if (value >= 0)
23  return static_cast<long>(value + 0.5);
24  else
25  return static_cast<long>(value - 0.5);
26  }
27 
30  double random(double min, double max);
31 
34  inline double gosuToRadians(double angle)
35  {
36  return (angle - 90) * pi / 180;
37  }
40  inline double radiansToGosu(double angle)
41  {
42  return angle * 180 / pi + 90;
43  }
44 
47  inline double degreesToRadians(double angle)
48  {
49  return angle * pi / 180;
50  }
53  inline double radiansToDegrees(double angle)
54  {
55  return angle * 180 / pi;
56  }
57 
62  double offsetX(double angle, double radius);
67  double offsetY(double angle, double radius);
70  double angle(double fromX, double fromY, double toX, double toY,
71  double def = 0);
74  double angleDiff(double angle1, double angle2);
76  double normalizeAngle(double angle);
77 
79  template<typename T>
80  T square(T value)
81  {
82  return value * value;
83  }
84 
87  template<typename T>
88  T clamp(T value, T min, T max)
89  {
90  if (value < min)
91  return min;
92  if (value > max)
93  return max;
94  return value;
95  }
96 
97  // Backward compatibility with 0.7.x
98  template<typename T>
99  T boundBy(T value, T min, T max)
100  {
101  return clamp(value, min, max);
102  }
103 
107  int wrap(int value, int min, int max);
111  float wrap(float value, float min, float max);
115  double wrap(double value, double min, double max);
116 
118  inline double distanceSqr(double x1, double y1, double x2, double y2)
119  {
120  return square(x1 - x2) + square(y1 - y2);
121  }
122 
124  double distance(double x1, double y1, double x2, double y2);
125 
128  template<typename T>
129  T interpolate(T a, T b, double weight = 0.5)
130  {
131  return a * (1.0 - weight) + b * weight;
132  }
133 }
134 
135 #endif