From 654285e57f7b5a3345480c2d2e271b04bfed3cb2 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Wed, 17 Feb 2021 00:30:24 +0100 Subject: [PATCH] Separated NextVector and NextUnitVector, refactoring While I was at it I made some slight refactoring making it much more readable and establish consistency --- Source/Engine/Utilities/Extensions.cs | 94 ++++++++++++++++++++++----- 1 file changed, 77 insertions(+), 17 deletions(-) diff --git a/Source/Engine/Utilities/Extensions.cs b/Source/Engine/Utilities/Extensions.cs index 29aa7329a..d5e383ab6 100644 --- a/Source/Engine/Utilities/Extensions.cs +++ b/Source/Engine/Utilities/Extensions.cs @@ -291,9 +291,26 @@ namespace FlaxEngine.Utilities public static Quaternion NextQuaternion(this Random random, bool randomRoll = false) { return Quaternion.Euler( - NextFloat(random, -180, 180), - NextFloat(random, -180, 180), - randomRoll ? NextFloat(random, -180, 180) : 0); + NextFloat(random, -180.0f, 180.0f), + NextFloat(random, -180.0f, 180.0f), + randomRoll ? NextFloat(random, -180.0f, 180.0f) : 0.0f); + } + + /// + /// Generates a random point in a circle of a given radius. + /// + /// An instance of . + /// Radius of circle. Default 1.0f./>. + /// A random . + public static Vector2 NextUnitVector2(this Random random, float radius = 1.0f) + { + var randomRadius = (float)random.NextDouble() * radius; + + return new Vector2 + { + X = (float)Math.Cos(random.NextDouble()) * randomRadius, + Y = (float)Math.Sin(random.NextDouble()) * randomRadius, + }; } /// @@ -301,7 +318,7 @@ namespace FlaxEngine.Utilities /// /// An instance of . /// A random . - public static Vector3 NextVector3(this Random random) + public static Vector3 NextUnitVector3(this Random random) { Vector3 output; float l; @@ -323,19 +340,53 @@ namespace FlaxEngine.Utilities } /// - /// Generates a random point in a circle of a given radius. + /// Gets a random with components in a given range. /// /// An instance of . - /// Radius of circle. Default 1.0f./>. + /// The minimum value. + /// The maximum value. /// A random . - public static Vector2 NextVector2(this Random random, float radius = 1.0f) + public static Vector2 NextVector2(this Random random, float min = 0.0f, float max = 1.0f) { - var randomRadius = (float)random.NextDouble() * radius; - - return new Vector2 + return new Vector2 { - X = (float)Math.Cos(random.NextDouble()) * randomRadius, - Y = (float)Math.Sin(random.NextDouble()) * randomRadius, + X = NextFloat(random, min, max), + Y = NextFloat(random, min, max) + }; + } + + /// + /// Gets a random with components in a given range. + /// + /// An instance of . + /// The minimum value. + /// The maximum value. + /// A random . + public static Vector3 NextVector3(this Random random, float min = 0.0f, float max = 1.0f) + { + return new Vector3 + { + X = NextFloat(random, min, max), + Y = NextFloat(random, min, max), + Z = NextFloat(random, min, max) + }; + } + + /// + /// Gets a random with components in a given range. + /// + /// An instance of . + /// The minimum value. + /// The maximum value. + /// A random . + public static Vector4 NextVector4(this Random random, float min = 0.0f, float max = 1.0f) + { + return new Vector4 + { + X = NextFloat(random, min, max), + Y = NextFloat(random, min, max), + Z = NextFloat(random, min, max), + W = NextFloat(random, min, max) }; } @@ -347,10 +398,13 @@ namespace FlaxEngine.Utilities /// A nice random . public static Color NextColor(this Random random, bool randomAlpha = false) { - return new Color( - NextFloat(random), - NextFloat(random), - NextFloat(random), randomAlpha ? NextFloat(random) : 1f); + return new Color + { + R = NextFloat(random), + G = NextFloat(random), + B = NextFloat(random), + A = randomAlpha ? NextFloat(random) : 1.0f + }; } /// @@ -361,7 +415,13 @@ namespace FlaxEngine.Utilities /// A nice random . public static ColorHSV NextColorHSV(this Random random, bool randomAlpha = false) { - return new ColorHSV(NextFloat(random, 0f, 360f), 1f, 1f, randomAlpha ? NextFloat(random) : 1f); + return new ColorHSV + { + H = NextFloat(random, 0.0f, 360.0f), + S = 1.0f, + V = 1.0f, + A = randomAlpha ? NextFloat(random) : 1.0f + }; } ///