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);
                    }
                }
            });
        }
        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;
        }
        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;
            });
        }