I'm pretty sure it's a common pattern, but I'm looking for the name of the pattern where you match one range of numbers to another range of numbers. Something similar like:
Map(from1: 60, to1: 90, from2: 100, to2: 140, value: 75); // Result: 120 (middle of from2/to2)
Map(from1: 60, to1: 90, from2: 100, to2: 140, value: 30); // Result: 100 (clamped bottom)
Map(from1: 60, to1: 90, from2: 100, to2: 140, value: 60); // Result: 100 (bottom)
Map(from1: 60, to1: 90, from2: 100, to2: 140, value: 500); // Result: 140 (clamped to2)
Map(from1: 60, to1: 90, from2: 100, to2: 140, value: 85); // Result: 133.33 (in between)
What is the name for this method? I'm specifically looking for a solution in Unity, but I'm pretty sure if I know the name of the pattern I can find it.
Ah never mind - I found it in a Unreal Engine tutorial:
https://www.youtube.com/watch?v=11T4UvkVYb8
The term used here is MapRangeClamped and MapRangeUnclamped.
Here is the code for C#:
public static class FloatExtensions
{
public static float MapRange(this float value, float from1, float to1, float from2, float to2, bool clamp = false)
{
if ( clamp )
{
if ( value < from1 )
return from2;
if ( value > to1 )
return to2;
}
return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
}
}