-
Notifications
You must be signed in to change notification settings - Fork 0
/
Math.h
108 lines (83 loc) · 1.57 KB
/
Math.h
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
#pragma once
#include<cmath>
#include<assert.h>
#include"JmxRConfig.h"
#include"Vector.h"
#ifndef max
#define max(a,b) a>b?a:b
#endif // !max
#ifndef min
#define min(a,b) a<b?a:b
#endif
#ifndef EPSILON
#define EPSILON 0.00000001f
#endif
#define PI 3.141592657f
#define PI_2 6.2831852f
namespace jmxRCore
{
FORCEINLINE f32 angle2Radian(f32 angle)
{
return angle / 180.f *PI;
}
FORCEINLINE f32 radian2Angle(f32 radian)
{
return radian / PI *180.f;
}
FORCEINLINE f32 clamp(f32 v, f32 low, f32 high)
{
if (v < low) return low;
else if (v > high) return high;
else return v;
}
FORCEINLINE f32 lerp(f32 t, f32 a, f32 b)
{
return (1.f - t)*a + (t)*b;
}
FORCEINLINE f32 frac(f32 n)
{
return n - (int)n;
}
FORCEINLINE Vec2 frac(Vec2 v)
{
v.x = v.x - (int)v.x;
v.y = v.y - (int)v.y;
return v;
}
FORCEINLINE Vec3 frac(Vec3 v)
{
v.x = v.x - (int)v.x;
v.y = v.y - (int)v.y;
v.z = v.z - (int)v.z;
return v;
}
//二维矩阵求逆
inline void Mat2Inv(f32 m[4], f32 inv[4])
{
f32 det = m[3] * m[0] - m[1] * m[2];
if (fabsf(det) < EPSILON)
{
inv[0] = 0; inv[1] = 0; inv[2] = 0; inv[3] = 0;
}
f32 dd = 1.f / det;
inv[0] = +dd*m[3];
inv[1] = -dd*m[1];
inv[2] = -dd*m[2];
inv[3] = +dd*m[0];
}
//求解一元二次方程
inline bool equation2(f32 a, f32 b, f32 c, f32* t0 = nullptr, f32* t1 = nullptr)
{
f32 dd = b*b - 4 * a*c;
if (dd < 0)
return false;
f32 sqrtd = sqrtf(dd);
if (fabs(a) < EPSILON)
return false;
if (t0)
*t0 = (-b + sqrtd) / (2 * a);
if (t1)
*t1 = (-b - sqrtd) / (2 * a);
return true;
}
}