예제 #1
0
        public override WriteableBitmap Draw(DrawingArgs args)
        {
            int w             = (int)args.CanvasWidth;
            int h             = (int)args.CanvasHeight;
            var fractalBitmap = new WriteableBitmap(w, h, 96.0, 96.0, PixelFormats.Bgra32, null);

            var width     = (int)fractalBitmap.Width;
            var rawStride = (width * PixelFormats.Bgra32.BitsPerPixel + 7) / 8;

            var points = GeneratePoints(args.StartPoint, args.IterationsCount, args.Scale);

            var       stride    = w * 4;
            var       pixelData = new byte[h * stride];
            Int32Rect rect;

            int i = -1;

            foreach (var point in points)
            {
                if (point.X < w && point.Y < h &&
                    point.X >= 0 && point.Y >= 0)
                {
                    int offset = (int)point.Y * stride + (int)point.X * 4;

                    if (args.ColorMode)
                    {
                        UpdateColor(pixelData, offset);
                    }
                    else
                    {
                        pixelData[offset]     = 255;
                        pixelData[offset + 1] = 255;
                        pixelData[offset + 2] = 255;
                        pixelData[offset + 3] = 255;
                    }

                    i++;
                    if (!args.DelayFlag || i % args.Delay != 0)
                    {
                        continue;
                    }

                    rect = new Int32Rect(0, 0, w, h);
                    fractalBitmap.WritePixels(rect, pixelData, rawStride, 0);

                    // TODO: partial redraw
                    //args.Image.Refresh();
                    //args.Image.InvalidateVisual();
                    //Thread.Sleep(args.Delay);
                }
            }

            rect = new Int32Rect(0, 0, w, h);
            fractalBitmap.WritePixels(rect, pixelData, rawStride, 0);

            return(fractalBitmap);
        }
예제 #2
0
        public override WriteableBitmap Draw(DrawingArgs args)
        {
            /*var fractalBitmap = new WriteableBitmap((int)args.Canvas.ActualWidth, (int)args.Canvas.ActualHeight, 96.0, 96.0, PixelFormats.Bgra32, null);
             * args.Image.Source = fractalBitmap;
             *
             * var width = (int)fractalBitmap.Width;
             * var rawStride = (width * PixelFormats.Bgra32.BitsPerPixel + 7) / 8;
             * var pixelData = new byte[] {255, 255, 255, 255};
             *
             * var startPoint = Mouse.GetPosition(args.Canvas);
             * var scale = args.Scale;
             *
             *
             * for (double imagCoord = 1.2; imagCoord >= -1.2; imagCoord -= 0.01)
             * {
             *  for (double realCoord = -0.6; realCoord <= 1.77; realCoord += 0.01)
             *  {
             *      double realTemp = realCoord;
             *      double imagTemp = imagCoord;
             *      double arg = (realCoord * realCoord) + (imagCoord * imagCoord);
             *      for (int i = 0; (arg < 4) && (i < 40); i++)
             *      {
             *          double realTemp2 = (realTemp * realTemp) - (imagTemp * imagTemp) + realCoord;
             *          imagTemp = (2 * realTemp * imagTemp) + imagCoord;
             *          realTemp = realTemp2;
             *          arg = (realTemp * realTemp) + (imagTemp * imagTemp);
             *
             *          points.Add(new Point(realTemp * scale + startPoint.X, startPoint.Y - imagTemp * scale));
             *      }
             *  }
             * }
             *
             * foreach (var point in points)
             *  if (point.X < args.Canvas.ActualWidth && point.Y < args.Canvas.ActualHeight &&
             *      point.X >= 0 && point.Y >= 0)
             *  {
             *
             *      var rect = new Int32Rect((int)Math.Abs(point.X), (int)Math.Abs(point.Y), 1, 1);
             *
             *      fractalBitmap.WritePixels(rect, pixelData, rawStride, 0);
             *
             *      if (!args.DelayFlag)
             *          continue;
             *      args.Image.Refresh();
             *      Thread.Sleep(args.Delay);
             *  }*/

            return(null);
        }
