Is there one I’m missing that maps one range to another - a.k.a this?
// maps one range to another.
// e.g. I have one range at 2-12 and I want it in the range 0-FastMath.PI.
private float map(float value, float oldMin, float oldMax, float newMin, float newMax) {
return (((value - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin;
}
I am not a math guy, but yep I think maybe good to include it.
I remember once I also needed this mapping functionality, but I couldn’t find it FastMath.
I just found one that was doing a normalizing:
/**
* Converts one range into another.
* Interpolates between newMin and newMax with the given value between oldMin and oldMax.
* @param value the value between oldMin and oldMax.
* @param oldMin the minimum old value.
* @param oldMax the maximum old value.
* @param newMin the minimum new value.
* @param newMax the maximum new value.
* @return
*/
private float interpolateLinear(float value, float oldMin, float oldMax, float newMin, float newMax) {
return (((value - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin;
}
In p5.js they do the same as me except worded better. I guess I could use a couple vec2’s or a vec4 as the ranges? I’m not sure how I could lessen it other than that unless something obvious is escaping me.
It’s not about the raw argument count… and you certainly don’t want to be creating totally-completely-100% unrelated objects just to avoid a raw argument count.
To me, it just seems that glomming on a bunch of arguments to a “simple math” function is a sign that it’s not a “simple math” function… but more than one.
lerp(unlerp(x, min, max), newMin, newMax)
The fact that we can’t come up with a good name for it is also a sign that it’s overloaded… but so far to me “reinterpolate” is the best name.
The problem with “map” is that it means 100 other things directly related to games and software development that are not even reasonably close to the meaning of this function.
/**
* Converts a range of min/max to a 0-1 range.
* @param value the value between min-max (inclusive).
* @param min the minimum of the range.
* @param max the maximum of the range.
* @return A value between 0-1 if the given value is between min/max.
*/
public static float unInterpolateLinear(float value, float min, float max) {
return (value - min) / (max - min);
}
Its usually just called unlerp from what I’ve seen. In the jme lib it’s written longhand - interpolateLinear - instead of lerp -, so I just went with that. I don’t mind what it’s called, but I understand completely that it’s important if it’s going to be there forever.