Javascript Number clamp(min, max)

Description

Javascript Number clamp(min, max)

/**//from  w  ww  . j a v  a  2 s .co  m
 * Returns a number whose value is limited to the given range.
 *
 * @method Number.prototype.clamp
 * @param {Number} min The lower boundary
 * @param {Number} max The upper boundary
 * @return {Number} A number in the range (min, max)
 */
Number.prototype.clamp = function(min, max) {
    return Math.min(Math.max(this, min), max);
};

Javascript Number clamp(min, max)

// NUMBER//from  w  w w  . ja va 2  s . c  om

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

Javascript Number clamp(min, max)

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

Javascript Number clamp(min, max)

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

function randomFloat(min, max) {
  return min + Math.random() * (max - min);
}

Javascript Number clamp(min, max)

/**//  w w w  .ja v a 2 s. c  om
 *  Number#clamp(min, max) -> Number
 *  - min (Number): The minimum allowable value.
 *  - max (Number): The maximum allowable value.
 *
 *  Returns the `min` or `max` value if the number is outside the specified range.
**/
Number.prototype.clamp = function(min, max) {
  // Via http://twitter.com/kangax/status/13023629344
  return this < min ? min : (this > max ? max : this);
};

Javascript Number clamp(min, max)

/**/* w  w  w. ja  v a 2s.  c om*/
 * Returns a number whose value is limited to the given range.
 *
 * Example: limit the output of this computation to between 0 and 255
 * (x * 255).clamp(0, 255)
 *
 * @param {Number} min The lower boundary of the output range
 * @param {Number} max The upper boundary of the output range
 * @returns A number in the range [min, max]
 * @type Number
 */
Number.prototype.clamp = function(min, max) {
  return Math.min(Math.max(this, min), max);
};

Javascript Number clamp(min, max)

/**//from  w ww  . ja v a 2 s .  c  o  m
 * @author Zoho
 * A lot of this code is boilerplate and/or reused from various sources
 */

//Doug Crockford's Inheritance system for non-ecma5 browsers
if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}

Number.prototype.clamp = function(min, max) {
  return Math.min(Math.max(this, min), max);
};
// 
// if(typeof Math.clamp !== 'function') {
 // Math.clamp = function (value, min, max) {
  // return Math.max(0, Math.min(value, 100));
 // };
// }

Javascript Number clamp(min, max)

Number.prototype.clamp = function(min, max) {
 return (this < min ? min : (this > max ? max : this));
};

Javascript Number clamp(min, max)

/**/*from  www.  ja va2  s. c  om*/
 * Returns a number whose value is limited to the given range.
 *
 * Example: limit the output of this computation to between 0 and 255
 * (x * 255).clamp(0, 255)
 *
 * @param {Number} min The lower boundary of the output range
 * @param {Number} max The upper boundary of the output range
 * @returns A number in the range [min, max]
 * @type Number
 */
Number.prototype.clamp = function(min, max){
  return Math.min(Math.max(this, min), max)
}// JavaScript Document

Javascript Number clamp(min, max)

/**//from w w w  .ja  v a  2s .c om
 * Misc helper methods
 */

/**
 * accepts a min and max value and determines
 * 1. is the number or the min greater
 * 2. is the result of 1 or the max smaller
 * helps with positioning in the canvas element
 *
 * @param  {[number]} min [min value]
 * @param  {[number]} max [max value]
 * @return {[number]}     [returns result of 2. above]
 */
Number.prototype.clamp = function (min, max) {
  return Math.min(Math.max(this, min), max);
};

/**
 * Generate a random number
 */
function randomFloat(min, max) {
  return min + Math.random() * (max - min);
}

Javascript Number clamp(min, max)

/** helpers.js **/
/** HELPER FUNCTIONS **/


function collides(a, b) {
 return a.x < b.x + b.width &&
  a.x + a.width > b.x &&/*from ww w.j av a2s .  c om*/
  a.y < b.y + b.height &&
  a.y + a.height > b.y;
}

/**
 * Returns a number whose value is limited to the given range.
 *
 * Example: limit the output of this computation to between 0 and 255
 * (x * 255).clamp(0, 255)
 *
 * @param {Number} min The lower boundary of the output range
 * @param {Number} max The upper boundary of the output range
 * @returns A number in the range [min, max]
 * @type Number
 * Source: http://stackoverflow.com/a/11409944/1689605
 */
Number.prototype.clamp = function(min, max) {
  return Math.min(Math.max(this, min), max);
};

Javascript Number clamp(min, max)

// More math functions
Math.toRadians = function(d) { return d * Math.toR; }
Math.toDegrees = function(r) { return r * Math.toD; }

Math.euclidean = function(pos0,pos1) {
  d1 = pos0.x - pos1.x;/*  w ww. j a  v  a2 s.  c  o  m*/
  d2 = pos0.y - pos1.y;
  return Math.sqrt(d1*d1 + d2*d2);
}

Number.prototype.clamp = function(min, max) {
  return Math.max(Math.min(this, max), min);
  /*return this > max ? max : this < min ? min : this;*/
}

Math.manhattan = function(pos0,pos1) {
  d1 = Math.abs(pos0.x - pos1.x);
  d2 = Math.abs(pos0.y - pos2.y);
  return d1 + d2;
}

// Math constants
Math.HALF_PI      = Math.PI/2;
Math.PI2          = Math.PI*2;
Math.PY_CONST     = Math.sqrt(2);
Math.toD          = 180 / Math.PI;
Math.toR          = Math.PI / 180;

Javascript Number clamp(min, max)

/* file:Number.js Number extensions 
@author: Richard Assar, Daniel */

// Returns the given number clamped between min and max
Number.prototype.clamp = function(min, max) {
 if(this < min) {
  return min;// w w w .jav a 2  s.com
 } else if(this > max) {
  return max;
 } else {
  return this;
 }
};

Javascript Number clamp(min,max)



// @Deprecated: no direct replacement, since not used in core code
// Clamp a number to a range
Number.prototype.clamp = function(min,max)
{
 var c = this;//from   www .j  a v a 2  s  .  c o m
 if(c < min)
  c = min;
 if(c > max)
  c = max;
 return Number(c);
};



PreviousNext

Related