AForge.Math.PerlinNoise.Function2D C# (CSharp) Method

Function2D() public method

2-D Perlin noise function.
public Function2D ( double x, double y ) : double
x double x value.
y double y value.
return double
        public double Function2D( double x, double y )
        {
            double	frequency = initFrequency;
            double	amplitude = initAmplitude;
            double	sum = 0;

            // octaves
            for ( int i = 0; i < octaves; i++ )
            {
                sum += SmoothedNoise( x * frequency, y * frequency ) * amplitude;

                frequency *= 2;
                amplitude *= persistence;
            }
            return sum;
        }

Usage Example

Beispiel #1
0
        // Generic functions down there, all ageing is done above
        public Bitmap GeneratePerlinMask(int[] perlinSize, int perlinMultiplier, int perlinResolution)
        {
            int regionSize = 512;

            perlinSize[0] = perlinSize[0] / perlinResolution;
            perlinSize[1] = perlinSize[1] / perlinResolution;

            perlinSize[0] = Math.Max(perlinSize[0], regionSize);
            perlinSize[1] = Math.Max(perlinSize[1], regionSize);

            int regionMultiplier = Math.Min((perlinSize[0] / regionSize) * perlinMultiplier, (perlinSize[1] / regionSize) * perlinMultiplier);

            Bitmap bitmap = new Bitmap(perlinSize[0], perlinSize[1]);

            PerlinNoise noise = new PerlinNoise(8, 0.3, 1.0 / 256, 1.0);

            for (int y = 0; y < perlinSize[1]; y++)
            {
                for (int x = 0; x < perlinSize[0]; x++)
                {
                    double c = Math.Max(0.0f, Math.Min(1.0f, (float)noise.Function2D(regionMultiplier * x, regionMultiplier * y) * 0.5f + 0.5f));
                    byte b = (byte)(c * 255);

                    bitmap.SetPixel(x, y, Color.FromArgb(b, b, b));
                }
            }

            return bitmap;
        }