Skip to content

Commit f13043e

Browse files
committed
Math.MinMax classes added
1 parent 61c8f63 commit f13043e

3 files changed

Lines changed: 313 additions & 0 deletions

File tree

v3/src/math/MinMax2.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
var Between = require('./Between');
2+
var FloatBetween = require('./FloatBetween');
3+
var Class = require('../utils/Class');
4+
var Percent = require('./Percent');
5+
var Wrap = require('./Wrap');
6+
7+
// A Helper Class that allows you to specify a range between min and max, and then
8+
// keep the value within those bounds, or get random ints or floats from the range.
9+
10+
var MinMax2 = new Class({
11+
12+
initialize:
13+
14+
function MinMax2 (min, max)
15+
{
16+
if (min === undefined) { min = 0; }
17+
if (max === undefined) { max = min; }
18+
19+
this.min = min;
20+
this.max = max;
21+
22+
this._current = min;
23+
},
24+
25+
set: function (min, max)
26+
{
27+
if (min === undefined) { min = 0; }
28+
if (max === undefined) { max = min; }
29+
30+
this.min = min;
31+
this.max = max;
32+
33+
return this;
34+
},
35+
36+
clone: function ()
37+
{
38+
return new MinMax2(this.min, this.max);
39+
},
40+
41+
copy: function (dest)
42+
{
43+
dest.min = this.min;
44+
dest.max = this.max;
45+
46+
return this;
47+
},
48+
49+
copyXY: function (dest)
50+
{
51+
dest.x = this.min;
52+
dest.y = this.max;
53+
54+
return this;
55+
},
56+
57+
// Given U (a value between 0 and 1) return the value in the range
58+
getU: function (u)
59+
{
60+
// TODO
61+
},
62+
63+
// Returns a value between 0 and 1 based on value
64+
getPercent: function (value)
65+
{
66+
return Percent(value, this.min, this.max);
67+
},
68+
69+
getRandom: function ()
70+
{
71+
return Between(this.min, this.max);
72+
},
73+
74+
getRandomFloat: function ()
75+
{
76+
return FloatBetween(this.min, this.max);
77+
},
78+
79+
current: {
80+
81+
get: function ()
82+
{
83+
return this._current;
84+
},
85+
86+
set: function (value)
87+
{
88+
this._current = Wrap(value, this.min, this.max);
89+
}
90+
91+
},
92+
93+
x: {
94+
95+
get: function ()
96+
{
97+
return this.min;
98+
},
99+
100+
set: function (value)
101+
{
102+
this.min = value;
103+
}
104+
105+
},
106+
107+
y: {
108+
109+
get: function ()
110+
{
111+
return this.max;
112+
},
113+
114+
set: function (value)
115+
{
116+
this.max = value;
117+
}
118+
119+
}
120+
121+
});
122+
123+
module.exports = MinMax2;

