/*jslint plusplus: true, white: true, indent: 2, maxlen: 90 */
/*global HTMLCanvasElement, document */
/**
* @author Wolfgang Kowarschick
* @copyright 2013, Wolfgang Kowarschick
* <br/>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted under the terms of the
* Creative Commons License Attribution-NonCommercial-ShareAlike 3.0 Unported
* (CC BY-NC-SA 3.0: http://creativecommons.org/licenses/by-nc-sa/3.0/).
*/
(function($wk$)
{ "use strict";
////////////////////////////////////////////////////////////////////////////////
// Class "Math"
////////////////////////////////////////////////////////////////////////////////
/**
* @class
* @name $wk$.Math
*/
/**
* Computes a random integer number within an interval.
* @method
* @name $wk$.Math.randomInt
* @param {Number | Object} p_interval
* @returns An integer number within the interval
* [<code>p_interval.min</code>, <code>p_interval.max</code>].
*/
Math.randomInt =
function(p_interval)
{ if (p_interval instanceof Object)
{ var l_min = p_interval.min, l_max = p_interval.max;
return l_min+Math.floor(Math.random()*(l_max-l_min+1));
}
else if (p_interval instanceof String)
{ return Math.floor(parseInt(p_interval, 10)); }
else
{ return Math.floor(p_interval); }
};
/**
* Computes a random number within an interval.
* @method
* @name $wk$.Math.randomNum
* @param {Number | Object} p_interval
* @returns A number within the interval
* [<code>p_interval.min</code>, <code>p_interval.max</code>].
*/
Math.randomNum =
function(p_interval)
{ if (p_interval instanceof Object)
{ var l_min = p_interval.min, l_max = p_interval.max;
return l_min+Math.random()*(l_max-l_min+1);
}
else if (p_interval instanceof String)
{ return Math.floor(parseFloat(p_interval)); }
else
{ return p_interval; }
};
/**
* Returns a value. If only a value <code>p_value</code> is passed
* that value is returned.
* If, on the other hand, an array <code>p_value</code> and a position
* <code>p_value</code> are passed, the value
* <code>p_value[Math.min(p_pos, p_value.length-1)]</code> is returned.
* @method
* @name $wk$.Math.value
* @param {Object} p_value
* @param {int} [p_pos]
* @returns A value.
*/
Math.value =
function(p_value, p_pos)
{ var p_is_array = p_value instanceof Array,
p_length;
if (p_pos === undefined || !p_is_array)
{ return p_value; }
p_length = p_value.length;
if (p_pos >= p_length)
{ p_pos = p_length-1; }
return p_value[p_pos];
};
$wk$.Math = Math;
////////////////////////////////////////////////////////////////////////////////
//End of Class "Math"
////////////////////////////////////////////////////////////////////////////////
}(this.$wk$));