-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathExtensions.cs
More file actions
88 lines (80 loc) · 3.31 KB
/
Copy pathMathExtensions.cs
File metadata and controls
88 lines (80 loc) · 3.31 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
81
82
83
84
85
86
87
88
namespace asim.unity.extensions
{
public static class MathExtensions
{
//====================================================================================================
//====================================================================================================
/// <summary>
/// Remap the current value from a current range to another range
/// e.g remap value of 0.5f with range of 0-1 to range of 0-255 will return 127.5f
///
/// newValue = [new range] * [value in percentage based on old range]
/// 1. calculate old range
/// 2. calculate value in terms of percentage based on old range
/// 3. calculate newValue using percentage on new range
/// </summary>
public static float Remap(this float value,
float fromMin, float fromMax, float toMin, float toMax)
{
return toMin + (toMax - toMin) * ((value - fromMin) / (fromMax - fromMin));
}
/// <summary>
/// Remap the current value from a current range to another range
/// e.g remap value of 0.5 with range of 0-1 to range of 0-255 will return 127.5
///
/// newValue = [new range] * [value in percentage based on old range]
/// 1. calculate old range
/// 2. calculate value in terms of percentage based on old range
/// 3. calculate newValue using percentage on new range
/// </summary>
public static double Remap(this double value,
double fromMin, double fromMax, double toMin, double toMax)
{
return toMin + (toMax - toMin) * ((value - fromMin) / (fromMax - fromMin));
}
//====================================================================================================
//====================================================================================================
/// <summary>
/// modulus that works with negative numbers
/// e.g mod(-2,5) = 3 , e.g mod(-2,-5) = 3
/// </summary>
public static int mod(int a, int b)
{
int m = a % b;
if (m < 0)
{
m = (b < 0) ? m - b : m + b;
}
return m;
}
/// <summary>
/// modulus that works with negative divisors and returns based on negative number
/// e.g mod(2,-5) = -3
/// </summary>
public static int nagativemod(int a, int b)
{
int m = a % b;
if (m < 0)
{
m = (b < 0) ? m - b : m + b;
}
return m;
}
//====================================================================================================
//====================================================================================================
/// <summary>
/// Count the number between indices in a list, looped
/// </summary>
public static int CountFromToIndexLoop(int start, int end, int listCount)
{
int count = end - start;
if (count < 0) count += listCount;
return count;
}
public static int IndexLoop(int index, int max)
{
if (index < 0) return index + max;
return index % max;
}
}
}