public static VectorD[][] GenerateNoise(int seed, int size) { Random rand = new Random(seed); VectorD[][] vec = new VectorD[size][]; for (int c = 0; c < size; c++) { vec[c] = new VectorD[size]; for (int r = 0; r < size; r++) { double direction = rand.NextDouble() * Math.PI * 2; vec[c][r] = new VectorD(Math.Cos(direction), Math.Sin(direction)); } } return(vec); }
public static double Noise(VectorD[][] vectors, int x, int y, double xScale, double yScale) { int length = vectors.Length; double xx = (((x / xScale) % length) + length) % length; double yy = (((y / yScale) % length) + length) % length; VectorD vectl = vectors[(int)yy][(int)xx]; VectorD vectr = vectors[(int)yy][((int)xx + 1) % vectors.Length]; VectorD vecbl = vectors[((int)yy + 1) % vectors.Length][(int)xx]; VectorD vecbr = vectors[((int)yy + 1) % vectors.Length][((int)xx + 1) % vectors.Length]; VectorD vec = new VectorD(xx - (int)xx, yy - (int)yy); double s = vec.Dot(vectl); vec = new VectorD(xx - ((int)xx + 1), yy - (int)yy); double t = vec.Dot(vectr); vec = new VectorD(xx - (int)xx, yy - ((int)yy + 1)); double u = vec.Dot(vecbl); vec = new VectorD(xx - ((int)xx + 1), yy - ((int)yy + 1)); double v = vec.Dot(vecbr); double xW = xx - (int)xx; xW = 6 * xW * xW * xW * xW * xW - 15 * xW * xW * xW * xW + 10 * xW * xW * xW; double yW = yy - (int)yy; yW = 6 * yW * yW * yW * yW * yW - 15 * yW * yW * yW * yW + 10 * yW * yW * yW; ////return (((s * (1 - xW) + t * xW) * (1 - yW) //// + (u * (1 - xW) + v * xW) * yW) / 0.65 + 1) / 2; double calc = ((s * (1 - xW) + t * xW) * (1 - yW) + (u * (1 - xW) + v * xW) * yW); return(((calc / 0.65) + 1) / 2); }
public double Dot(VectorD other) { return((X * other.X) + (Y * other.Y)); }