Skip to content

Commit 0fce66c

Browse files
committed
Added in all of the Phaser Math functions. Phew!
1 parent 7bf07c2 commit 0fce66c

64 files changed

Lines changed: 1043 additions & 2 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

v3/src/math/Average.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var Average = function ()
2+
{
3+
var sum = 0;
4+
var len = arguments.length;
5+
6+
for (var i = 0; i < len; i++)
7+
{
8+
sum += (+arguments[i]);
9+
}
10+
11+
return sum / len;
12+
};
13+
14+
module.exports = Average;

v3/src/math/Bernstein.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var Factorial = require('./Factorial');
2+
3+
var Bernstein = function (n, i)
4+
{
5+
return Factorial(n) / Factorial(i) / Factorial(n - i);
6+
};
7+
8+
module.exports = Bernstein;

v3/src/math/Between.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var Between = function (min, max)
2+
{
3+
return Math.floor(Math.random() * (max - min + 1) + min);
4+
};
5+
6+
module.exports = Between;

v3/src/math/CatmullRom.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var CatmullRom = function (p0, p1, p2, p3, t)
2+
{
3+
var v0 = (p2 - p0) * 0.5, v1 = (p3 - p1) * 0.5, t2 = t * t, t3 = t * t2;
4+
5+
return (2 * p1 - 2 * p2 + v0 + v1) * t3 + (-3 * p1 + 3 * p2 - 2 * v0 - v1) * t2 + v0 * t + p1;
6+
};
7+
8+
module.exports = CatmullRom;

v3/src/math/CeilTo.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var CeilTo = function (value, place, base)
2+
{
3+
if (place === undefined) { place = 0; }
4+
if (base === undefined) { base = 10; }
5+
6+
var p = Math.pow(base, -place);
7+
8+
return Math.ceil(value * p) / p;
9+
};
10+
11+
module.exports = CeilTo;

v3/src/math/DegToRad.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var CONST = require('./const');
2+
3+
var DegToRad = function (degrees)
4+
{
5+
return degrees * CONST.DEG_TO_RAD;
6+
};
7+
8+
module.exports = DegToRad;

v3/src/math/Difference.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var Difference = function (a, b)
2+
{
3+
return Math.abs(a - b);
4+
};
5+
6+
module.exports = Difference;

v3/src/math/Factorial.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var Factorial = function (value)
2+
{
3+
if (value === 0)
4+
{
5+
return 1;
6+
}
7+
8+
var res = value;
9+
10+
while (--value)
11+
{
12+
res *= value;
13+
}
14+
15+
return res;
16+
};
17+
18+
module.exports = Factorial;

v3/src/math/FloatBetween.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var FloatBetween = function (min, max)
2+
{
3+
return Math.random() * (max - min + 1) + min;
4+
};
5+
6+
module.exports = FloatBetween;

v3/src/math/FloorTo.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var FloorTo = function (value, place, base)
2+
{
3+
if (place === undefined) { place = 0; }
4+
if (base === undefined) { base = 10; }
5+
6+
var p = Math.pow(base, -place);
7+
8+
return Math.floor(value * p) / p;
9+
};
10+
11+
module.exports = FloorTo;

0 commit comments

Comments
 (0)