标签:ESS range tom together end scale lam tin type
lerp is the acronym for linear interpolation.the idea is very simple , you have 2 values, and you want to "walk" between those values by a factor.
fun lerp = (start, end, factor) => start * (1 - factor) + end * factor
lerp(20, 80, 0) // 20
lerp(20, 80, 1) // 80
lerp(20, 80, 0.5) // 40
- if you pass a factor 0 , you are pointing to eh beginning of the walk so the value is equal the start .
- if you pass a factor of 1 , you are pointing to the end of the walk so the value is equal to the end.
- any factor between 0 and 1 will add a (1 - factor) of start argument and a factor of end argument.(e.g with start 0 and end 10 with a factor 0.5 you will have 5 , so the half of the path).
fun invlerp = (start, end, value) => (value - start) / (end - start)
- this function is the opposite of lerp. instead of a range and a factor, we give a range and a value to find out the factor.
invlerp(50, 100, 75) // 0.5
invlerp(50, 100, 25) // 0
invlerp(50, 100, 125) // 1
fun mapvalue = (value, frommin, frommax, tomin, tomax) => {
const factor = normalize(frommin, frommax, value);
return lerp(tomain, tomax, factor);
}
- as you can see and guess it converts a value form the scale[frommain, frommax] to a value from the scale[tomin, tomax. it‘s just the normalize and lerp function working together.
fun clamp = (value, min, max) => math.min(math.max(value, min, max), max);
clamp(24, 20, 30) // 24
clamp(12, 20, 30) // 20
clamp(32, 20, 30) // 30
- it limits the value to the scale[min, max] any value that is more than the max will turn max, if it‘s less than min will turn min.
note : notices how lerp, normalize and mapvalue isn‘t constrained by the factor or the scale range. you can get factors outside the [0,1] range and flipped min and max ranges . you can do for example a mapvalue(value,0,20,20,0) inverting the scale (simple but powerful).
this final method is ace. it’s a one-liner that converts a value from one data range to another. that might sound a bit arbitrary, but it’s surprisingly useful. we pass in two data ranges and a value that sits within data range one (it will still be clamped).
const range = (x1, y1, x2, y2, a) => lerp(x2, y2, invlerp(x1, y1, a));
// range 1 range 2 value
range(10, 100, 2000, 20000, 50) // 10000
const lerp = (x: number, y: number, a: number) => x * (1 - a) + y * a;
const invlerp = (x: number, y: number, a: number) => clamp((a - x) / (y - x));
const clamp = (a: number, min = 0, max = 1) => math.min(max, math.max(min, a));
const range = (
x1: number,
y1: number,
x2: number,
y2: number,
a: number
) => lerp(x2, y2, invlerp(x1, y1, a));
linear interpolation functions
the simple yet power math we don‘t talk about
Simple Linear Interpolation Functions
标签:ESS range tom together end scale lam tin type
原文地址:https://www.cnblogs.com/xiaoluo123/p/14840166.html