From 11aa39e29ed2b9b6032bb794ae4f35784deafa06 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Mon, 18 Sep 2017 21:48:26 +0100 Subject: [PATCH] Removed the Vector random methods and moved to their own function as they're swappable between each other --- v3/src/math/RandomXY.js | 13 +++++++++++++ v3/src/math/RandomXYZ.js | 17 +++++++++++++++++ v3/src/math/RandomXYZW.js | 14 ++++++++++++++ v3/src/math/Vector2.js | 12 ------------ v3/src/math/Vector3.js | 16 ---------------- v3/src/math/Vector4.js | 13 ------------- v3/src/math/index.js | 3 +++ 7 files changed, 47 insertions(+), 41 deletions(-) create mode 100644 v3/src/math/RandomXY.js create mode 100644 v3/src/math/RandomXYZ.js create mode 100644 v3/src/math/RandomXYZW.js diff --git a/v3/src/math/RandomXY.js b/v3/src/math/RandomXY.js new file mode 100644 index 000000000..eff40c757 --- /dev/null +++ b/v3/src/math/RandomXY.js @@ -0,0 +1,13 @@ +var RandomXY = function (vector, scale) +{ + if (scale === undefined) { scale = 1; } + + var r = Math.random() * 2 * Math.PI; + + vector.x = Math.cos(r) * scale; + vector.y = Math.sin(r) * scale; + + return vector; +}; + +module.exports = RandomXY; diff --git a/v3/src/math/RandomXYZ.js b/v3/src/math/RandomXYZ.js new file mode 100644 index 000000000..fab5f5052 --- /dev/null +++ b/v3/src/math/RandomXYZ.js @@ -0,0 +1,17 @@ +// Position Vector randomly in a spherical area defined by the given radius +var RandomXYZ = function (vector, radius) +{ + if (radius === undefined) { radius = 1; } + + var r = Math.random() * 2 * Math.PI; + var z = (Math.random() * 2) - 1; + var zScale = Math.sqrt(1 - z * z) * radius; + + vector.x = Math.cos(r) * zScale; + vector.y = Math.sin(r) * zScale; + vector.z = z * radius; + + return vector; +}; + +module.exports = RandomXYZ; diff --git a/v3/src/math/RandomXYZW.js b/v3/src/math/RandomXYZW.js new file mode 100644 index 000000000..da1b1b804 --- /dev/null +++ b/v3/src/math/RandomXYZW.js @@ -0,0 +1,14 @@ +var RandomXYZW = function (vector, scale) +{ + if (scale === undefined) { scale = 1; } + + // Not spherical; should fix this for more uniform distribution + vector.x = (Math.random() * 2 - 1) * scale; + vector.y = (Math.random() * 2 - 1) * scale; + vector.z = (Math.random() * 2 - 1) * scale; + vector.w = (Math.random() * 2 - 1) * scale; + + return vector; +}; + +module.exports = RandomXYZW; diff --git a/v3/src/math/Vector2.js b/v3/src/math/Vector2.js index 45cf6fcf0..92af4ba70 100644 --- a/v3/src/math/Vector2.js +++ b/v3/src/math/Vector2.js @@ -198,18 +198,6 @@ var Vector2 = new Class({ this.x = 0; this.y = 0; - return this; - }, - - random: function (scale) - { - if (scale === undefined) { scale = 1; } - - var r = Math.random() * 2 * Math.PI; - - this.x = Math.cos(r) * scale; - this.y = Math.sin(r) * scale; - return this; } diff --git a/v3/src/math/Vector3.js b/v3/src/math/Vector3.js index 4b783962d..aadad1b2b 100644 --- a/v3/src/math/Vector3.js +++ b/v3/src/math/Vector3.js @@ -327,22 +327,6 @@ var Vector3 = new Class({ return this.project(invProjectionView); }, - // Position Vector randomly in a spherical area defined by the given radius - random: function (radius) - { - if (radius === undefined) { radius = 1; } - - var r = Math.random() * 2 * Math.PI; - var z = (Math.random() * 2) - 1; - var zScale = Math.sqrt(1 - z * z) * radius; - - this.x = Math.cos(r) * zScale; - this.y = Math.sin(r) * zScale; - this.z = z * radius; - - return this; - }, - reset: function () { this.x = 0; diff --git a/v3/src/math/Vector4.js b/v3/src/math/Vector4.js index 4783b5597..96c5eda5f 100644 --- a/v3/src/math/Vector4.js +++ b/v3/src/math/Vector4.js @@ -246,19 +246,6 @@ var Vector4 = new Class({ return this; }, - random: function (scale) - { - if (scale === undefined) { scale = 1; } - - // Not spherical; should fix this for more uniform distribution - this.x = (Math.random() * 2 - 1) * scale; - this.y = (Math.random() * 2 - 1) * scale; - this.z = (Math.random() * 2 - 1) * scale; - this.w = (Math.random() * 2 - 1) * scale; - - return this; - }, - reset: function () { this.x = 0; diff --git a/v3/src/math/index.js b/v3/src/math/index.js index fedab330f..3afa5c305 100644 --- a/v3/src/math/index.js +++ b/v3/src/math/index.js @@ -29,6 +29,9 @@ module.exports = { MinSub: require('./MinSub'), Percent: require('./Percent'), RadToDeg: require('./RadToDeg'), + RandomXY: require('./RandomXY'), + RandomXYZ: require('./RandomXYZ'), + RandomXYZW: require('./RandomXYZW'), Rotate: require('./Rotate'), RotateAround: require('./RotateAround'), RotateAroundDistance: require('./RotateAroundDistance'),