예제 #3
0
        private void image_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //canvas.UpdateLayout();
            var fractal = Fractals[comboBoxFractals.SelectedIndex];

            var args = new DrawingArgs
            {
                CanvasWidth     = (int)ImageGrid.ActualWidth,
                CanvasHeight    = (int)ImageGrid.ActualHeight,
                StartPoint      = Mouse.GetPosition(ImageGrid),
                ColorMode       = checkBoxColorMode.IsChecked == true,
                Delay           = _delay,
                IterationsCount = _iterationsCount,
                DelayFlag       = checkBoxDelay.IsChecked == true,
                Scale           = _scale
            };


            // TODO: make it at another thread
            DrawFractalThread(fractal, args);
        }
예제 #4
0
        async void DrawFractalThread(AFractal fractal, DrawingArgs args)
        {
            FractalSettingsGrid.IsEnabled = false;
            var bmp = fractal.Draw(args);

            image.Source = bmp;
            FractalSettingsGrid.IsEnabled = true;

            // control size for Julia fractal
            if (fractal.GetType() != typeof(JuliaFractal))
            {
                image.Width  = bmp.Width;
                image.Height = bmp.Height;
            }
            else
            {
                image.Width  = args.CanvasWidth;
                image.Height = args.CanvasHeight;
            }
            image.InvalidateArrange();
            image.InvalidateMeasure();
        }
예제 #5
0
 public override WriteableBitmap Draw(DrawingArgs args)
 {
     return(_affineFractal.Draw(args));
 }
예제 #6
0
 public abstract WriteableBitmap Draw(DrawingArgs args);
예제 #7
0
        public override WriteableBitmap Draw(DrawingArgs args)
        {
            int scale         = (int)args.Scale;
            var h             = args.CanvasHeight;
            var w             = h;
            var fractalBitmap = new WriteableBitmap(w * scale, h * scale, 96.0, 96.0, PixelFormats.Bgra32, null);

            h = h * scale;
            w = w * scale;

            var stride    = w * 4;
            var pixelData = new byte[h * stride];

            double xMin = double.NaN;
            double yMin = double.NaN;
            double xMax = double.NaN;
            double yMax = double.NaN;

            double r = CalculateR(C);

            if (Double.IsNaN(xMin) || Double.IsNaN(xMax) || Double.IsNaN(yMin) || Double.IsNaN(yMax))
            {
                xMin = -r;
                yMin = -r;
                xMax = r;
                yMax = r;
            }
            double xStep = Math.Abs(xMax - xMin) / w;
            double yStep = Math.Abs(yMax - yMin) / h;

            var xyIdx  = new Dictionary <int, IDictionary <int, int> >();
            int maxIdx = 0;

            for (int i = 0; i < w; i++)
            {
                xyIdx.Add(i, new Dictionary <int, int>());
                for (int j = 0; j < h; j++)
                {
                    double x     = xMin + i * xStep;
                    double y     = yMin + j * yStep;
                    var    z     = new Complex(x, y);
                    var    zIter = SqPolyIteration(z, C, args.IterationsCount, r);
                    int    idx   = zIter.Count - 1;
                    if (maxIdx < idx)
                    {
                        maxIdx = idx;
                    }
                    xyIdx[i].Add(j, idx);
                }
            }
            for (int i = 0; i < w; i++)
            {
                for (int j = 0; j < h; j++)
                {
                    int    idx = xyIdx[i][j];
                    double x   = xMin + i * xStep;
                    double y   = yMin + j * yStep;
                    var    z   = new Complex(x, y);

                    var color = ComplexHeatMap(idx, 0, maxIdx, z, r);
                    pixelData[j * stride + (w - i - 1) * 4 + 0] = color.R;
                    pixelData[j * stride + (w - i - 1) * 4 + 1] = color.G;
                    pixelData[j * stride + (w - i - 1) * 4 + 2] = color.B;
                    pixelData[j * stride + (w - i - 1) * 4 + 3] = color.A;
                }
            }

            var rect      = new Int32Rect(0, 0, w - 1, h - 1);
            var rawStride = (w * PixelFormats.Bgra32.BitsPerPixel + 7) / 8;

            fractalBitmap.WritePixels(rect, pixelData, rawStride, 0);

            return(fractalBitmap);
        }