Math.hpp
Go to the documentation of this file.
1 
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
double normalizeAngle(double angle)
Normalizes an angle to fit into the range [0; 360[.
long trunc(double value)
Truncates the fractional part of a real value.
Definition: Math.hpp:14
double distance(double x1, double y1, double x2, double y2)
Returns the distance between two points.
const double pi
Pi.
Definition: Math.hpp:10
double gosuToRadians(double angle)
Translates between Gosu&#39;s angle system (where 0¡ is at the top) and radians (where 0 is at the right)...
Definition: Math.hpp:34
T clamp(T value, T min, T max)
Returns min if value is smaller than min, max if value is larger than max and value otherwise...
Definition: Math.hpp:88
T square(T value)
Returns value * value.
Definition: Math.hpp:80
long round(double value)
Rounds a real value towards the next integer.
Definition: Math.hpp:20
T boundBy(T value, T min, T max)
Definition: Math.hpp:99
double offsetX(double angle, double radius)
Returns the horizontal distance between the origin and the point to which you would get if you moved ...
double angleDiff(double angle1, double angle2)
Returns the smallest angle that can be added to angle1 to get to angle2 (can be negative if counter-c...
double distanceSqr(double x1, double y1, double x2, double y2)
Returns the square of the distance between two points.
Definition: Math.hpp:118
int wrap(int value, int min, int max)
Returns (value-min) % (max-min) + min, where % always has a positive result for max &gt; min...
Color interpolate(Color a, Color b, double weight=0.5)
Interpolates linearly between two colors, with a given weight towards the second color.
double random(double min, double max)
Returns a real value between min (inclusive) and max (exclusive).
double radiansToDegrees(double angle)
Translates between degrees (used by Gosu) and radians, i.e.
Definition: Math.hpp:53
double offsetY(double angle, double radius)
Returns the vertical distance between the origin and the point to which you would get if you moved ra...
double degreesToRadians(double angle)
Translates between degrees (used by Gosu) and radians, i.e.
Definition: Math.hpp:47
double radiansToGosu(double angle)
Translates between Gosu&#39;s angle system (where 0¡ is at the top) and radians (where 0 is at the right)...
Definition: Math.hpp:40
double angle(double fromX, double fromY, double toX, double toY, double def=0)
Returns the angle from point 1 to point 2 in degrees, where 0.0 means upwards.