1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
/*
math/functions.h
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
#ifndef __INCLUDED_MATH_FUNCTIONS_H__
#define __INCLUDED_MATH_FUNCTIONS_H__
#include <cmath>
#include <cstdlib>
#include <cmath>
namespace math
{
/**
* @brief return the smallest of two floats
*/
inline const float min(const float a, const float b)
{
return (a < b ? a : b);
}
/**
* @brief return the smallest of two integers
*/
inline const int min(const int a, const int b)
{
return (a < b ? a : b);
}
/**
* @brief return the smallest of two longs
*/
inline const long min(const long a, const long b)
{
return (a < b ? a : b);
}
/**
* @brief return the largest of two floats
*/
inline const float max(const float a, const float b)
{
return (a > b ? a : b);
}
/**
* @brief return the largest of two integers
*/
inline const int max(const int a, const int b)
{
return (a > b ? a : b);
}
/**
* @brief return the largest of two longs
*/
inline const long max(const long a, const long b)
{
return (a > b ? a : b);
}
/**
* @brief returns a random float
* The value returned will be in the interval [0-max]
* @param max the maximum value returned
* */
inline float randomf(const float max = 1.0f)
{
return ((float) rand() / (float) RAND_MAX) * max;
}
/**
* @brief returns a random float
* The value returned will be in the interval [min-max]
* */
inline float randomf(const float min, const float max)
{
return (min + randomf(max - min));
}
/**
* @brief returns a random integer
* The value returned will be in the interval [0-(max-1)]
* @param max the maximum value returned
* */
inline unsigned randomi(const unsigned int max)
{
return ((unsigned int)(rand() % max));
}
/// return the sign of a float value
float sgnf(float value);
/// return an angle in the ]-180,180] range
float degrees180f(float angle);
/// return an angle in the [0,360[ range
float degrees360f(float angle);
/// clamp a float to a specified range
inline void clamp(float &value, const float min = 0.0f, const float max = 1.0f)
{
if (value < min)
value = min;
else if (value > max)
value = max;
}
/// return the absolute value of a float
inline float absf(const float f)
{
return ( f < 0.0f ? -f : f );
}
/// swap two float values
inline void swap(float &x, float &y)
{
float tmp = x;
x = y;
y = tmp;
}
} // namespace math
#endif // __INCLUDED_MATH_FUNCTIONS_H__
|