This repository was archived by the owner on Jun 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathExtensions.cs
More file actions
80 lines (69 loc) · 2.22 KB
/
Copy pathMathExtensions.cs
File metadata and controls
80 lines (69 loc) · 2.22 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
#nullable enable
namespace System
{
public static class MathExtensions
{
public static float Map(this float value, float inputFrom, float inputTo, float outputFrom, float outputTo) =>
(value - inputFrom) / (inputTo - inputFrom) * (outputTo - outputFrom) + outputFrom;
public static double Map(this double value, double inputFrom, double inputTo, double outputFrom, double outputTo) =>
(value - inputFrom) / (inputTo - inputFrom) * (outputTo - outputFrom) + outputFrom;
public static float Lerp(this float value, float outputFrom, float outputTo) =>
(1 - value) * outputFrom + value * outputTo;
public static double Lerp(this double value, double outputFrom, double outputTo) =>
(1 - value) * outputFrom + value * outputTo;
public static float Clamped(this float value, float min = 0.0f, float max = 1.0f) =>
Math.Clamp(value, min, max);
public static double Clamped(this double value, double min = 0.0, double max = 1.0) =>
Math.Clamp(value, min, max);
/// <summary>
/// Returns positive mod of value
/// </summary>
public static float Mod(this float value, float mod)
{
float res = value % mod;
if (res < 0) res += mod;
return res;
}
/// <summary>
/// Returns positive mod of value
/// </summary>
public static double Mod(this double value, double mod)
{
double res = value % mod;
if (res < 0) res += mod;
return res;
}
/// <summary>
/// Returns positive mod of value
/// </summary>
public static int Mod(this int value, int mod)
{
int res = value % mod;
if (res < 0) res += mod;
return res;
}
/// <summary>
/// Returns mod of value between -mod/2 and mod/2
/// </summary>
public static float ModAround(this float value, float mod)
{
float res = value.Mod(mod);
if (res > mod / 2)
res -= mod;
return res;
}
/// <summary>
/// Returns mod of value between -mod/2 and mod/2
/// </summary>
public static double ModAround(this double value, double mod)
{
double res = value.Mod(mod);
if (res > mod / 2)
res -= mod;
return res;
}
public static int Add(int a, int b) => a + b;
public static float Add(float a, float b) => a + b;
public static double Add(double a, double b) => a + b;
}
}