Gosu
Math.hpp
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <cmath>
7 
8 // Portability: Define M_PI, which MSVC's <cmath> does not do by default.
9 #if !defined(M_PI) && !defined(SWIG)
10 #define M_PI 3.14159265358979323846264338327950288
11 #endif
12 
13 namespace Gosu
14 {
17  inline long trunc(double value)
18  {
19  return static_cast<long>(value);
20  }
21 
23  inline long round(double value)
24  {
25  if (value >= 0) {
26  return static_cast<long>(value + 0.5);
27  }
28  else {
29  return static_cast<long>(value - 0.5);
30  }
31  }
32 
35  double random(double min, double max);
36 
39  inline double gosu_to_radians(double angle)
40  {
41  return (angle - 90) * M_PI / 180;
42  }
45  inline double radians_to_gosu(double angle)
46  {
47  return angle * 180 / M_PI + 90;
48  }
49 
52  inline double degrees_to_radians(double angle)
53  {
54  return angle * M_PI / 180;
55  }
58  inline double radians_to_degrees(double angle)
59  {
60  return angle * 180 / M_PI;
61  }
62 
67  double offset_x(double angle, double radius);
72  double offset_y(double angle, double radius);
75  double angle(double from_x, double from_y, double to_x, double to_y, double def = 0);
78  double angle_diff(double angle1, double angle2);
80  double normalize_angle(double angle);
81 
83  template<typename T>
84  T square(T value)
85  {
86  return value * value;
87  }
88 
91  template<typename T>
92  T clamp(T value, T min, T max)
93  {
94  if (value < min) return min;
95  if (value > max) return max;
96  return value;
97  }
98 
102  int wrap(int value, int min, int max);
106  float wrap(float value, float min, float max);
110  double wrap(double value, double min, double max);
111 
113  inline double distance_sqr(double x1, double y1, double x2, double y2)
114  {
115  return square(x1 - x2) + square(y1 - y2);
116  }
117 
119  double distance(double x1, double y1, double x2, double y2);
120 
124  template<typename T>
125  T interpolate(T a, T b, double weight = 0.5)
126  {
127  return a * (1.0 - weight) + b * weight;
128  }
129 }
long trunc(double value)
Truncates the fractional part of a real value.
Definition: Math.hpp:17
The library&#39;s main namespace.
Definition: Audio.hpp:12
double distance(double x1, double y1, double x2, double y2)
Returns the distance between two points.
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:92
double distance_sqr(double x1, double y1, double x2, double y2)
Returns the square of the distance between two points.
Definition: Math.hpp:113
T square(T value)
Returns value * value.
Definition: Math.hpp:84
long round(double value)
Rounds a real value towards the next integer.
Definition: Math.hpp:23
double degrees_to_radians(double angle)
Translates between degrees (used by Gosu) and radians, i.e.
Definition: Math.hpp:52
double radians_to_gosu(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:45
double angle_diff(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 angle(double from_x, double from_y, double to_x, double to_y, double def=0)
Returns the angle from point 1 to point 2 in degrees, where 0.0 means upwards.
double offset_x(double angle, double radius)
Returns the horizontal distance between the origin and the point to which you would get if you moved ...
double normalize_angle(double angle)
Normalizes an angle to fit into the range [0; 360[.
int wrap(int value, int min, int max)
Returns (value-min) % (max-min) + min, where % always has a positive result for max > 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 gosu_to_radians(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:39
#define M_PI
Definition: Math.hpp:10
double radians_to_degrees(double angle)
Translates between degrees (used by Gosu) and radians, i.e.
Definition: Math.hpp:58
double offset_y(double angle, double radius)
Returns the vertical distance between the origin and the point to which you would get if you moved ra...