Посмотрите на это:
Код: Выделить всё
void Main()
{
Interpolation.LinearBetween((0, 5), (10, 3), 7).DumpIt();
Interpolation.LinearBetween((0.0f, 5), (10, 3), 7).RoundToInt().DumpIt();
}
public static class Interpolation
{
public static T LinearBetween((T x1, T y1) a, (T x2, T y2) b, T x) where T : INumber
{
//f(x) = m*x + n
//one oft operands needs to be floating point type
var m = (b.y2 - a.y1) / (b.x2 - a.x1);
var n = b.y2 - m * b.x2;
return (x * m + n);
}
public static int RoundToInt(this float value)
{
return (int)Math.Round(value);
}
public static void DumpIt(this object toDump)
{
Console.WriteLine(toDump);
}
}
Изменить: перефразировать вопрос.
Подробнее здесь: https://stackoverflow.com/questions/791 ... 7-or-newer
Мобильная версия