-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathnum.hpp
More file actions
72 lines (57 loc) · 1.94 KB
/
num.hpp
File metadata and controls
72 lines (57 loc) · 1.94 KB
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
/**
* Copyright (C) 2011-2022 Bitcraze AB, 2025 Simon D. Levy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <math.h>
#include <stdint.h>
#include <string.h>
#include <datatypes.hpp>
class Num {
public:
// Small number epsilon, to prevent dividing by zero
static constexpr float EPSILON = 1e-6f;
static constexpr float RAD2DEG = 180.0f / M_PI;
static constexpr float DEG2RAD = M_PI / 180.0f;
static float fconstrain(const float val, const float maxabs)
{
return val < -maxabs ? -maxabs : val > maxabs ? maxabs : val;
}
static float fconstrain(
float value, const float minVal, const float maxVal)
{
return fminf(maxVal, fmaxf(minVal,value));
}
static float cap_angle(float angle)
{
float result = angle;
while (result > 180.0f) {
result -= 360.0f;
}
while (result < -180.0f) {
result += 360.0f;
}
return result;
}
static float rescale(
const float value,
const float oldmin,
const float oldmax,
const float newmin,
const float newmax)
{
return (value - oldmin) / (oldmax - oldmin) *
(newmax - newmin) + newmin;
}
};