public static decimal Map (this decimal value, decimal fromSource, decimal toSource, decimal fromTarget, decimal toTarget)
{
return (value - fromSource) / (toSource - fromSource) * (toTarget - fromTarget) + fromTarget;
}
Color lerp(Color a, Color b, float t)
{
t = clamp01(t);
return new Color(
a.r + (b.r - a.r) * t,
a.g + (b.g - a.g) * t,
a.b + (b.b - a.b) * t,
a.a + (b.a - a.a) * t
);
}
- constrain value (with min, max)
- SubArray
public static T[] SubArray<T>(this T[] data, int index, int length)
{
T[] result = new T[length];
Array.Copy(data, index, result, 0, length);
return result;
}
static void Main()
{
int[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] sub = data.SubArray(3, 4); // contains {3,4,5,6}
}
array.Skip(3).Take(5).ToArray();
- permutation (all, Next, Prev)
public static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length)
{
if (length == 1) return list.Select(t => new T[] { t });
return GetPermutations(list, length - 1)
.SelectMany(t => list.Where(e => !t.Contains(e)),
(t1, t2) => t1.Concat(new T[] { t2 }));
}
int Factorial(int i)
{
if (i <= 1)
return 1;
return i * Factorial(i - 1);
}
- Enumerable.Range with step, similar to python one
- string.Rerlace (stringBuilder)
two char arrays, go through cycle, change one letter from first array to another. maybe use Dictionary
two char arrays, go through cycle, change one letter from first array to another. maybe use Dictionary