Example #1
0
        private void WorleyNoise()
        {
            Color my_Color;

            PixelData = new byte[(int)(Stride * Image1.Height)];
            //Create the seed points
            Seeds = new List <Point>();
            for (int I = 0; I < SeedCount; I++)
            {
                Seeds.Add(new Point(Rnd.NextDouble() * Canvas1.ActualWidth, Rnd.NextDouble() * Canvas1.ActualHeight));
            }
            if (UseColor)
            {
                double[] distances = new double[SeedCount];
                byte     R, G, B;
                for (int X = 0; X < W; X++)
                {
                    for (int Y = 0; Y < H; Y++)
                    {
                        for (int I = 0; I < SeedCount; I++)
                        {
                            distances[I] = Math.Sqrt((Seeds[I].X - X) * (Seeds[I].X - X) + (Seeds[I].Y - Y) * (Seeds[I].Y - Y));
                        }
                        Array.Sort(distances);
                        if (distances[0] > maxDistance)
                        {
                            distances[0] = maxDistance - 1;
                        }
                        if (distances[1] > maxDistance)
                        {
                            distances[1] = maxDistance - 1;
                        }
                        if (distances[2] > maxDistance)
                        {
                            distances[2] = maxDistance - 1;
                        }
                        R = (byte)(255 * (maxDistance - distances[1]) / maxDistance);
                        G = (byte)(255 * (maxDistance - distances[2]) / maxDistance);
                        B = (byte)(255 * (maxDistance - distances[0]) / maxDistance);

                        my_Color = Color.FromRgb(R, G, B);
                        SetPixel(X, Y, my_Color, PixelData, Stride);
                    }
                }
            }
            else
            {
                double[]     distances = new double[SeedCount];
                ColorPalette pal       = new ColorPalette(Environment.CurrentDirectory + "\\ThermalGrayScale.cpl");
                List <Color> my_Colors = pal.GetColors((int)maxDistance);
                for (int X = 0; X < W; X++)
                {
                    for (int Y = 0; Y < H; Y++)
                    {
                        for (int I = 0; I < SeedCount; I++)
                        {
                            distances[I] = Math.Sqrt((Seeds[I].X - X) * (Seeds[I].X - X) + (Seeds[I].Y - Y) * (Seeds[I].Y - Y));
                        }
                        Array.Sort(distances);
                        if (distances[0] > maxDistance)
                        {
                            distances[0] = maxDistance - 1;
                        }
                        my_Color = my_Colors[(int)(maxDistance - distances[0])];
                        SetPixel(X, Y, my_Color, PixelData, Stride);
                    }
                }
            }

            bitmap        = BitmapSource.Create(W, H, 96, 96, PixelFormats.Rgb24, null, PixelData, Stride);
            Image1.Source = bitmap;
        }