v3/src/math/MinMax4.js

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
var Between = require('./Between');
2+
var FloatBetween = require('./FloatBetween');
3+
var Class = require('../utils/Class');
4+
var Percent = require('./Percent');
5+
var Wrap = require('./Wrap');
6+
var Vector2 = require('./Vector2');
7+
8+
// A Helper Class that allows you to specify a range between min and max, and then
9+
// keep the value within those bounds, or get random ints or floats from the range.
10+
11+
var MinMax4 = new Class({
12+
13+
initialize:
14+
15+
function MinMax4 (xMin, xMax, yMin, yMax)
16+
{
17+
this.xMin = 0;
18+
this.xMax = 0;
19+
20+
this.yMin = 0;
21+
this.yMax = 0;
22+
23+
this._currentX = 0;
24+
this._currentY = 0;
25+
26+
if (xMin !== undefined)
27+
{
28+
this.set(xMin, xMax, yMin, yMax);
29+
}
30+
},
31+
32+
setX: function (min, max)
33+
{
34+
if (min === undefined) { min = 0; }
35+
if (max === undefined) { max = min; }
36+
37+
this.xMin = min;
38+
this.xMax = max;
39+
this._currentX = min;
40+
41+
return this;
42+
},
43+
44+
setY: function (min, max)
45+
{
46+
if (min === undefined) { min = 0; }
47+
if (max === undefined) { max = min; }
48+
49+
this.yMin = min;
50+
this.yMax = max;
51+
this._currentY = min;
52+
53+
return this;
54+
},
55+
56+
set: function (xMin, xMax, yMin, yMax)
57+
{
58+
if (xMin === undefined) { xMin = 0; }
59+
if (xMax === undefined) { xMax = xMin; }
60+
if (yMin === undefined) { yMin = xMin; }
61+
if (yMax === undefined) { yMax = xMax; }
62+
63+
this.setX(xMax, xMax);
64+
this.setY(yMax, yMax);
65+
66+
return this;
67+
},
68+
69+
clone: function ()
70+
{
71+
return new MinMax4(this.xMin, this.xMax, this.yMin, this.yMax);
72+
},
73+
74+
copyX: function (dest)
75+
{
76+
dest.x = this.xMin;
77+
dest.y = this.xMax;
78+
79+
return this;
80+
},
81+
82+
copyY: function (dest)
83+
{
84+
dest.x = this.yMin;
85+
dest.y = this.yMax;
86+
87+
return this;
88+
},
89+
90+
copy: function (dest)
91+
{
92+
dest.xMin = this.xMin;
93+
dest.xMax = this.xMax;
94+
95+
dest.yMin = this.yMin;
96+
dest.yMax = this.yMax;
97+
98+
return dest;
99+
},
100+
101+
/*
102+
// Given U (a value between 0 and 1) return the value in the range
103+
getU: function (u)
104+
{
105+
// TODO
106+
},
107+
108+
// Returns a value between 0 and 1 based on value
109+
getPercent: function (value)
110+
{
111+
return Percent(value, this.min, this.max);
112+
},
113+
*/
114+
115+
getRandom: function (vec2)
116+
{
117+
if (vec2 === undefined) { vec2 = new Vector2(); }
118+
119+
vec2.x = this.getRandomX();
120+
vec2.y = this.getRandomY();
121+
122+
return vec2;
123+
},
124+
125+
getRandomFloat: function (vec2)
126+
{
127+
if (vec2 === undefined) { vec2 = new Vector2(); }
128+
129+
vec2.x = this.getRandomXFloat();
130+
vec2.y = this.getRandomYFloat();
131+
132+
return vec2;
133+
},
134+
135+
getRandomX: function ()
136+
{
137+
return Between(this.xMin, this.xMax);
138+
},
139+
140+
getRandomY: function ()
141+
{
142+
return Between(this.yMin, this.yMax);
143+
},
144+
145+
getRandomXFloat: function ()
146+
{
147+
return FloatBetween(this.xMin, this.xMax);
148+
},
149+
150+
getRandomYFloat: function ()
151+
{
152+
return FloatBetween(this.yMin, this.yMax);
153+
},
154+
155+
x: {
156+
157+
get: function ()
158+
{
159+
return this._currentX
160+
},
161+
162+
set: function (value)
163+
{
164+
this._currentX = Wrap(this._currentX, this.xMin, this.xMax);
165+
}
166+
167+
},
168+
169+
y: {
170+
171+
get: function ()
172+
{
173+
return this._currentY;
174+
},
175+
176+
set: function (value)
177+
{
178+
this._currentY = Wrap(this._currentY, this.yMin, this.yMax);
179+
}
180+
181+
}
182+
183+
});
184+
185+
module.exports = MinMax4;

v3/src/math/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module.exports = {
3333
Factorial: require('./Factorial'),
3434
FloatBetween: require('./FloatBetween'),
3535
FloorTo: require('./FloorTo'),
36+
FromPercent: require('./FromPercent'),
3637
GetSpeed: require('./GetSpeed'),
3738
IsEven: require('./IsEven'),
3839
IsEvenStrict: require('./IsEvenStrict'),
@@ -55,6 +56,10 @@ module.exports = {
5556
Within: require('./Within'),
5657
Wrap: require('./Wrap'),
5758

59+
// Classes
60+
MinMax2: require('./MinMax2'),
61+
MinMax4: require('./MinMax4'),
62+
5863
// Vector classes
5964
Vector2: require('./Vector2'),
6065
Vector3: require('./Vector3'),

0 commit comments

Comments
 (0)