Skip to content

Instantly share code, notes, and snippets.

@Martin-Pitt
Created September 7, 2017 14:20
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 Martin-Pitt/6b05f2fed36ee1f3dc4c5b8f8407eac7 to your computer and use it in GitHub Desktop.
Save Martin-Pitt/6b05f2fed36ee1f3dc4c5b8f8407eac7 to your computer and use it in GitHub Desktop.
A very simple but powerful primitive that lets you make awesome UI or 2D/3D movement very quickly
function rescale(from_min, from_max, to_min, to_max, from) {
return to_min + ((to_max-to_min) * ((from_min-from) / (from_min-from_max)));
}
rescale.clamped = function(fMin, fMax, tMin, tMax, f) {
f = this(fMin, fMax, tMin, tMax, f);
if(tMin < tMax) { if(f < tMin) return tMin; else if(f > tMax) return tMax; }
else { if(f < tMax) return tMax; else if(f > tMin) return tMin; }
return f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment