async void OnCalculateButtonClicked(object sender, EventArgs args) { // Configure the UI for a background process. calculateButton.IsEnabled = false; cancelButton.IsEnabled = true; cancelTokenSource = new CancellationTokenSource(); try { // Render the Mandelbrot set on a bitmap. BmpMaker bmpMaker = await CalculateMandelbrotAsync(progressReporter, cancelTokenSource.Token); image.Source = bmpMaker.Generate(); } catch (OperationCanceledException) { calculateButton.IsEnabled = true; progressBar.Progress = 0; } catch (Exception) { // Shouldn't occur in this case. } cancelButton.IsEnabled = false; }
private void DisplayNewBitmap(BitmapInfo bitmapInfo) { BmpMaker bmpMaker = new BmpMaker(bitmapInfo.PixelWidth, bitmapInfo.PixelHeight); int index = 0; for (int row = 0; row < bitmapInfo.PixelHeight; row++) { for (int col = 0; col < bitmapInfo.PixelWidth; col++) { int iterationCount = bitmapInfo.IterationCounts[index++]; if (iterationCount == -1) { bmpMaker.SetPixel(row, col, 0, 0, 0); } else { double proportion = (iterationCount / 32.0) % 1; if (proportion < 0.5) { bmpMaker.SetPixel(row, col, (int)(255 * (1 - 2 * proportion)), 0, (int)(255 * 2 * proportion)); } else { proportion = 2 * (proportion - 0.5); bmpMaker.SetPixel(row, col, 0, (int)(255 * proportion), (int)(255 * (1 - proportion))); } } } } image.Source = bmpMaker.Generate(); }
Task CalculateMandelbrotAsync(BmpMaker bmpMaker) { return(Task.Run(() => { for (int row = 0; row < pixelWidth; row++) { double y = center.Imaginary - size.Height / 2 + row * size.Height / pixelHeight; for (int col = 0; col < pixelHeight; 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 MandelbrotXFPage() { InitializeComponent(); mandelbrotViewModel = new MandelbrotViewModel(2.5, 2.5) { PixelWidth = 1000, PixelHeight = 1000, CurrentCenter = new Complex(GetProperty("CenterReal", -0.75), GetProperty("CenterImaginary", 0.0)), CurrentMagnification = GetProperty("Magnification", 1.0), TargetMagnification = GetProperty("Magnification", 1.0), Iterations = GetProperty("Iterations", 8), RealOffset = 0.5, ImaginaryOffset = 0.5 }; BindingContext = mandelbrotViewModel; mandelbrotViewModel.PropertyChanged += OnMandelbrotViewModelPropertyChanged; BmpMaker bmpMaker = new BmpMaker(120, 120); testImage.SizeChanged += (sender, args) => { pixelsPerUnit = bmpMaker.Width / testImage.Width; SetPixelWidthAndHeight(); }; testImage.Source = bmpMaker.Generate(); Device.StartTimer(TimeSpan.FromMilliseconds(100), () => { realCrossHair.Opacity -= 0.01f; imageCrossHair.Opacity -= 0.01f; return(true); }); }
async void OnCalculateButtonClicked(object sender, EventArgs e) { calculateButton.IsEnabled = false; BmpMaker bmpMaker = await CalculateMandelbrotAsync(progressReporter); image.Source = bmpMaker.Generate(); }
async void OnCalculateButtonClicked(object sender, EventArgs args) { // Configure the UI for a background process. calculateButton.IsEnabled = false; // Render the Mandelbrot set on a bitmap. BmpMaker bmpMaker = await CalculateMandelbrotAsync(progressReporter); image.Source = bmpMaker.Generate(); }
private async void OnCalculateButtonClicked(object sender, EventArgs e) { calculateButton.IsEnabled = false; activityIndicator.IsRunning = true; BmpMaker bmpMaker = new BmpMaker(pixelWidth, pixelHeight); await CalculateMandelbrotAsync(bmpMaker); image.Source = bmpMaker.Generate(); activityIndicator.IsRunning = false; }
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)); }
async void OnCalculateButtonClicked(object sender, EventArgs args) { // Configure the UI for a background process. calculateButton.IsEnabled = false; activityIndicator.IsRunning = true; // Render the Mandelbrot set on a bitmap. BmpMaker bmpMaker = new BmpMaker(pixelWidth, pixelHeight); await CalculateMandelbrotAsync(bmpMaker); image.Source = bmpMaker.Generate(); // Configure the UI for the completed background process. activityIndicator.IsRunning = false; }
void DisplayNewBitmap(BitmapInfo bitmapInfo) { // Create the bitmap. BmpMaker bmpMaker = new BmpMaker(bitmapInfo.PixelWidth, bitmapInfo.PixelHeight); // Set the colors. int index = 0; for (int row = 0; row < bitmapInfo.PixelHeight; row++) { for (int col = 0; col < bitmapInfo.PixelWidth; col++) { int iterationCount = bitmapInfo.IterationCounts[index++]; // In the Mandelbrot set: Color black. if (iterationCount == -1) { bmpMaker.SetPixel(row, col, 0, 0, 0); } // Not in the Mandelbrot set: Pick a color based on count. else { double proportion = (iterationCount / 32.0) % 1; if (proportion < 0.5) { bmpMaker.SetPixel(row, col, (int)(255 * (1 - 2 * proportion)), 0, (int)(255 * 2 * proportion)); } else { proportion = 2 * (proportion - 0.5); bmpMaker.SetPixel(row, col, 0, (int)(255 * proportion), (int)(255 * (1 - proportion))); } } } } image.Source = bmpMaker.Generate(); }
public MandelbrotXFPage() { InitializeComponent(); // Instantiate ViewModel and get saved values. mandelbrotViewModel = new MandelbrotViewModel(2.5, 2.5) { PixelWidth = 1000, PixelHeight = 1000, CurrentCenter = new Complex(GetProperty("CenterReal", -0.75), GetProperty("CenterImaginary", 0.0)), CurrentMagnification = GetProperty("Magnification", 1.0), TargetMagnification = GetProperty("Magnification", 1.0), Iterations = GetProperty("Iterations", 8), RealOffset = 0.5, ImaginaryOffset = 0.5 }; // Set BindingContext on page. BindingContext = mandelbrotViewModel; // Set PropertyChanged handler on ViewModel for "manual" processing. mandelbrotViewModel.PropertyChanged += OnMandelbrotViewModelPropertyChanged; // Create test image to obtain pixels per device-independent unit. BmpMaker bmpMaker = new BmpMaker(120, 120); testImage.SizeChanged += (sender, args) => { pixelsPerUnit = bmpMaker.Width / testImage.Width; SetPixelWidthAndHeight(); }; testImage.Source = bmpMaker.Generate(); // Gradually reduce opacity of cross-hairs. Device.StartTimer(TimeSpan.FromMilliseconds(100), () => { realCrossHair.Opacity -= 0.01; imagCrossHair.Opacity -= 0.01; return(true); }); }
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; })); }
async void OnCalculateButtonClicked(object sender, EventArgs e) { calculateButton.IsEnabled = false; cancelButton.IsEnabled = true; cancelTokenSource = new CancellationTokenSource(); try { BmpMaker bmpMaker = await CalculateMandelbrotAsync(progressReporter, cancelTokenSource.Token); image.Source = bmpMaker.Generate(); } catch (OperationCanceledException) { calculateButton.IsEnabled = true; progressBar.Progress = 0; } catch (Exception) { } cancelButton.IsEnabled = false; }