From c58d7c104bb6b6cbb2d5478bf50eb60d30e6a63d Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 11 Jan 2020 23:34:10 +0100 Subject: [PATCH] Optimization --- CubicNoise/Noisers/CubicNoiseEngine.cs | 58 +++++++++++++------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/CubicNoise/Noisers/CubicNoiseEngine.cs b/CubicNoise/Noisers/CubicNoiseEngine.cs index 7d86729..51498dc 100644 --- a/CubicNoise/Noisers/CubicNoiseEngine.cs +++ b/CubicNoise/Noisers/CubicNoiseEngine.cs @@ -16,7 +16,7 @@ namespace CubicNoise.Noisers private readonly int periodY; private readonly int seed; - public CubicNoiseEngine(int seed, Dictionary intParameters) + public CubicNoiseEngine(int seed, IReadOnlyDictionary intParameters) { this.seed = seed; this.octave = intParameters?.ContainsKey(CubicNoiseIntParameters.OCTAVE) == true ? intParameters[CubicNoiseIntParameters.OCTAVE] : 16; @@ -29,11 +29,11 @@ namespace CubicNoise.Noisers var xi = (int)Math.Floor(x / this.octave); var octaveXFactor = x / this.octave - xi; - return this.Interpolation( - a: this.RandomNumberGenerator(x: this.DeterminePatch(xi - 1, this.periodX), y: 0), - b: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 0, this.periodX), y: 0), - c: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 1, this.periodX), y: 0), - d: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 2, this.periodX), y: 0), + return Interpolation( + a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: 0), + b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: 0), + c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: 0), + d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: 0), x: octaveXFactor) * 0.5f + 0.25f; } @@ -45,33 +45,33 @@ namespace CubicNoise.Noisers var octaveXFactor = x / octave - xi; var octaveYFactor = y / octave - yi; - return this.Interpolation( - a: this.Interpolation( - a: this.RandomNumberGenerator(x: this.DeterminePatch(xi - 1, this.periodX), y: this.DeterminePatch(yi - 1 + 0, this.periodY)), - b: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 0, this.periodX), y: this.DeterminePatch(yi - 1 + 0, this.periodY)), - c: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 1, this.periodX), y: this.DeterminePatch(yi - 1 + 0, this.periodY)), - d: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 2, this.periodX), y: this.DeterminePatch(yi - 1 + 0, this.periodY)), + return Interpolation( + a: Interpolation( + a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: DeterminePatch(yi - 1 + 0, this.periodY)), + b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: DeterminePatch(yi - 1 + 0, this.periodY)), + c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: DeterminePatch(yi - 1 + 0, this.periodY)), + d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: DeterminePatch(yi - 1 + 0, this.periodY)), x: octaveXFactor), - b: this.Interpolation( - a: this.RandomNumberGenerator(x: this.DeterminePatch(xi - 1, this.periodX), y: this.DeterminePatch(yi - 1 + 1, this.periodY)), - b: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 0, this.periodX), y: this.DeterminePatch(yi - 1 + 1, this.periodY)), - c: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 1, this.periodX), y: this.DeterminePatch(yi - 1 + 1, this.periodY)), - d: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 2, this.periodX), y: this.DeterminePatch(yi - 1 + 1, this.periodY)), + b: Interpolation( + a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: DeterminePatch(yi - 1 + 1, this.periodY)), + b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: DeterminePatch(yi - 1 + 1, this.periodY)), + c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: DeterminePatch(yi - 1 + 1, this.periodY)), + d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: DeterminePatch(yi - 1 + 1, this.periodY)), x: octaveXFactor), - c: this.Interpolation( - a: this.RandomNumberGenerator(x: this.DeterminePatch(xi - 1, this.periodX), y: this.DeterminePatch(yi - 1 + 2, this.periodY)), - b: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 0, this.periodX), y: this.DeterminePatch(yi - 1 + 2, this.periodY)), - c: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 1, this.periodX), y: this.DeterminePatch(yi - 1 + 2, this.periodY)), - d: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 2, this.periodX), y: this.DeterminePatch(yi - 1 + 2, this.periodY)), + c: Interpolation( + a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: DeterminePatch(yi - 1 + 2, this.periodY)), + b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: DeterminePatch(yi - 1 + 2, this.periodY)), + c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: DeterminePatch(yi - 1 + 2, this.periodY)), + d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: DeterminePatch(yi - 1 + 2, this.periodY)), x: octaveXFactor), - d: this.Interpolation( - a: this.RandomNumberGenerator(x: this.DeterminePatch(xi - 1, this.periodX), y: this.DeterminePatch(yi - 1 + 3, this.periodY)), - b: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 0, this.periodX), y: this.DeterminePatch(yi - 1 + 3, this.periodY)), - c: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 1, this.periodX), y: this.DeterminePatch(yi - 1 + 3, this.periodY)), - d: this.RandomNumberGenerator(x: this.DeterminePatch(xi + 2, this.periodX), y: this.DeterminePatch(yi - 1 + 3, this.periodY)), + d: Interpolation( + a: this.RandomNumberGenerator(x: DeterminePatch(xi - 1, this.periodX), y: DeterminePatch(yi - 1 + 3, this.periodY)), + b: this.RandomNumberGenerator(x: DeterminePatch(xi + 0, this.periodX), y: DeterminePatch(yi - 1 + 3, this.periodY)), + c: this.RandomNumberGenerator(x: DeterminePatch(xi + 1, this.periodX), y: DeterminePatch(yi - 1 + 3, this.periodY)), + d: this.RandomNumberGenerator(x: DeterminePatch(xi + 2, this.periodX), y: DeterminePatch(yi - 1 + 3, this.periodY)), x: octaveXFactor), x: octaveYFactor) * 0.5f + 0.25f; @@ -82,12 +82,12 @@ namespace CubicNoise.Noisers return (float)((((x ^ y) * RANDOM_NUMBER_A) ^ (this.seed + x)) * (((RANDOM_NUMBER_B * x) << 16) ^ (RANDOM_NUMBER_B * y) - RANDOM_NUMBER_A)) / int.MaxValue; } - private int DeterminePatch(int coordinate, int period) + private static int DeterminePatch(int coordinate, int period) { return coordinate % period; } - private float Interpolation(float a, float b, float c, float d, float x) + private static float Interpolation(float a, float b, float c, float d, float x) { var p = (d - c) - (a - b); return x * (x * (x * p + ((a - b) - p)) + (c - a)) + b;