Skip to content

Instantly share code, notes, and snippets.

@wbbradley
Created September 18, 2013 06:40
Show Gist options
  • Save wbbradley/6605348 to your computer and use it in GitHub Desktop.
Save wbbradley/6605348 to your computer and use it in GitHub Desktop.
Javascript math helpers
Math.lerp = Math.lerp || function(a, p1, p2) {
return (p2 - p1) * a + p1;
};
Math.alpha = Math.alpha || function(a, p1, p2) {
return (a - p1) / (p2 - p1);
};
Math.clamp = Math.clamp || function(a, p1, p2) {
if (p2 > p1) {
if (a < p1) {
return p1;
} else if (a > p2) {
return p2;
} else {
return a;
}
} else {
if (a < p2) {
return p2;
} else if (a > p1) {
return p1;
} else {
return a;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment