-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloatMath.h
More file actions
executable file
·137 lines (104 loc) · 2.27 KB
/
FloatMath.h
File metadata and controls
executable file
·137 lines (104 loc) · 2.27 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
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
131
132
133
134
135
136
137
#ifndef __FLOATMATH_H__
#define __FLOATMATH_H__
#include <math.h>
const float PI = 3.1415926535897932384626433832795f;
const float EPS = 1e-8f;
typedef float AngRad; //角度(弧度制)
typedef float Angle; //角度(角度制)
typedef float Dist; //距离
inline float Min(float a, float b){return (a < b ? a : b);}
inline float Max(float a, float b){return (a > b ? a : b);}
inline float Sqr(float x){ return x*x; }
//弧度转角度
inline Angle Rad2Deg ( float x ){return ( x * 180 / PI );}
//角度转弧度
inline float Deg2Rad ( Angle x ){return ( x * PI / 180 );}
//角度制的cos函数
inline float cosDeg ( Angle x ){return ( float )( cos( Deg2Rad( x ) ) );}
//角度制的sin函数
inline float sinDeg ( Angle x ){return ( float )( sin( Deg2Rad( x ) ) );}
//角度制的tan函数
inline float tanDeg ( Angle x ){return ( float )( tan( Deg2Rad( x ) ) );}
//角度制的atan函数
inline Angle atanDeg ( float x ){return ( Rad2Deg( ( float )atan( x ) ) );}
//角度制的atan2函数(通过两条直角边计算第一条边所对的锐角)
inline Angle atan2Deg( float x, float y )
{
if( ( float )fabs( x ) < EPS && ( float )fabs( y ) < EPS )
{
return ( 0.0 );
}
else
{
return ( Rad2Deg( ( float )atan2( x, y ) ) );
}
}
//角度制的acos函数
inline Angle acosDeg ( float x )
{
if( x >= 1 )
return ( 0.0 );
else if( x <= -1 )
return ( 180.0 );
return ( Rad2Deg( ( float )acos( x ) ) );
}
//角度制的asin函数
inline Angle asinDeg ( float x )
{
if( x >= 1 )
return ( 90.0 );
else if ( x <= -1 )
return ( -90.0 );
return ( Rad2Deg( ( float )asin( x ) ) );
}
//将角度限制在-180到180之间
inline Angle Normalize(Angle a)
{
while(a > 180)
a -= 360;
while(a < -180)
a += 360;
return a;
}
//将角度限制在0到360之间
inline Angle InNormalize(Angle a)
{
a = Normalize(a);
if (a < 0)
{
a += 360;
}
return a;
}
inline AngRad NormalizeRad(AngRad ang)
{
float dPi = 2 * PI;
while (ang > PI)
{
ang -= dPi;
}
while (ang < -PI)
{
ang += dPi;
}
return ang;
}
inline void Clamp(float &val, float min, float max)
{
if (val < min)
{
val = min;
}
if (val > max)
{
val = max;
}
}
inline void Swap(float &a, float &b)
{
float t;
t = a;
a = b;
b = t;
}
#endif //__FLOATMATH_H__