Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Collection of mathy math functions for mathing and animating
export type float = number;
/**
* Interpolate a value with specified extrema, to a new value between new extrema.
*
* @param value the current value
* @param inputMin minimum the input value can reach
* @param inputMax maximum the input value can reach
* @param outputMin minimum the output value can reach
* @param outputMax maximum the output value can reach
*/
export function map(value: float, inputMin: float, inputMax: float, outputMin: float, outputMax: float): float {
return outputMin + (outputMax - outputMin) * ((value - inputMin) / (inputMax - inputMin));
}
/**
* Same as {@link #map}, but clamps the values so they never exceed the extrema.
*/
export function mapClamped(value: float, inputMin: float, inputMax: float, outputMin: float, outputMax: float): float {
return Math.max(outputMin, Math.min(outputMax, map(value, inputMin, inputMax, outputMin, outputMax)));
}
/***
* Interpolate between values a and b.
*/
export function lerp(a: float, b: float, fraction: float): float {
return (a * (1.0 - fraction)) + (b * fraction);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment