25 lines
493 B
JavaScript
25 lines
493 B
JavaScript
module.exports = limit;
|
|
|
|
/**
|
|
* Limit the magnitude of this vector to the value used for the `max`
|
|
* parameter.
|
|
*
|
|
* @param {vec2} the vector to limit
|
|
* @param {Number} max the maximum magnitude for the vector
|
|
* @returns {vec2} out
|
|
*/
|
|
function limit(out, a, max) {
|
|
var mSq = a[0] * a[0] + a[1] * a[1];
|
|
|
|
if (mSq > max * max) {
|
|
var n = Math.sqrt(mSq);
|
|
out[0] = a[0] / n * max;
|
|
out[1] = a[1] / n * max;
|
|
} else {
|
|
out[0] = a[0];
|
|
out[1] = a[1];
|
|
}
|
|
|
|
return out;
|
|
}
|