Skip to content

Instantly share code, notes, and snippets.

@ayamflow
Created February 4, 2016 16:38
Show Gist options
  • Save ayamflow/d5f24c87ab0446d65d4e to your computer and use it in GitHub Desktop.
Save ayamflow/d5f24c87ab0446d65d4e to your computer and use it in GitHub Desktop.
Math extra
// Point position on a line
// (see also https://github.com/ayamflow/point-in-line)
pointInLine: function(p1, p2, t)
return {
p1.x + t * (p2.x - p1.x)
p1.y + t * (p2.y - p1.y)
};
}
clamp: function(value, min, max) {
return Math.max(min, Math.min(max, value));
}
norm: function(value, min, max) {
return (value - min) / (max - min);
}
lerp: function(ratio, start, end) {
return start + (end - start) * ratio;
}
map: function(value, sourceMin, sourceMax, destMin, destMax) {
return lerp(norm(value, sourceMin, sourceMax), destMin, destMax);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment