-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathf.ts
191 lines (173 loc) · 3.98 KB
/
mathf.ts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Common utilities
* @module glMatrix
*/
export default class mathf {
// Configuration Constants
static readonly EPSILON = 0.000001;
static readonly RANDOM = Math.random;
static readonly ANGLE_ORDER = "zyx";
static readonly DegToRad = Math.PI / 180;
static readonly RadToDeg = 180 / Math.PI;
/**
* Convert Degree To Radian
*
* @param {number} a Angle in Degrees
*/
static toRadian(a: number) {
return a * mathf.DegToRad;
}
/**
* Tests whether or not the arguments have approximately the same value, within an absolute
* or relative tolerance of glMatrix.EPSILON (an absolute tolerance is used for values less
* than or equal to 1.0, and a relative tolerance is used for larger values)
*
* @param {number} a The first number to test.
* @param {number} b The second number to test.
* @returns {Boolean} True if the numbers are approximately equal, false otherwise.
*/
equals(a: number, b: number) {
return Math.abs(a - b) <= mathf.EPSILON * Math.max(1.0, Math.abs(a), Math.abs(b));
}
public static readonly PI: number = Math.PI;
/**
* 绝对值
* @param value
* @returns
*/
public static abs(value: number) {
return Math.abs(value);
}
/**
* 取上边界
* @param value
* @returns
*/
public static ceil(value: number) {
return Math.ceil(value);
}
/**
* 取下边界
* @param value
* @returns
*/
public static floor(value: number) {
return Math.floor(value);
}
/**
* 四舍五入
* @param value
* @returns
*/
public static round(value: number) {
return Math.round(value);
}
/**
* 开方
* @param value
* @returns
*/
public static sqrt(value: number) {
return Math.sqrt(value);
}
/**
* 正弦
* @param radians
* @returns
*/
public static sin(radians: number) {
return Math.sin(radians);
}
/**
* 余弦
* @param radians
* @returns
*/
public static cos(radians: number) {
return Math.cos(radians);
}
/**
* 正切
* @param radians
* @returns
*/
public static tan(radians: number) {
return Math.tan(radians);
}
/**
* 方位角
* @param y
* @param x
* @returns
*/
public static atan2(y: number, x: number) {
return Math.atan2(y, x);
}
/**
* 最大值
* @param a
* @param b
* @returns
*/
public static max(a: number, b: number) {
return Math.max(a, b);
}
/**
* 最小值
* @param a
* @param b
* @returns
*/
public static min(a: number, b: number) {
return Math.min(a, b);
}
/**
* 限定在最小值和最大值之间
* @param value
* @param min
* @param max
* @returns
*/
public static clamp(value: number, min: number, max: number) {
return Math.max(Math.min(value, max), min);
}
/**
* 插值
* @param a
* @param b
* @param t
* @returns
*/
public static lerp(a: number, b: number, t: number) {
return a + (b - a) * t;
}
/**
* 当前值向target每次最多移动maxDelta距离
* @param current
* @param target
* @param maxDelta
* @returns
*/
public static moveTowards(current: number, target: number, maxDelta: number) {
const difference = target - current;
if (Math.abs(difference) <= maxDelta) {
return target;
} else {
return current + Math.sign(difference) * maxDelta;
}
}
/**
* 反插值
* @param a
* @param b
* @param t
* @returns
*/
inverseLerp(a: number, b: number, t: number) {
if (a === b) {
return 0;
} else {
return (t - a) / (b - a);
}
}
}