ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
math.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 //=============================================================================
23 //
24 // Helper math functions
25 //
26 //=============================================================================
27 
28 #ifndef AGS_SHARED_UTIL_MATH_H
29 #define AGS_SHARED_UTIL_MATH_H
30 
31 #include "common/std/limits.h"
32 
33 #include "ags/lib/std.h"
34 
35 namespace AGS3 {
36 
37 #ifndef M_PI
38 #define M_PI 3.14159265358979323846
39 #endif
40 
41 namespace AGS {
42 namespace Shared {
43 
44 namespace Math {
45 template <class T>
46 inline const T &Max(const T &a, const T &b) {
47  return a < b ? b : a;
48 }
49 
50 template <class T>
51 inline const T &Min(const T &a, const T &b) {
52  return a < b ? a : b;
53 }
54 
55 template <class T>
56 inline const T &Clamp(const T &val, const T &floor, const T &ceil) {
57  return Max<T>(floor, Min<T>(val, ceil));
58 }
59 
60 template <class T>
61 inline void ClampLength(T &from, T &length, const T &floor, const T &height) {
62  if (from < floor) {
63  length -= floor - from;
64  from = floor;
65  } else if (from >= floor + height) {
66  from = floor + height;
67  length = 0;
68  }
69 
70  length = Max<T>(length, 0);
71  length = Min<T>(length, height - from);
72 }
73 
74 // Get a measure of how value A is greater than value B;
75 // if A is smaller than or equal to B, returns 0.
76 template <class T>
77 inline T Surplus(const T &larger, const T &smaller) {
78  return larger > smaller ? larger - smaller : 0;
79 }
80 
81 // Tests if the big-type value is in range of the result type;
82 // returns same value converted if it's in range, or provided replacement if it's not.
83 template <typename T, typename TBig>
84 inline T InRangeOrDef(const TBig &val, const T &def) {
85  return (val >= std::numeric_limits<T>::min() && val <= std::numeric_limits<T>::max()) ?
86  static_cast<T>(val) : def;
87 }
88 
89 inline float RadiansToDegrees(float rads) {
90  return rads * (float)(180.0 / M_PI);
91 }
92 
93 inline float DegreesToRadians(float deg) {
94  return deg * (float)(M_PI / 180.0);
95 }
96 
97 } // namespace Math
98 
99 } // namespace Shared
100 } // namespace AGS
101 } // namespace AGS3
102 
103 #endif
Definition: achievements_tables.h:27
Definition: wma.h:38
Definition: ags.h:40