Math.MinMax classes added

This commit is contained in:
Richard Davey 2017-10-17 21:32:00 +01:00
parent 61c8f63168
commit f13043ee63
3 changed files with 313 additions and 0 deletions

123
v3/src/math/MinMax2.js Normal file
View file

@ -0,0 +1,123 @@
var Between = require('./Between');
var FloatBetween = require('./FloatBetween');
var Class = require('../utils/Class');
var Percent = require('./Percent');
var Wrap = require('./Wrap');
// A Helper Class that allows you to specify a range between min and max, and then
// keep the value within those bounds, or get random ints or floats from the range.
var MinMax2 = new Class({
initialize:
function MinMax2 (min, max)
{
if (min === undefined) { min = 0; }
if (max === undefined) { max = min; }
this.min = min;
this.max = max;
this._current = min;
},
set: function (min, max)
{
if (min === undefined) { min = 0; }
if (max === undefined) { max = min; }
this.min = min;
this.max = max;
return this;
},
clone: function ()
{
return new MinMax2(this.min, this.max);
},
copy: function (dest)
{
dest.min = this.min;
dest.max = this.max;
return this;
},
copyXY: function (dest)
{
dest.x = this.min;
dest.y = this.max;
return this;
},
// Given U (a value between 0 and 1) return the value in the range
getU: function (u)
{
// TODO
},
// Returns a value between 0 and 1 based on value
getPercent: function (value)
{
return Percent(value, this.min, this.max);
},
getRandom: function ()
{
return Between(this.min, this.max);
},
getRandomFloat: function ()
{
return FloatBetween(this.min, this.max);
},
current: {
get: function ()
{
return this._current;
},
set: function (value)
{
this._current = Wrap(value, this.min, this.max);
}
},
x: {
get: function ()
{
return this.min;
},
set: function (value)
{
this.min = value;
}
},
y: {
get: function ()
{
return this.max;
},
set: function (value)
{
this.max = value;
}
}
});
module.exports = MinMax2;

185
v3/src/math/MinMax4.js Normal file
View file

@ -0,0 +1,185 @@
var Between = require('./Between');
var FloatBetween = require('./FloatBetween');
var Class = require('../utils/Class');
var Percent = require('./Percent');
var Wrap = require('./Wrap');
var Vector2 = require('./Vector2');
// A Helper Class that allows you to specify a range between min and max, and then
// keep the value within those bounds, or get random ints or floats from the range.
var MinMax4 = new Class({
initialize:
function MinMax4 (xMin, xMax, yMin, yMax)
{
this.xMin = 0;
this.xMax = 0;
this.yMin = 0;
this.yMax = 0;
this._currentX = 0;
this._currentY = 0;
if (xMin !== undefined)
{
this.set(xMin, xMax, yMin, yMax);
}
},
setX: function (min, max)
{
if (min === undefined) { min = 0; }
if (max === undefined) { max = min; }
this.xMin = min;
this.xMax = max;
this._currentX = min;
return this;
},
setY: function (min, max)
{
if (min === undefined) { min = 0; }
if (max === undefined) { max = min; }
this.yMin = min;
this.yMax = max;
this._currentY = min;
return this;
},
set: function (xMin, xMax, yMin, yMax)
{
if (xMin === undefined) { xMin = 0; }
if (xMax === undefined) { xMax = xMin; }
if (yMin === undefined) { yMin = xMin; }
if (yMax === undefined) { yMax = xMax; }
this.setX(xMax, xMax);
this.setY(yMax, yMax);
return this;
},
clone: function ()
{
return new MinMax4(this.xMin, this.xMax, this.yMin, this.yMax);
},
copyX: function (dest)
{
dest.x = this.xMin;
dest.y = this.xMax;
return this;
},
copyY: function (dest)
{
dest.x = this.yMin;
dest.y = this.yMax;
return this;
},
copy: function (dest)
{
dest.xMin = this.xMin;
dest.xMax = this.xMax;
dest.yMin = this.yMin;
dest.yMax = this.yMax;
return dest;
},
/*
// Given U (a value between 0 and 1) return the value in the range
getU: function (u)
{
// TODO
},
// Returns a value between 0 and 1 based on value
getPercent: function (value)
{
return Percent(value, this.min, this.max);
},
*/
getRandom: function (vec2)
{
if (vec2 === undefined) { vec2 = new Vector2(); }
vec2.x = this.getRandomX();
vec2.y = this.getRandomY();
return vec2;
},
getRandomFloat: function (vec2)
{
if (vec2 === undefined) { vec2 = new Vector2(); }
vec2.x = this.getRandomXFloat();
vec2.y = this.getRandomYFloat();
return vec2;
},
getRandomX: function ()
{
return Between(this.xMin, this.xMax);
},
getRandomY: function ()
{
return Between(this.yMin, this.yMax);
},
getRandomXFloat: function ()
{
return FloatBetween(this.xMin, this.xMax);
},
getRandomYFloat: function ()
{
return FloatBetween(this.yMin, this.yMax);
},
x: {
get: function ()
{
return this._currentX
},
set: function (value)
{
this._currentX = Wrap(this._currentX, this.xMin, this.xMax);
}
},
y: {
get: function ()
{
return this._currentY;
},
set: function (value)
{
this._currentY = Wrap(this._currentY, this.yMin, this.yMax);
}
}
});
module.exports = MinMax4;

View file

@ -33,6 +33,7 @@ module.exports = {
Factorial: require('./Factorial'),
FloatBetween: require('./FloatBetween'),
FloorTo: require('./FloorTo'),
FromPercent: require('./FromPercent'),
GetSpeed: require('./GetSpeed'),
IsEven: require('./IsEven'),
IsEvenStrict: require('./IsEvenStrict'),
@ -55,6 +56,10 @@ module.exports = {
Within: require('./Within'),
Wrap: require('./Wrap'),
// Classes
MinMax2: require('./MinMax2'),
MinMax4: require('./MinMax4'),
// Vector classes
Vector2: require('./Vector2'),
Vector3: require('./Vector3'),