-
Notifications
You must be signed in to change notification settings - Fork 45
/
Mathd.cs
executable file
·376 lines (323 loc) · 12.9 KB
/
Mathd.cs
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Type: UnityEngine.Mathd
// Assembly: UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// Assembly location: C:\Program Files (x86)\Unity\Editor\Data\Managed\UnityEngine.dll
using System;
using System.Runtime.CompilerServices;
namespace UnityEngine {
public struct Mathd {
public const double PI = 3.141593d;
public const double Infinity = double.PositiveInfinity;
public const double NegativeInfinity = double.NegativeInfinity;
public const double Deg2Rad = 0.01745329d;
public const double Rad2Deg = 57.29578d;
public const double Epsilon = 1.401298E-45d;
public static double Sin(double d) {
return Math.Sin(d);
}
public static double Cos(double d) {
return Math.Cos(d);
}
public static double Tan(double d) {
return Math.Tan(d);
}
public static double Asin(double d) {
return Math.Asin(d);
}
public static double Acos(double d) {
return Math.Acos(d);
}
public static double Atan(double d) {
return Math.Atan(d);
}
public static double Atan2(double y, double x) {
return Math.Atan2(y, x);
}
public static double Sqrt(double d) {
return Math.Sqrt(d);
}
public static double Abs(double d) {
return Math.Abs(d);
}
public static int Abs(int value) {
return Math.Abs(value);
}
public static double Min(double a, double b) {
if (a < b)
return a;
else
return b;
}
public static double Min(params double[] values) {
int length = values.Length;
if (length == 0)
return 0.0d;
double num = values[0];
for (int index = 1; index < length; ++index) {
if (values[index] < num)
num = values[index];
}
return num;
}
public static int Min(int a, int b) {
if (a < b)
return a;
else
return b;
}
public static int Min(params int[] values) {
int length = values.Length;
if (length == 0)
return 0;
int num = values[0];
for (int index = 1; index < length; ++index) {
if (values[index] < num)
num = values[index];
}
return num;
}
public static double Max(double a, double b) {
if (a > b)
return a;
else
return b;
}
public static double Max(params double[] values) {
int length = values.Length;
if (length == 0)
return 0d;
double num = values[0];
for (int index = 1; index < length; ++index) {
if ((double)values[index] > (double)num)
num = values[index];
}
return num;
}
public static int Max(int a, int b) {
if (a > b)
return a;
else
return b;
}
public static int Max(params int[] values) {
int length = values.Length;
if (length == 0)
return 0;
int num = values[0];
for (int index = 1; index < length; ++index) {
if (values[index] > num)
num = values[index];
}
return num;
}
public static double Pow(double d, double p) {
return Math.Pow(d, p);
}
public static double Exp(double power) {
return Math.Exp(power);
}
public static double Log(double d, double p) {
return Math.Log(d, p);
}
public static double Log(double d) {
return Math.Log(d);
}
public static double Log10(double d) {
return Math.Log10(d);
}
public static double Ceil(double d) {
return Math.Ceiling(d);
}
public static double Floor(double d) {
return Math.Floor(d);
}
public static double Round(double d) {
return Math.Round(d);
}
public static int CeilToInt(double d) {
return (int)Math.Ceiling(d);
}
public static int FloorToInt(double d) {
return (int)Math.Floor(d);
}
public static int RoundToInt(double d) {
return (int)Math.Round(d);
}
public static double Sign(double d) {
return d >= 0.0 ? 1d : -1d;
}
public static double Clamp(double value, double min, double max) {
if (value < min)
value = min;
else if (value > max)
value = max;
return value;
}
public static int Clamp(int value, int min, int max) {
if (value < min)
value = min;
else if (value > max)
value = max;
return value;
}
public static double Clamp01(double value) {
if (value < 0.0)
return 0.0d;
if (value > 1.0)
return 1d;
else
return value;
}
public static double Lerp(double from, double to, double t) {
return from + (to - from) * Mathd.Clamp01(t);
}
public static double LerpAngle(double a, double b, double t) {
double num = Mathd.Repeat(b - a, 360d);
if (num > 180.0d)
num -= 360d;
return a + num * Mathd.Clamp01(t);
}
public static double MoveTowards(double current, double target, double maxDelta) {
if (Mathd.Abs(target - current) <= maxDelta)
return target;
else
return current + Mathd.Sign(target - current) * maxDelta;
}
public static double MoveTowardsAngle(double current, double target, double maxDelta) {
target = current + Mathd.DeltaAngle(current, target);
return Mathd.MoveTowards(current, target, maxDelta);
}
public static double SmoothStep(double from, double to, double t) {
t = Mathd.Clamp01(t);
t = (-2.0 * t * t * t + 3.0 * t * t);
return to * t + from * (1.0 - t);
}
public static double Gamma(double value, double absmax, double gamma) {
bool flag = false;
if (value < 0.0)
flag = true;
double num1 = Mathd.Abs(value);
if (num1 > absmax) {
if (flag)
return -num1;
else
return num1;
} else {
double num2 = Mathd.Pow(num1 / absmax, gamma) * absmax;
if (flag)
return -num2;
else
return num2;
}
}
public static bool Approximately(double a, double b) {
return Mathd.Abs(b - a) < Mathd.Max(1E-06d * Mathd.Max(Mathd.Abs(a), Mathd.Abs(b)), 1.121039E-44d);
}
public static double SmoothDamp(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed) {
double deltaTime = (double)Time.deltaTime;
return Mathd.SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
}
public static double SmoothDamp(double current, double target, ref double currentVelocity, double smoothTime) {
double deltaTime = Time.deltaTime;
double maxSpeed = double.PositiveInfinity;
return Mathd.SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
}
public static double SmoothDamp(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed, double deltaTime) {
smoothTime = Mathd.Max(0.0001d, smoothTime);
double num1 = 2d / smoothTime;
double num2 = num1 * deltaTime;
double num3 = (1.0d / (1.0d + num2 + 0.479999989271164d * num2 * num2 + 0.234999999403954d * num2 * num2 * num2));
double num4 = current - target;
double num5 = target;
double max = maxSpeed * smoothTime;
double num6 = Mathd.Clamp(num4, -max, max);
target = current - num6;
double num7 = (currentVelocity + num1 * num6) * deltaTime;
currentVelocity = (currentVelocity - num1 * num7) * num3;
double num8 = target + (num6 + num7) * num3;
if (num5 - current > 0.0 == num8 > num5) {
num8 = num5;
currentVelocity = (num8 - num5) / deltaTime;
}
return num8;
}
public static double SmoothDampAngle(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed) {
double deltaTime = (double)Time.deltaTime;
return Mathd.SmoothDampAngle(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
}
public static double SmoothDampAngle(double current, double target, ref double currentVelocity, double smoothTime) {
double deltaTime = (double)Time.deltaTime;
double maxSpeed = double.PositiveInfinity;
return Mathd.SmoothDampAngle(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
}
public static double SmoothDampAngle(double current, double target, ref double currentVelocity, double smoothTime, double maxSpeed, double deltaTime) {
target = current + Mathd.DeltaAngle(current, target);
return Mathd.SmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, deltaTime);
}
public static double Repeat(double t, double length) {
return t - Mathd.Floor(t / length) * length;
}
public static double PingPong(double t, double length) {
t = Mathd.Repeat(t, length * 2d);
return length - Mathd.Abs(t - length);
}
public static double InverseLerp(double from, double to, double value) {
if (from < to) {
if (value < from)
return 0d;
if (value > to)
return 1d;
value -= from;
value /= to - from;
return value;
} else {
if (from <= to)
return 0d;
if (value < to)
return 1d;
if (value > from)
return 0d;
else
return (1.0d - (value - to) / (from - to));
}
}
public static double DeltaAngle(double current, double target) {
double num = Mathd.Repeat(target - current, 360d);
if (num > 180.0d)
num -= 360d;
return num;
}
internal static bool LineIntersection(Vector2d p1, Vector2d p2, Vector2d p3, Vector2d p4, ref Vector2d result) {
double num1 = p2.x - p1.x;
double num2 = p2.y - p1.y;
double num3 = p4.x - p3.x;
double num4 = p4.y - p3.y;
double num5 = num1 * num4 - num2 * num3;
if (num5 == 0.0d)
return false;
double num6 = p3.x - p1.x;
double num7 = p3.y - p1.y;
double num8 = (num6 * num4 - num7 * num3) / num5;
result = new Vector2d(p1.x + num8 * num1, p1.y + num8 * num2);
return true;
}
internal static bool LineSegmentIntersection(Vector2d p1, Vector2d p2, Vector2d p3, Vector2d p4, ref Vector2d result) {
double num1 = p2.x - p1.x;
double num2 = p2.y - p1.y;
double num3 = p4.x - p3.x;
double num4 = p4.y - p3.y;
double num5 = (num1 * num4 - num2 * num3);
if (num5 == 0.0d)
return false;
double num6 = p3.x - p1.x;
double num7 = p3.y - p1.y;
double num8 = (num6 * num4 - num7 * num3) / num5;
if (num8 < 0.0d || num8 > 1.0d)
return false;
double num9 = (num6 * num2 - num7 * num1) / num5;
if (num9 < 0.0d || num9 > 1.0d)
return false;
result = new Vector2d(p1.x + num8 * num1, p1.y + num8 * num2);
return true;
}
}
}