Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
MathUtils.cs
浏览该文件的文档.
1namespace Engine {
2 public static class MathUtils {
3 public const float PI = (float)Math.PI;
4
5 public const float E = (float)Math.E;
6
7 public static int Min(int x1, int x2) {
8 if (x1 >= x2) {
9 return x2;
10 }
11 return x1;
12 }
13
14 public static int Min(int x1, int x2, int x3) => Min(Min(x1, x2), x3);
15
16 public static int Min(int x1, int x2, int x3, int x4) => Min(Min(Min(x1, x2), x3), x4);
17
18 public static int Max(int x1, int x2) {
19 if (x1 <= x2) {
20 return x2;
21 }
22 return x1;
23 }
24
25 public static int Max(int x1, int x2, int x3) => Max(Max(x1, x2), x3);
26
27 public static int Max(int x1, int x2, int x3, int x4) => Max(Max(Max(x1, x2), x3), x4);
28
29 public static int Clamp(int x, int min, int max) {
30 if (x >= min) {
31 if (x <= max) {
32 return x;
33 }
34 return max;
35 }
36 return min;
37 }
38
39 public static int Sign(int x) => Math.Sign(x);
40
41 public static int Abs(int x) => Math.Abs(x);
42
43 public static int Sqr(int x) => x * x;
44
45 public static bool IsPowerOf2(uint x) {
46 if (x != 0) {
47 return (x & (x - 1)) == 0;
48 }
49 return false;
50 }
51
52 public static uint NextPowerOf2(uint x) {
53 x--;
54 x |= x >> 1;
55 x |= x >> 2;
56 x |= x >> 4;
57 x |= x >> 8;
58 x |= x >> 16;
59 x++;
60 return x;
61 }
62
63 public static int Hash(int key) => (int)Hash((uint)key);
64
65 public static uint Hash(uint key) {
66 key ^= key >> 16;
67 key *= 2146121005;
68 key ^= key >> 15;
69 key *= 2221713035u;
70 key ^= key >> 16;
71 return key;
72 }
73
74 public static long Min(long x1, long x2) {
75 if (x1 >= x2) {
76 return x2;
77 }
78 return x1;
79 }
80
81 public static long Min(long x1, long x2, long x3) => Min(Min(x1, x2), x3);
82
83 public static long Min(long x1, long x2, long x3, long x4) => Min(Min(Min(x1, x2), x3), x4);
84
85 public static long Max(long x1, long x2) {
86 if (x1 <= x2) {
87 return x2;
88 }
89 return x1;
90 }
91
92 public static long Max(long x1, long x2, long x3) => Max(Max(x1, x2), x3);
93
94 public static long Max(long x1, long x2, long x3, long x4) => Max(Max(Max(x1, x2), x3), x4);
95
96 public static long Clamp(long x, long min, long max) {
97 if (x >= min) {
98 if (x <= max) {
99 return x;
100 }
101 return max;
102 }
103 return min;
104 }
105
106 public static long Sign(long x) => Math.Sign(x);
107
108 public static long Abs(long x) => Math.Abs(x);
109
110 public static long Sqr(long x) => x * x;
111
112 public static bool IsPowerOf2(long x) {
113 if (x > 0) {
114 return (x & (x - 1)) == 0;
115 }
116 return false;
117 }
118
119 public static ulong NextPowerOf2(ulong x) {
120 x--;
121 x |= x >> 1;
122 x |= x >> 2;
123 x |= x >> 4;
124 x |= x >> 8;
125 x |= x >> 16;
126 x |= x >> 32;
127 x++;
128 return x;
129 }
130
131 public static float Min(float x1, float x2) {
132 if (!(x1 < x2)) {
133 return x2;
134 }
135 return x1;
136 }
137
138 public static float Min(float x1, float x2, float x3) => Min(Min(x1, x2), x3);
139
140 public static float Min(float x1, float x2, float x3, float x4) => Min(Min(Min(x1, x2), x3), x4);
141
142 public static float Max(float x1, float x2) {
143 if (!(x1 > x2)) {
144 return x2;
145 }
146 return x1;
147 }
148
149 public static float Max(float x1, float x2, float x3) => Max(Max(x1, x2), x3);
150
151 public static float Max(float x1, float x2, float x3, float x4) => Max(Max(Max(x1, x2), x3), x4);
152
153 public static float Clamp(float x, float min, float max) {
154 if (!(x < min)) {
155 if (!(x > max)) {
156 return x;
157 }
158 return max;
159 }
160 return min;
161 }
162
163 public static float Saturate(float x) {
164 if (!(x < 0f)) {
165 if (!(x > 1f)) {
166 return x;
167 }
168 return 1f;
169 }
170 return 0f;
171 }
172
173 public static float Sign(float x) => Math.Sign(x);
174
175 public static float Abs(float x) => Math.Abs(x);
176
177 public static float Floor(float x) => (float)Math.Floor(x);
178
179 public static float Ceiling(float x) => (float)Math.Ceiling(x);
180
181 public static float Round(float x) => (float)Math.Round(x);
182
183 public static float Remainder(float x, float y) => x - Floor(x / y) * y;
184
185 public static float Sqr(float x) => x * x;
186
187 public static float Sqrt(float x) => (float)Math.Sqrt(x);
188
189 public static float Sin(float x) => (float)Math.Sin(x);
190
191 public static float Cos(float x) => (float)Math.Cos(x);
192
193 public static float Tan(float x) => (float)Math.Tan(x);
194
195 public static float Asin(float x) => (float)Math.Asin(x);
196
197 public static float Acos(float x) => (float)Math.Acos(x);
198
199 public static float Atan(float x) => (float)Math.Atan(x);
200
201 public static float Atan2(float y, float x) => (float)Math.Atan2(y, x);
202
203 public static float Exp(float n) => (float)Math.Exp(n);
204
205 public static float Log(float x) => (float)Math.Log(x);
206
207 public static float Log10(float x) => (float)Math.Log10(x);
208
209 public static float Pow(float x, float n) => (float)Math.Pow(x, n);
210
211 public static float PowSign(float x, float n) => Sign(x) * Pow(Abs(x), n);
212
213 public static float Lerp(float x1, float x2, float f) => x1 + (x2 - x1) * f;
214
215 public static float SmoothStep(float min, float max, float x) {
216 x = Clamp((x - min) / (max - min), 0f, 1f);
217 return x * x * (3f - 2f * x);
218 }
219
220 public static float CatmullRom(float v1, float v2, float v3, float v4, float f) {
221 float num = f * f;
222 float num2 = num * f;
223 return 0.5f * (2f * v2 + (v3 - v1) * f + (2f * v1 - 5f * v2 + 4f * v3 - v4) * num + (3f * v2 - v1 - 3f * v3 + v4) * num2);
224 }
225
226 public static float NormalizeAngle(float angle) {
227 angle = (float)Math.IEEERemainder(angle, 6.2831854820251465);
228 if (angle > (float)Math.PI) {
229 angle -= (float)Math.PI * 2f;
230 }
231 else if (angle <= -(float)Math.PI) {
232 angle += (float)Math.PI * 2f;
233 }
234 return angle;
235 }
236
237 public static float Sigmoid(float x, float steepness) {
238 if (x <= 0f) {
239 return 0f;
240 }
241 if (x >= 1f) {
242 return 1f;
243 }
244 float num = Exp(steepness);
245 float num2 = Exp(2f * steepness * x);
246 return num * (num2 - 1f) / ((num - 1f) * (num2 + num));
247 }
248
249 public static float DegToRad(float degrees) => degrees / 180f * (float)Math.PI;
250
251 public static float RadToDeg(float radians) => radians * 180f / (float)Math.PI;
252
253 public static double Min(double x1, double x2) {
254 if (!(x1 < x2)) {
255 return x2;
256 }
257 return x1;
258 }
259
260 public static double Min(double x1, double x2, double x3) => Min(Min(x1, x2), x3);
261
262 public static double Min(double x1, double x2, double x3, double x4) => Min(Min(Min(x1, x2), x3), x4);
263
264 public static double Max(double x1, double x2) {
265 if (!(x1 > x2)) {
266 return x2;
267 }
268 return x1;
269 }
270
271 public static double Max(double x1, double x2, double x3) => Max(Max(x1, x2), x3);
272
273 public static double Max(double x1, double x2, double x3, double x4) => Max(Max(Max(x1, x2), x3), x4);
274
275 public static double Clamp(double x, double min, double max) {
276 if (!(x < min)) {
277 if (!(x > max)) {
278 return x;
279 }
280 return max;
281 }
282 return min;
283 }
284
285 public static double Saturate(double x) {
286 if (!(x < 0.0)) {
287 if (!(x > 1.0)) {
288 return x;
289 }
290 return 1.0;
291 }
292 return 0.0;
293 }
294
295 public static double Sign(double x) => Math.Sign(x);
296
297 public static double Abs(double x) => Math.Abs(x);
298
299 public static double Floor(double x) => Math.Floor(x);
300
301 public static double Ceiling(double x) => Math.Ceiling(x);
302
303 public static double Round(double x) => Math.Round(x);
304
305 public static double Remainder(double x, double y) => x - Floor(x / y) * y;
306
307 public static double Sqr(double x) => x * x;
308
309 public static double Sqrt(double x) => Math.Sqrt(x);
310
311 public static double Sin(double x) => Math.Sin(x);
312
313 public static double Cos(double x) => Math.Cos(x);
314
315 public static double Tan(double x) => Math.Tan(x);
316
317 public static double Asin(double x) => Math.Asin(x);
318
319 public static double Acos(double x) => Math.Acos(x);
320
321 public static double Atan(double x) => Math.Atan(x);
322
323 public static double Atan2(double y, double x) => Math.Atan2(y, x);
324
325 public static double Exp(double n) => Math.Exp(n);
326
327 public static double Log(double x) => Math.Log(x);
328
329 public static double Log10(double x) => Math.Log10(x);
330
331 public static double Pow(double x, double n) => Math.Pow(x, n);
332
333 public static double PowSign(double x, double n) => Sign(x) * Pow(Abs(x), n);
334
335 public static double Lerp(double x1, double x2, double f) => x1 + (x2 - x1) * f;
336
337 public static double SmoothStep(double min, double max, double x) {
338 x = Clamp((x - min) / (max - min), 0.0, 1.0);
339 return x * x * (3.0 - 2.0 * x);
340 }
341
342 public static double CatmullRom(double v1, double v2, double v3, double v4, double f) {
343 double num = f * f;
344 double num2 = num * f;
345 return 0.5 * (2.0 * v2 + (v3 - v1) * f + (2.0 * v1 - 5.0 * v2 + 4.0 * v3 - v4) * num + (3.0 * v2 - v1 - 3.0 * v3 + v4) * num2);
346 }
347
348 public static double NormalizeAngle(double angle) {
349 angle = Math.IEEERemainder(angle, Math.PI * 2.0);
350 if (angle > 3.1415927410125732) {
351 angle -= Math.PI * 2.0;
352 }
353 else if (angle <= -Math.PI) {
354 angle += Math.PI * 2.0;
355 }
356 return angle;
357 }
358
359 public static double DegToRad(double degrees) => degrees / 180.0 * Math.PI;
360
361 public static double RadToDeg(double radians) => radians * 180.0 / Math.PI;
362
363 public static float LinearStep(float zero, float one, float f) => Saturate((f - zero) / (one - zero));
364 }
365}
static float Max(float x1, float x2, float x3, float x4)
static double Sign(double x)
static double SmoothStep(double min, double max, double x)
static float RadToDeg(float radians)
static float Round(float x)
static int Min(int x1, int x2, int x3, int x4)
static double Abs(double x)
static float Acos(float x)
static int Clamp(int x, int min, int max)
static float Exp(float n)
static int Sign(int x)
static float Remainder(float x, float y)
static float Asin(float x)
static uint NextPowerOf2(uint x)
static long Min(long x1, long x2)
static int Abs(int x)
static float Sqrt(float x)
static long Sqr(long x)
static float Pow(float x, float n)
static int Max(int x1, int x2, int x3)
static double Max(double x1, double x2)
static double RadToDeg(double radians)
static float Sqr(float x)
static double Sqr(double x)
static bool IsPowerOf2(uint x)
static float SmoothStep(float min, float max, float x)
static float Cos(float x)
static double PowSign(double x, double n)
static float Abs(float x)
static float Sin(float x)
static double Sqrt(double x)
static double Saturate(double x)
static double NormalizeAngle(double angle)
static float Max(float x1, float x2)
static double Exp(double n)
static double Min(double x1, double x2, double x3, double x4)
static double CatmullRom(double v1, double v2, double v3, double v4, double f)
static double Log10(double x)
static double Atan(double x)
static double Log(double x)
static int Min(int x1, int x2)
static float Min(float x1, float x2, float x3)
static float Log(float x)
static long Min(long x1, long x2, long x3, long x4)
static double DegToRad(double degrees)
static float Sign(float x)
static double Asin(double x)
static long Abs(long x)
static double Tan(double x)
static float Sigmoid(float x, float steepness)
static long Sign(long x)
static float Log10(float x)
static long Max(long x1, long x2, long x3)
static ulong NextPowerOf2(ulong x)
static long Max(long x1, long x2, long x3, long x4)
static double Max(double x1, double x2, double x3)
static float Atan(float x)
static int Max(int x1, int x2, int x3, int x4)
static float NormalizeAngle(float angle)
static double Max(double x1, double x2, double x3, double x4)
static long Min(long x1, long x2, long x3)
static int Hash(int key)
static double Cos(double x)
static float Tan(float x)
static double Sin(double x)
static double Floor(double x)
static double Lerp(double x1, double x2, double f)
static float Clamp(float x, float min, float max)
static double Pow(double x, double n)
static float Saturate(float x)
static float PowSign(float x, float n)
static double Acos(double x)
static double Remainder(double x, double y)
static double Clamp(double x, double min, double max)
static long Clamp(long x, long min, long max)
static double Round(double x)
static uint Hash(uint key)
static float Min(float x1, float x2, float x3, float x4)
static float Max(float x1, float x2, float x3)
static float Min(float x1, float x2)
static double Min(double x1, double x2, double x3)
static long Max(long x1, long x2)
static int Max(int x1, int x2)
static double Ceiling(double x)
static bool IsPowerOf2(long x)
static float Atan2(float y, float x)
static float Ceiling(float x)
static int Sqr(int x)
static double Min(double x1, double x2)
static float CatmullRom(float v1, float v2, float v3, float v4, float f)
static double Atan2(double y, double x)
static int Min(int x1, int x2, int x3)
static float Floor(float x)
static float DegToRad(float degrees)
static float Lerp(float x1, float x2, float f)
static float LinearStep(float zero, float one, float f)