1// SuperTux
2// Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#ifndef HEADER_SUPERTUX_MATH_UTIL_HPP
18#define HEADER_SUPERTUX_MATH_UTIL_HPP
19
20namespace math {
21
22template<class T>
23const T& clamp(const T& val, const T& min, const T& max)
24{
25 if (val < min)
26 {
27 return min;
28 }
29 else if (val > max)
30 {
31 return max;
32 }
33 else
34 {
35 return val;
36 }
37}
38
39constexpr float TAU = 6.28318530717958647693f;
40constexpr float PI = 3.14159265358979323846f;
41constexpr float PI_2 = 1.57079632679489661923f;
42constexpr float PI_4 = 0.78539816339744830962f;
43
44inline float degrees(float rad)
45{
46 return rad / TAU * 360.0f;
47}
48
49inline float radians(float deg)
50{
51 return deg / 360.0f * TAU;
52}
53
54inline int positive_mod(int lhs, int rhs)
55{
56 return (lhs % rhs + rhs) % rhs;
57}
58
59inline float positive_fmodf(float lhs, float rhs)
60{
61 return fmodf(fmodf(lhs, rhs) + rhs, rhs);
62}
63
64} // namespace math
65
66#endif
67
68/* EOF */
69