Task CalculateMandelbrotAsync(BmpMaker bmpMaker) { return Task.Run(() => { for (int row = 0; row < pixelHeight; row++) { double y = center.Imaginary - size.Height / 2 + row * size.Height / pixelHeight; for (int col = 0; col < pixelWidth; col++) { double x = center.Real - size.Width / 2 + col * size.Width / pixelWidth; Complex c = new Complex(x, y); Complex z = 0; int iteration = 0; do { z = z * z + c; iteration++; } while (iteration < iterations && z.MagnitudeSquared < 4); bool isMandelbrotSet = iteration == iterations; bmpMaker.SetPixel(row, col, isMandelbrotSet ? Color.Black : Color.White); } } }); }
public DiyGradientBitmapPage() { InitializeComponent(); int rows = 128; int cols = 64; BmpMaker bmpMaker = new BmpMaker(cols, rows); for (int row = 0; row < rows; row++) for (int col = 0; col < cols; col++) { bmpMaker.SetPixel(row, col, 2 * row, 0, 2 * (128 - row)); } ImageSource imageSource = bmpMaker.Generate(); image.Source = imageSource; }
Task<BmpMaker> CalculateMandelbrotAsync(IProgress<double> progress) { return Task.Run<BmpMaker>(() => { BmpMaker bmpMaker = new BmpMaker(pixelWidth, pixelHeight); for (int row = 0; row < pixelHeight; row++) { double y = center.Imaginary - size.Height / 2 + row * size.Height / pixelHeight; // Report the progress. progress.Report((double)row / pixelHeight); for (int col = 0; col < pixelWidth; col++) { double x = center.Real - size.Width / 2 + col * size.Width / pixelWidth; Complex c = new Complex(x, y); Complex z = 0; int iteration = 0; bool isMandelbrotSet = false; if ((c - new Complex(-1, 0)).MagnitudeSquared < 1.0 / 16) { isMandelbrotSet = true; } else { do { z = z * z + c; iteration++; } while (iteration < iterations && z.MagnitudeSquared < 4); isMandelbrotSet = iteration == iterations; } bmpMaker.SetPixel(row, col, isMandelbrotSet ? Color.Black : Color.White); } } return bmpMaker; }); }
Task<BmpMaker> CalculateMandelbrotAsync(IProgress<double> progress, CancellationToken cancelToken) { return Task.Run<BmpMaker>(() => { BmpMaker bmpMaker = new BmpMaker(pixelWidth, pixelHeight); for (int row = 0; row < pixelHeight; row++) { double y = center.Imaginary - size.Height / 2 + row * size.Height / pixelHeight; // Report the progress. progress.Report((double)row / pixelHeight); // Possibly cancel. cancelToken.ThrowIfCancellationRequested(); for (int col = 0; col < pixelWidth; col++) { double x = center.Real - size.Width / 2 + col * size.Width / pixelWidth; Complex c = new Complex(x, y); Complex z = 0; int iteration = 0; bool isMandelbrotSet = false; if ((c - new Complex(-1, 0)).MagnitudeSquared < 1.0 / 16) { isMandelbrotSet = true; } // http://www.reenigne.org/blog/algorithm-for-mandelbrot-cardioid/ else if (c.MagnitudeSquared * (8 * c.MagnitudeSquared - 3) < 3.0 / 32 - c.Real) { isMandelbrotSet = true; } else { do { z = z * z + c; iteration++; } while (iteration < iterations && z.MagnitudeSquared < 4); isMandelbrotSet = iteration == iterations; } bmpMaker.SetPixel(row, col, isMandelbrotSet ? Color.Black : Color.White); } } return bmpMaker; }, cancelToken); }