pauloabmiranda
1/12/2016 - 12:54 PM

MathUtils.js

MathUtils = {
    TO_RADIANS: Math.PI / 180,
    TO_DEGREES: 180 / Math.PI,
    
    limit: function (value, min, max) {
        return value < min ? min : value > max ? max : value;
    },
  
    normalize: function(value, min, max) {
        return (value - min) / (max - min);
    },
  
    interpolate: function(normValue, min, max) {
        return min + (max - min) * normValue;
    },
  
    map: function(value, min1, max1, min2, max2, p_limit) {
        if (p_limit) {
            return this.interpolate(this.normalize(this.limit(value, min1, max1), min1, max1), min2, max2);
        } else {
            return this.interpolate(this.normalize(value, min1, max1), min2, max2);
        }
    },
  
    random: function(min, max) {
        return Math.random() * (max - min) + min;
    }
};