Created
May 19, 2013 02:06
-
-
Save rtanote/5606386 to your computer and use it in GitHub Desktop.
processingのmap関数をC#で使えるように。ジェネリックメソッド&拡張メソッドにしている。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class MiscUtils | |
{ | |
/// <summary> | |
/// Re-maps a number from one range to another. | |
/// </summary> | |
/// <typeparam name="Type">IConvertible-implementing type</typeparam> | |
/// <param name="value">the incoming value to be converted</param> | |
/// <param name="start1">lower bound of the value's current range</param> | |
/// <param name="stop1">upper bound of the value's current range</param> | |
/// <param name="start2">lower bound of the value's target range</param> | |
/// <param name="stop2">upper bound of the value's target range</param> | |
/// <returns></returns> | |
public static Type Map<Type>(this Type value, Type start1, Type stop1, Type start2, Type stop2) where Type : IConvertible { | |
double mappedValue | |
= start2.ToDouble(NumberFormatInfo.CurrentInfo) | |
+ (stop2.ToDouble(NumberFormatInfo.CurrentInfo) - start2.ToDouble(NumberFormatInfo.CurrentInfo)) | |
* ((value.ToDouble(NumberFormatInfo.CurrentInfo) - start1.ToDouble(NumberFormatInfo.CurrentInfo)) | |
/ (stop1.ToDouble(NumberFormatInfo.CurrentInfo) - start1.ToDouble(NumberFormatInfo.CurrentInfo))); | |
return (Type)Convert.ChangeType(mappedValue, typeof(Type)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment