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  double random(double min, double max);
18
22  {
23  return (angle - 90) * M_PI / 180;
24  }
28  {
29  return angle * 180 / M_PI + 90;
30  }
31
35  {
36  return angle * M_PI / 180;
37  }
41  {
42  return angle * 180 / M_PI;
43  }
44
49  double offset_x(double angle, double radius);
54  double offset_y(double angle, double radius);
57  double angle(double from_x, double from_y, double to_x, double to_y, double def = 0);
60  double angle_diff(double angle1, double angle2);
62  double normalize_angle(double angle);
63
65  template<typename T>
66  T square(T value)
67  {
68  return value * value;
69  }
70
73  template<typename T>
74  T clamp(T value, T min, T max)
75  {
76  if (value < min) return min;
77  if (value > max) return max;
78  return value;
79  }
80
84  int wrap(int value, int min, int max);
88  float wrap(float value, float min, float max);
92  double wrap(double value, double min, double max);
93
95  inline double distance_sqr(double x1, double y1, double x2, double y2)
96  {
97  return square(x1 - x2) + square(y1 - y2);
98  }
99
101  double distance(double x1, double y1, double x2, double y2);
102
106  template<typename T>
107  T interpolate(T a, T b, double weight = 0.5)
108  {
109  return a * (1.0 - weight) + b * weight;
110  }
111 }
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:74
double distance_sqr(double x1, double y1, double x2, double y2)
Returns the square of the distance between two points.
Definition: Math.hpp:95
T square(T value)
Returns value * value.
Definition: Math.hpp:66
Translates between degrees (used by Gosu) and radians, i.e.
Definition: Math.hpp:34
Translates between Gosu&#39;s angle system (where 0 is at the top) and radians (where 0 is at the right)...
Definition: Math.hpp:27
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.
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).