Nodejs Number Clamp clamp(min, max)

Here you can find the source of clamp(min, max)

Method Source Code

window.rnd = function(p1, p2) {
        var r = Math.random();
        if (Array.isArray(p1)) {
            return p1[Math.floor(r * p1.length)];
        }//from  ww w.  ja  va2  s.c  o  m

        if (!(p1 === undefined)) {
            if (!(p2 === undefined)) {
                r = r * (p2 - p1) + p1;
            } else {
                r = r * 2 * p1 - p1;
            }
        }
        return r;
};

Number.prototype.clamp = function(min, max) {
  return Math.min(Math.max(this, min), max);
};


window.map = function(value, min1, max1, min2, max2){
  return min2 + (max2 - min2) * ((value - min1) / (max1 - min1))
}

Related

  1. clamp(min, max)
    Number.prototype.clamp = function (min, max) {
      return this<min?min:this>max?max:this;
    };
    
  2. clamp(min, max)
    Number.prototype.clamp = function(min, max) {
      return Math.min(Math.max(this, min), max);
    };
    function calcAngle(x1, x2, y1, y2) {
      var calcAngle = Math.atan2(x1-x2,y1-y2)*(180/Math.PI);
      if(calcAngle < 0)
        calcAngle = Math.abs(calcAngle);
      else
        calcAngle = 360 - calcAngle;
    ...
    
  3. clamp(min, max)
    'use strict';
    Number.prototype.clamp = function(min, max) {
      return Math.min(Math.max(this, min), max);
    };
    
  4. clamp(min, max)
    Number.prototype.clamp = function(min, max) {
      return Math.min(Math.max(this, min), max);
    };
    
  5. clamp(min, max)
    Number.prototype.clamp = function(min, max) {
      return Math.min(Math.max(this, min), max);
    };
    
  6. clamp(min, max)
    Number.prototype.clamp = function(min, max) {
      return Math.min(Math.max(this, min), max);
    };
    collides = function(a, b) {
        return a.x < b.x + b.width &&
          a.x + a.width > b.x &&
          a.y < b.y + b.height &&
          a.y + a.height > b.y;
    
  7. clamp(min, max)
    Number.prototype.clamp = function(min, max)
      return Math.min(Math.max(this, min), max);
    };
    function sortNumber(a,b) {
        return a - b;
    var sortObjectByKey = function(obj)
        var keys = [];
        var sorted_obj = {};
        for(var key in obj){
            if(obj.hasOwnProperty(key)){
                keys.push(key);
        keys.sort(sortNumber);
        jQuery.each(keys, function(i, key){
            sorted_obj[key] = obj[key];
        });
        return sorted_obj;
    };
    var pickRandomProperty = function(obj)
        var result;
        var count = 0;
        for (var prop in obj)
            if (Math.random() < 1/++count)
                result = prop;
        return result;
    
  8. clamp(min,max)
    Number.prototype.clamp = function(min,max)
      var c = this;
      if(c < min)
        c = min;
      if(c > max)
        c = max;
      return Number(c);
    };
    ...
    
  9. clamp(min,max)
    Number.prototype.clamp = function(min,max)
      var c = this;
      if(c < min)
        c = min;
      if(c > max)
        c = max;
      return Number(c);
    };
    ...