Created
February 4, 2016 16:38
-
-
Save ayamflow/d5f24c87ab0446d65d4e to your computer and use it in GitHub Desktop.
Math extra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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