Подтвердить что ты не робот

Как сопоставить числа на С#, например, с картой в Arduino?

Есть ли такая функция, как Arduino map в С#?

4b9b3361

Ответ 1

Вы можете сделать это с помощью метода расширения (например, для decimal):

public static class ExtensionMethods
{
    public static decimal Map (this decimal value, decimal fromSource, decimal toSource, decimal fromTarget, decimal toTarget)
    {
        return (value - fromSource) / (toSource - fromSource) * (toTarget - fromTarget) + fromTarget;
    }
}

Затем вы можете использовать его как:

decimal res = 2.Map(1, 3, 0, 10);
// res will be 5

Ответ 2

private static int map(int value, int fromLow, int fromHigh, int toLow, int toHigh) 
{
    return (value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow;
}