ofMapみたいなMapping&Scaling

ofMapのように、任意の数値を定められた数値範囲変換(スケーリング、数学的には線形写像というらしい)をC#でできるようにする。
独自のユーティリティクラスを作る方法もあるが、既存のfloat型を拡張する形での実装方法。

以下のコードを、Map.csなどとして、スクリプトを置くフォルダ(ex. /Scripts)に保存する

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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

}

使用方法は以下の通り

float foo = 0.5f;
float result = foo.Map( 0.0f, 1.0f, 0, 100 );
//50

Via! https://stackoverflow.com/questions/14353485/how-do-i-map-numbers-in-c-sharp-like-with-map-in-arduino