示例#1
0
        public PixelCollection BresenhamCircle(PixelCollection pixels, Point center,
                                               double radius, Color colour)
        {
            double x        = radius;
            double y        = 0;
            double decition = 0;

            while (x >= y)
            {
                if (decition <= 0)
                {
                    y++;
                    decition += 2 * y + 1;
                }

                if (decition > 0)
                {
                    x--;
                    decition -= 2 * x + 1;
                }
                pixels.SetPixel((int)(center.X + x), (int)(center.Y + y), colour);
                pixels.SetPixel((int)(center.X + y), (int)(center.Y + x), colour);
                pixels.SetPixel((int)(center.X - y), (int)(center.Y + x), colour);
                pixels.SetPixel((int)(center.X - x), (int)(center.Y + y), colour);
                pixels.SetPixel((int)(center.X - x), (int)(center.Y - y), colour);
                pixels.SetPixel((int)(center.X - y), (int)(center.Y - x), colour);
                pixels.SetPixel((int)(center.X + y), (int)(center.Y - x), colour);
                pixels.SetPixel((int)(center.X + x), (int)(center.Y - y), colour);
            }
            return(pixels);
        }
示例#2
0
        public PixelCollection FloodFill(PixelCollection pixels, Pixel fillPixel, Color targetColor, Color replacementColor)
        {
            if (targetColor.Equals(replacementColor))
            {
                return(pixels);
            }
            if (!fillPixel.Color.Equals(targetColor))
            {
                return(pixels);
            }
            var pixelQueue = new Queue <Pixel>();

            pixels.SetPixel(fillPixel, replacementColor);
            pixelQueue.Enqueue(fillPixel);
            Pixel currentPixel;

            while (pixelQueue.Count > 0)
            {
                currentPixel = pixelQueue.Dequeue();
                var northPixel = pixels.GetPixelOrNull(currentPixel.Column, currentPixel.Row - 1);
                var southPixel = pixels.GetPixelOrNull(currentPixel.Column, currentPixel.Row + 1);
                var eastPixel  = pixels.GetPixelOrNull(currentPixel.Column + 1, currentPixel.Row);
                var westPixel  = pixels.GetPixelOrNull(currentPixel.Column - 1, currentPixel.Row);
                TryToColorPixel(pixels, pixelQueue, northPixel, targetColor, replacementColor);
                TryToColorPixel(pixels, pixelQueue, southPixel, targetColor, replacementColor);
                TryToColorPixel(pixels, pixelQueue, eastPixel, targetColor, replacementColor);
                TryToColorPixel(pixels, pixelQueue, westPixel, targetColor, replacementColor);
            }
            return(pixels);
        }
示例#3
0
 private void TryToColorPixel(PixelCollection pixels, Queue <Pixel> pixelQueue, Pixel currentPixel,
                              Color targetColor, Color replacementColor)
 {
     if (currentPixel != null && targetColor.Equals(currentPixel.Color))
     {
         pixels.SetPixel(currentPixel, replacementColor);
         pixelQueue.Enqueue(currentPixel);
     }
 }
示例#4
0
 public PixelCollection Rectangle(PixelCollection pixels, Point startPoint, Point endPoint,
                                  Color colour)
 {
     if (startPoint.X > endPoint.X)
     {
         Swapper.Swap(ref startPoint, ref endPoint);
     }
     for (int i = (int)startPoint.X; i < endPoint.X; i++)
     {
         pixels.SetPixel(i, (int)startPoint.Y, colour);
         pixels.SetPixel(i, (int)endPoint.Y, colour);
     }
     if (startPoint.Y > endPoint.Y)
     {
         Swapper.Swap(ref startPoint, ref endPoint);
     }
     for (int i = (int)startPoint.Y; i < endPoint.Y; i++)
     {
         pixels.SetPixel((int)startPoint.X, i, colour);
         pixels.SetPixel((int)endPoint.X, i, colour);
     }
     return(pixels);
 }
示例#5
0
        public PixelCollection BresenhamLine(PixelCollection pixels, Point drawingStartPoint,
                                             Point drawingEndPoint, Color colour)
        {
            int lineWidth = (int)drawingEndPoint.X - (int)drawingStartPoint.X;
            int lineHeight = (int)drawingEndPoint.Y - (int)drawingStartPoint.Y;
            int widthDifference1 = 0, heightDifference1 = 0, widthDifference2 = 0, heightDifference2 = 0;

            if (lineWidth < 0)
            {
                widthDifference1 = -1;
            }
            else if (lineWidth > 0)
            {
                widthDifference1 = 1;
            }
            if (lineHeight < 0)
            {
                heightDifference1 = -1;
            }
            else if (lineHeight > 0)
            {
                heightDifference1 = 1;
            }
            if (lineWidth < 0)
            {
                widthDifference2 = -1;
            }
            else if (lineWidth > 0)
            {
                widthDifference2 = 1;
            }
            int longest  = Math.Abs(lineWidth);
            int shortest = Math.Abs(lineHeight);

            if (!(longest > shortest))
            {
                longest  = Math.Abs(lineHeight);
                shortest = Math.Abs(lineWidth);
                if (lineHeight < 0)
                {
                    heightDifference2 = -1;
                }
                else if (lineHeight > 0)
                {
                    heightDifference2 = 1;
                }
                widthDifference2 = 0;
            }
            int numerator = longest >> 1;

            for (int i = 0; i <= longest; i++)
            {
                pixels.SetPixel((int)drawingStartPoint.X, (int)drawingStartPoint.Y, colour);
                numerator += shortest;
                if (!(numerator < longest))
                {
                    numerator           -= longest;
                    drawingStartPoint.X += widthDifference1;
                    drawingStartPoint.Y += heightDifference1;
                }
                else
                {
                    drawingStartPoint.X += widthDifference2;
                    drawingStartPoint.Y += heightDifference2;
                }
            }
            return(pixels);
        }