private static Col RandomizeColor(Col baseColor, float maxRelativeVariance, bool monochrome = false) { if (maxRelativeVariance >= 0 && maxRelativeVariance <= 1) { //Monochrome colors should stay monochrome if (!monochrome) { float variance = Rul.RandFloat(maxRelativeVariance * GetVarianceFactor(baseColor.R)); int r = MathHelper.Clamp(baseColor.R + (int)(Rul.RandSign() * 255 * variance), 0, 255); variance = Rul.RandFloat(maxRelativeVariance * GetVarianceFactor(baseColor.G)); int g = MathHelper.Clamp(baseColor.G + (int)(Rul.RandSign() * 255 * variance), 0, 255); variance = Rul.RandFloat(maxRelativeVariance * GetVarianceFactor(baseColor.B)); int b = MathHelper.Clamp(baseColor.B + (int)(Rul.RandSign() * 255 * variance), 0, 255); return(new Col(r, g, b, baseColor.A)); } else { float variance = Rul.RandFloat(maxRelativeVariance * GetVarianceFactor(baseColor.R)); int newValue = MathHelper.Clamp(baseColor.R + (int)(Rul.RandSign() * 255 * variance), 0, 255); return(new Col(newValue, newValue, newValue, baseColor.A)); } } else { throw new ArgumentException("Value must be between 0 and 1"); } }
/// <summary> /// Returns a color that is randomly interpolated between the given colors /// </summary> public static Col RandColorBetween(Col colA, Col colB) { float v = Rul.RandFloat(); int r = (int)Math.Round(colA.R + (colB.R - colA.R) * v); int g = (int)Math.Round(colA.G + (colB.G - colA.G) * v); int b = (int)Math.Round(colA.B + (colB.B - colA.B) * v); int a = (int)Math.Round(colA.A + (colB.A - colA.A) * v); return(new Col(r, g, b, a)); }
/// <summary> /// Returns a random 2-dimensional vector that lies between the given end points /// </summary> public static Vector2 RandVector2Between(Vector2 pointA, Vector2 pointB) { if (pointA == pointB) { return(pointA); } Vector2 difference = pointB - pointA; return(pointA + difference * Rul.RandFloat()); }
/// <summary> /// Returns a random 3-dimensional vector with the length 1 /// </summary> public static Vector3 RandUnitVector3() { float theta = Rul.RandFloat((float)Math.PI * 2); float z = Rul.RandFloat(-1, 1); float c = (float)(Math.Sqrt(1 - z * z)); float x = (float)(c * Math.Cos(theta)); float y = (float)(c * Math.Sin(theta)); return(new Vector3(x, y, z)); }
/// <summary> /// Returns a color that is randomly interpolated between the given colors /// </summary> public static UnityEngine.Color RandColorBetween(UnityEngine.Color colA, UnityEngine.Color colB) { float v = Rul.RandFloat(); int r = (int)Math.Round(colA.r + (colB.r - colA.r) * v); int g = (int)Math.Round(colA.g + (colB.g - colA.g) * v); int b = (int)Math.Round(colA.b + (colB.b - colA.b) * v); int a = (int)Math.Round(colA.a + (colB.a - colA.a) * v); return(new UnityEngine.Color(r, g, b, a)); }
/// <summary> /// Returns a randomly rotated version of the given base vector /// </summary> /// <param name="baseVector">The vector that is used as a base for the new one</param> /// <param name="maxAngle">The greatest possible angle(in radians) between the base vector and the rotated random vector</param> public static Vector2 RandVector2(Vector2 baseVector, double maxAngle) { float angle = Rul.RandFloat((float)maxAngle % (float)(2F * Math.PI)) * Rul.RandSign(); double cos = Math.Cos(angle); double sin = Math.Sin(angle); float newX = (float)(baseVector.x * cos - baseVector.y * sin); float newY = (float)(baseVector.x * sin + baseVector.y * cos); return(new Vector2(newX, newY)); }
/// <summary> /// Returns a random, one-dimensional array of floats between 0 and 1 /// </summary> public static float[] RandNoise1(int length) { ValidateSizeParameters(new object[] { length }); float[] noise = new float[length]; for (int i = 0; i < length; i++) { noise[i] = Rul.RandFloat(); } return(noise); }
/// <summary> /// Returns a random, two-dimensional array of floats between 0 and 1 /// </summary> public static float[,] RandNoise2(int width, int height) { ValidateSizeParameters(new object[] { width, height }); float[,] noise = new float[width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { noise[x, y] = Rul.RandFloat(); } } return(noise); }
public SimplexOctave(int seed) { _p = (short[])_pSupply.Clone(); //Randomize _p Rul.Initialize(seed); Rul.Shuffle(_p); for (int i = 0; i < 512; i++) { _perm[i] = _p[i & 255]; _permMod12[i] = (short)(_perm[i] % 12); } }
/// <summary> /// Returns a random, three-dimensional array of floats between 0 and 1 /// </summary> public static float[, ,] RandNoise3(int width, int height, int depth) { ValidateSizeParameters(new object[] { width, height, depth }); float[, ,] noise = new float[width, height, depth]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { for (int z = 0; z < depth; z++) { noise[x, y, z] = Rul.RandFloat(); } } } return(noise); }
private static UnityEngine.Color RandomizeColor(UnityEngine.Color baseColor, float maxRelativeVariance, bool monochrome = false) { maxRelativeVariance = MathHelper.Clamp(maxRelativeVariance, 0, 1); //Monochrome colors should stay monochrome if (!monochrome) { float variance = Rul.RandFloat(maxRelativeVariance * GetVarianceFactor(baseColor.r)); float r = MathHelper.Clamp(baseColor.r + Rul.RandSign() * variance, 0, 1); variance = Rul.RandFloat(maxRelativeVariance * GetVarianceFactor(baseColor.g)); float g = MathHelper.Clamp(baseColor.g + Rul.RandSign() * variance, 0, 1); variance = Rul.RandFloat(maxRelativeVariance * GetVarianceFactor(baseColor.b)); float b = MathHelper.Clamp(baseColor.b + Rul.RandSign() * variance, 0, 1); return(new UnityEngine.Color(r, g, b, baseColor.a)); } else { float variance = Rul.RandFloat(maxRelativeVariance * GetVarianceFactor(baseColor.r)); float newValue = MathHelper.Clamp(baseColor.r + Rul.RandSign() * variance, 0, 1); return(new UnityEngine.Color(newValue, newValue, newValue, baseColor.a)); } }
/// <summary> /// Returns a completely random, opaque color /// </summary> public static Col RandColor() { return(new Col(Rul.RandInt(255), Rul.RandInt(255), Rul.RandInt(255))); }
/// <summary> /// Returns a random color with the specified hue and random luminosity /// </summary> /// <param name="hue">The approximate hue of the random color</param> public static UnityEngine.Color RandColor(Hues hue) { LuminosityTypes luminosity = Rul.RandElement(LuminosityTypes.Light, LuminosityTypes.Medium, LuminosityTypes.Dark); return(RandColor(hue, luminosity)); }
/// <summary> /// Returns a random 3-dimensional vector that lies in the sphere with the specified radius /// </summary> /// <param name="sphereRadius">The radius of the sphere that contains the point represented by the random vector</param> public static Vector3 RandVecInSphere(float sphereRadius) { return(RandUnitVector3() * Rul.RandFloat() * sphereRadius); }
/// <summary> /// Returns a random 3-dimensional vector within the specified range /// </summary> /// <param name="lowerBoundX">Lower bound for the x-component</param> /// <param name="upperBoundX">Upper bound for the x-component</param> /// <param name="lowerBoundY">Lower bound for the y-component</param> /// <param name="upperBoundY">Upper bound for the y-component</param> /// <param name="lowerBoundZ">Lower bound for the z-component</param> /// <param name="upperBoundZ">Upper bound for the z-component</param> public static Vector3 RandVector3(float lowerBoundX, float lowerBoundY, float lowerBoundZ, float upperBoundX, float upperBoundY, float upperBoundZ) { return(new Vector3(Rul.RandFloat(lowerBoundX, upperBoundX), Rul.RandFloat(lowerBoundY, upperBoundY), Rul.RandFloat(lowerBoundZ, upperBoundZ))); }
/// <summary> /// Returns a random 2-dimensional vector that points up, down, left or right /// </summary> public static Vector2 RandDirection2() { return(Rul.RandElement(new Vector2(1, 0), new Vector2(0, 1), new Vector2(-1, 0), new Vector2(0, -1))); }
/// <summary> /// Returns a random 2-dimensional vector that lies in the circle with the specified radius /// </summary> /// <param name="circleRadius">The radius of the circle that contains the point represented by the random vector</param> public static Vector2 RandVecInCircle(float circleRadius) { return(RandUnitVector2() * Rul.RandFloat(circleRadius)); }
/// <summary> /// Returns a random 2-dimensional vector with the length 1 /// </summary> public static Vector2 RandUnitVector2() { float rad = Rul.RandFloat((float)Math.PI * 2); return(new Vector2((float)Math.Cos(rad), (float)Math.Sin(rad))); }
/// <summary> /// Returns a random 3-dimensional vector that points left, right, up, down, forwards or backwards /// </summary> public static Vector3 RandDirection3() { return(Rul.RandElement(new Vector3(-1, 0, 0), new Vector3(1, 0, 0), new Vector3(0, -1, 0), new Vector3(0, 1, 0), new Vector3(0, 0, -1), new Vector3(0, 0, 1))); }
/// <summary> /// Returns a random 2-dimensional vector within the specified range /// </summary> /// <param name="lowerBoundX">Lower bound for the x-component</param> /// <param name="upperBoundX">Upper bound for the x-component</param> /// <param name="lowerBoundY">Lower bound for the y-component</param> /// <param name="upperBoundY">Upper bound for the y-component</param> public static Vector2 RandVector2(float lowerBoundX, float lowerBoundY, float upperBoundX, float upperBoundY) { return(new Vector2(Rul.RandFloat(lowerBoundX, upperBoundX), Rul.RandFloat(lowerBoundY, upperBoundY))); }
/// <summary> /// Returns a random element from the given array with the specified probabilities for each element /// </summary> /// <param name="elements">The selection of elements</param> /// <param name="probabilities">The probability for each element</param> public static T RandElement <T>(T[] elements, params float[] probabilities) { if (elements.Length == 0) { throw new ArgumentException("Element array cannot be empty"); } if (probabilities.Length == 0) { return(RandElement(elements)); } float pSum = probabilities.Sum(); //Add equal probabilities if the probabilities array is not long enough if (probabilities.Length < elements.Length && pSum < 1) { int missing = elements.Length - probabilities.Length; float[] additional = new float[missing]; for (int i = 0; i < additional.Length; i++) { additional[i] = (1 - pSum) / (float)missing; } float[] allProbs = new float[elements.Length]; probabilities.CopyTo(allProbs, 0); additional.CopyTo(allProbs, probabilities.Length); probabilities = allProbs; } //Correct invalid probabilities for (int i = 0; i < probabilities.Length; i++) { probabilities[i] = MathHelper.Clamp(probabilities[i], 0, 1); } //Make sure the probabilities add up to 1 float difference = 1 - pSum; //Sum too low ? Add missing probability to last element if possible if (!MathHelper.FloatsEqual(difference, 0) && difference > 0) { for (int i = probabilities.Length - 1; i <= 0 && difference > 0 && !MathHelper.FloatsEqual(difference, 0); i++) { float buffer = 1 - probabilities[i]; probabilities[i] += Math.Min(buffer, difference); difference -= buffer; } } //Sum too high ? Subtract excess probability from last element if possible else if (!MathHelper.FloatsEqual(difference, 0) && difference < 0) { for (int i = probabilities.Length - 1; i <= 0 && difference < 0 && !MathHelper.FloatsEqual(difference, 0); i++) { float buffer = probabilities[i]; probabilities[i] += Math.Max(buffer, difference); difference += buffer; } } float r = Rul.RandFloat(); float f = 0; for (int i = 0; i < elements.Length; i++) { if (probabilities.Length > i) { f += probabilities[i]; if (r <= f) { return(elements[i]); } } } return(elements[elements.Length - 1]); }
/// <summary> /// Returns a completely random, opaque color /// </summary> public static UnityEngine.Color RandColor() { return(new UnityEngine.Color(Rul.RandFloat(), Rul.RandFloat(), Rul.RandFloat())); }