Skip to content

Instantly share code, notes, and snippets.

@odbol
Created July 15, 2022 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odbol/7c4bfb51f9a32c02c5ed28c0b9e31351 to your computer and use it in GitHub Desktop.
Save odbol/7c4bfb51f9a32c02c5ed28c0b9e31351 to your computer and use it in GitHub Desktop.
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