private void Draw() { int w = (int)canvas1.ActualWidth; int h = (int)canvas1.ActualHeight; double p; double frequency = 0.5; while (App_Started) { XOff += 0.05; if (RbRandom.IsChecked.Value) { if (CBWide.IsChecked.Value) { p = 0.1 + 0.8 * Rnd.NextDouble(); } else { p = 0.2 + 0.6 * Rnd.NextDouble(); } } else if (RbPerlin.IsChecked.Value) { if (CBWide.IsChecked.Value) { p = PerlinNoise.WideNoise(XOff * frequency, Octaves, Persistence, factor); } else { p = PerlinNoise.Noise(XOff * frequency, Octaves, Persistence); } } else if (RbOpenSimplex.IsChecked.Value) { if (CBWide.IsChecked.Value) { p = OpenSimplexNoise.WideNoise(XOff * frequency, Octaves, Persistence, factor); } else { p = OpenSimplexNoise.Noise(XOff * frequency, Octaves, Persistence); } } else //Use FastSimplexNoise { if (CBWide.IsChecked.Value) { p = FastSimplexNoise.WideNoise(XOff * frequency / 2, Octaves, Persistence, factor); } else { p = FastSimplexNoise.Noise(XOff * frequency / 2, Octaves, Persistence); } } Line l = new Line() { StrokeThickness = 2 }; if (CBColor.IsChecked.Value) { l.Stroke = new SolidColorBrush(Rainbow[(int)(255 * p)]); } else { l.Stroke = Brushes.Gray; } if (lines.Count() > 1) { l.Y1 = lines[lines.Count - 2].Y2; l.Y2 = (1 - p) * h; } else { l.Y1 = (1 - p) * h; l.Y2 = (1 - p) * h; } lines.Add(l); if (lines.Count > w) { lines.RemoveAt(0); } //Move the line 1 to the left for (int I = 1; I < lines.Count; I++) { lines[I].X1 = I - 1; lines[I].X2 = I; } canvas1.Children.Clear(); for (int I = 0; I < lines.Count; I++) { canvas1.Children.Add(lines[I]); } Dispatcher.Invoke(Wait, DispatcherPriority.ApplicationIdle); } }
private void Draw4D() { int w = (int)canvas1.ActualWidth; int h = (int)canvas1.ActualHeight; double x; double y; double angle = 0.0; WriteableBitmap Writebitmap; int index = 0; double p; double frequency = 12; if (CBColor.IsChecked.Value) { Writebitmap = new WriteableBitmap(w, h, 96, 96, PixelFormats.Bgra32, null); } else { Writebitmap = new WriteableBitmap(w, h, 96, 96, PixelFormats.Gray8, null); } byte[] PixelData; int Stride = (int)(Writebitmap.PixelWidth * Writebitmap.Format.BitsPerPixel / 8); PixelData = new byte[Stride * Writebitmap.PixelHeight]; while (App_Started) { ZOff = 10 * Math.Cos(angle); WOff = 10 * Math.Sin(angle); for (int I = 0; I < Writebitmap.PixelHeight; I++) { for (int J = 0; J < Writebitmap.PixelWidth; J++) { x = J / (double)Writebitmap.PixelHeight; y = I / (double)Writebitmap.PixelHeight; index = I * Writebitmap.PixelWidth + J; if (RbRandom.IsChecked.Value) { if (CBWide.IsChecked.Value) { p = 0.1 + 0.8 * Rnd.NextDouble(); } else { p = 0.2 + 0.6 * Rnd.NextDouble(); } } else if (RbPerlin.IsChecked.Value) { if (CBWide.IsChecked.Value) { p = PerlinNoise.WideNoise4D(x * frequency, y * frequency, ZOff, WOff, Octaves, Persistence, factor); } else { p = PerlinNoise.Noise4D(x * frequency, y * frequency, ZOff, WOff, Octaves, Persistence); } } else if (RbOpenSimplex.IsChecked.Value) { if (CBWide.IsChecked.Value) { p = OpenSimplexNoise.WideNoise4D(x * frequency, y * frequency, ZOff, WOff, Octaves, Persistence, factor); } else { p = OpenSimplexNoise.Noise4D(x * frequency, y * frequency, ZOff, WOff, Octaves, Persistence); } } else //Use FastSimplexNoise { if (CBWide.IsChecked.Value) { p = FastSimplexNoise.WideNoise4D(x * frequency / 2, y * frequency / 2, ZOff, WOff, Octaves, Persistence, factor); } else { p = FastSimplexNoise.Noise4D(x * frequency / 2, y * frequency / 2, ZOff, WOff, Octaves, Persistence); } } if (CBColor.IsChecked.Value) { SetPixel(J, I, Rainbow[(int)(255 * p)], PixelData, Stride); } else { PixelData[I * Stride + J] = (byte)(255 * p); } } } Int32Rect Intrect = new Int32Rect(0, 0, Writebitmap.PixelWidth - 1, Writebitmap.PixelHeight - 1); Writebitmap.WritePixels(Intrect, PixelData, Stride, 0); Image1.Source = Writebitmap; Dispatcher.Invoke(Wait, DispatcherPriority.ApplicationIdle); angle += 0.002 * Math.PI; if (angle >= 2 * Math.PI) { angle = 0; } } }