예제 #1
0
파일: Program.cs 프로젝트: nilzen/Shapes
        private static void OutputClippedRectangle()
        {
            var rect1 = new RectangularPolygon(10, 10, 40, 40);
            var rect2 = new RectangularPolygon(20, 0, 20, 20);
            var paths = rect1.Clip(rect2);

            paths.SaveImage("Clipping", "RectangleWithTopClipped.png");
        }
예제 #2
0
        private static IPathCollection BuildCorners(int imageWidth, int imageHeight)
        {
            float widthCut  = imageWidth / 2f;
            float heightCut = imageHeight / 2f;

            var   rect          = new RectangularPolygon(-0.5f, -0.5f, widthCut, heightCut);
            IPath cornerTopLeft =
                rect.Clip(new EllipsePolygon(widthCut - 0.5f, heightCut - 0.5f, imageWidth, imageHeight));

            float rightPos       = imageWidth - cornerTopLeft.Bounds.Width + 1;
            float bottomPos      = imageHeight - cornerTopLeft.Bounds.Height + 1;
            IPath cornerTopRight =
                rect.Clip(new EllipsePolygon(-0.5f, heightCut - 0.5f, imageWidth, imageHeight))
                .Translate(rightPos, 0);
            IPath cornerBottomRight = cornerTopLeft.RotateDegree(180).Translate(rightPos, bottomPos);
            IPath cornerBottomLeft  = cornerTopRight.RotateDegree(180).Translate(-rightPos, bottomPos);

            return(new PathCollection(cornerTopLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight));
        }
예제 #3
0
        public static IPathCollection BuildCorners(int imageWidth, int imageHeight, float cornerRadius)
        {
            var   rect              = new RectangularPolygon(-0.5f, -0.5f, cornerRadius, cornerRadius);
            IPath cornerTopLeft     = rect.Clip(new EllipsePolygon(cornerRadius - 0.5f, cornerRadius - 0.5f, cornerRadius));
            float rightPos          = imageWidth - cornerTopLeft.Bounds.Width + 1;
            float bottomPos         = imageHeight - cornerTopLeft.Bounds.Height + 1;
            IPath cornerTopRight    = cornerTopLeft.RotateDegree(90).Translate(rightPos, 0);
            IPath cornerBottomLeft  = cornerTopLeft.RotateDegree(-90).Translate(0, bottomPos);
            IPath cornerBottomRight = cornerTopLeft.RotateDegree(180).Translate(rightPos, bottomPos);

            return(new PathCollection(cornerTopLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight));
        }
예제 #4
0
        private static IPathCollection BuildCorners(int imageWidth, int imageHeight, float cornerRadius)
        {
            // First create a square
            var rect = new RectangularPolygon(-0.5f, -0.5f, cornerRadius, cornerRadius);

            // Then cut out of the square a circle so we are left with a corner
            var cornerTopLeft = rect.Clip(new EllipsePolygon(cornerRadius - 0.5f, cornerRadius - 0.5f, cornerRadius));

            // Corner is now a corner shape positions top left
            // lets make 3 more positioned correctly, we can do that by translating the original around the center of the image
            var rightPos  = imageWidth - cornerTopLeft.Bounds.Width + 1;
            var bottomPos = imageHeight - cornerTopLeft.Bounds.Height + 1;

            // Move it across the width of the image - the width of the shape
            var cornerTopRight    = cornerTopLeft.RotateDegree(90).Translate(rightPos, 0);
            var cornerBottomLeft  = cornerTopLeft.RotateDegree(-90).Translate(0, bottomPos);
            var cornerBottomRight = cornerTopLeft.RotateDegree(180).Translate(rightPos, bottomPos);

            return(new PathCollection(cornerTopLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight));
        }
예제 #5
0
        IPathCollection BuildCorners(int x, int y, int width, int height, float cornerRadius)
        {
            // first create a square
            var rect = new RectangularPolygon(x - 0.5f, y - 0.5f, cornerRadius, cornerRadius);

            // then cut out of the square a circle so we are left with a corner
            IPath cornerToptLeft = rect.Clip(new EllipsePolygon(x + (cornerRadius - 0.5f), y + (cornerRadius - 0.5f), cornerRadius));

            // corner is now a corner shape positions top left
            //lets make 3 more positioned correctly, we can do that by translating the orgional artound the center of the image
            var center = new PointF(width / 2F, height / 2F);

            float rightPos  = width - cornerToptLeft.Bounds.Width + 1;
            float bottomPos = height - cornerToptLeft.Bounds.Height + 1;

            // move it across the widthof the image - the width of the shape
            IPath cornerTopRight    = cornerToptLeft.RotateDegree(90).Translate(rightPos, 0);
            IPath cornerBottomLeft  = cornerToptLeft.RotateDegree(-90).Translate(0, bottomPos);
            IPath cornerBottomRight = cornerToptLeft.RotateDegree(180).Translate(rightPos, bottomPos);

            return(new PathCollection(cornerToptLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight));
        }
예제 #6
0
        private static IPathCollection BuildCorners(float imageWidth, float imageHeight, float cornerRadiusX, float cornerRadiusY)
        {
            // first create a square
            var rect = new RectangularPolygon(0, 0, cornerRadiusX, cornerRadiusY);

            // then cut out of the square a circle so we are left with a corner
            IPath cornerToptLeft = rect.Clip(new EllipsePolygon(cornerRadiusX, cornerRadiusY, cornerRadiusX * 2, cornerRadiusY * 2));

            // corner is now a corner shape positions top left
            //lets make 3 more positioned correctly, we can do that by translating the orgional artound the center of the image
            var center = new Vector2(imageWidth / 2F, imageHeight / 2F);

            float rightPos  = imageWidth - cornerToptLeft.Bounds.Width;
            float bottomPos = imageHeight - cornerToptLeft.Bounds.Height;

            // move it across the widthof the image - the width of the shape
            IPath cornerTopRight    = cornerToptLeft.RotateDegree(90).Translate(rightPos, 0);
            IPath cornerBottomLeft  = cornerToptLeft.RotateDegree(-90).Translate(0, bottomPos);
            IPath cornerBottomRight = cornerToptLeft.RotateDegree(180).Translate(rightPos, bottomPos);

            return(new PathCollection(cornerToptLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight));
        }
예제 #7
0
        private static IPathCollection BuildCorners(int imageWidth, int imageHeight, float cornerRadius)
        {
            // Create a square
            var rect = new RectangularPolygon(-0.5f, -0.5f, cornerRadius, cornerRadius);

            // then cut out of the square a circle so we are left with a corner
            IPath cornerTopLeft = rect.Clip(new EllipsePolygon(cornerRadius - 0.5f, cornerRadius - 0.5f, cornerRadius));

            // corner is now positioned in the top left
            // let's make 3 more positioned correctly, we do that by translating the top left one
            // around the center of the image
            // center = width / 2, height /2

            float rightPos  = imageWidth - cornerTopLeft.Bounds.Width + 1;
            float bottomPos = imageHeight - cornerTopLeft.Bounds.Height + 1;

            // move it across the width of the image / shape
            IPath cornerTopRight    = cornerTopLeft.RotateDegree(90).Translate(rightPos, 0);
            IPath cornerBottomLeft  = cornerTopLeft.RotateDegree(-90).Translate(0, bottomPos);
            IPath cornerBottomRight = cornerTopLeft.RotateDegree(180).Translate(rightPos, bottomPos);

            return(new PathCollection(cornerTopLeft, cornerBottomLeft, cornerTopRight, cornerBottomRight));
        }
예제 #8
0
        private static Image CutImage(Texture2D m_Texture2D, Sprite m_Sprite, Rectf textureRect, Vector2 textureRectOffset, SpriteSettings settingsRaw)
        {
            var originalImage = m_Texture2D.ConvertToImage(false);

            if (originalImage != null)
            {
                using (originalImage)
                {
                    var rectX      = (int)Math.Floor(textureRect.x);
                    var rectY      = (int)Math.Floor(textureRect.y);
                    var rectRight  = (int)Math.Ceiling(textureRect.x + textureRect.width);
                    var rectBottom = (int)Math.Ceiling(textureRect.y + textureRect.height);
                    rectRight  = Math.Min(rectRight, m_Texture2D.m_Width);
                    rectBottom = Math.Min(rectBottom, m_Texture2D.m_Height);
                    var rect        = new Rectangle(rectX, rectY, rectRight - rectX, rectBottom - rectY);
                    var spriteImage = originalImage.Clone(x => x.Crop(rect));
                    if (settingsRaw.packed == 1)
                    {
                        //RotateAndFlip
                        switch (settingsRaw.packingRotation)
                        {
                        case SpritePackingRotation.kSPRFlipHorizontal:
                            spriteImage.Mutate(x => x.Flip(FlipMode.Horizontal));
                            break;

                        case SpritePackingRotation.kSPRFlipVertical:
                            spriteImage.Mutate(x => x.Flip(FlipMode.Vertical));
                            break;

                        case SpritePackingRotation.kSPRRotate180:
                            spriteImage.Mutate(x => x.Rotate(180));
                            break;

                        case SpritePackingRotation.kSPRRotate90:
                            spriteImage.Mutate(x => x.Rotate(270));
                            break;
                        }
                    }

                    //Tight
                    if (settingsRaw.packingMode == SpritePackingMode.kSPMTight)
                    {
                        try
                        {
                            var             triangles = GetTriangles(m_Sprite.m_RD);
                            var             polygons  = triangles.Select(x => new Polygon(new LinearLineSegment(x.Select(y => new PointF(y.X, y.Y)).ToArray()))).ToArray();
                            IPathCollection path      = new PathCollection(polygons);
                            var             matrix    = Matrix3x2.CreateScale(m_Sprite.m_PixelsToUnits);
                            var             version   = m_Sprite.version;
                            if (version[0] < 5 ||
                                (version[0] == 5 && version[1] < 4) ||
                                (version[0] == 5 && version[1] == 4 && version[2] <= 1))   //5.4.1p3 down
                            {
                                matrix *= Matrix3x2.CreateTranslation(m_Sprite.m_Rect.width * 0.5f - textureRectOffset.X, m_Sprite.m_Rect.height * 0.5f - textureRectOffset.Y);
                            }
                            else
                            {
                                matrix *= Matrix3x2.CreateTranslation(m_Sprite.m_Rect.width * m_Sprite.m_Pivot.X - textureRectOffset.X, m_Sprite.m_Rect.height * m_Sprite.m_Pivot.Y - textureRectOffset.Y);
                            }
                            path = path.Transform(matrix);
                            var options = new DrawingOptions
                            {
                                GraphicsOptions = new GraphicsOptions()
                                {
                                    AlphaCompositionMode = PixelAlphaCompositionMode.DestOut
                                }
                            };
                            var rectP = new RectangularPolygon(0, 0, rect.Width, rect.Height);
                            spriteImage.Mutate(x => x.Fill(options, SixLabors.ImageSharp.Color.Red, rectP.Clip(path)));
                            spriteImage.Mutate(x => x.Flip(FlipMode.Vertical));
                            return(spriteImage);
                        }
                        catch
                        {
                            // ignored
                        }
                    }

                    //Rectangle
                    spriteImage.Mutate(x => x.Flip(FlipMode.Vertical));
                    return(spriteImage);
                }
            }

            return(null);
        }
예제 #9
0
        private static IPath GetClippedRect(int imageWidth, int imageHeight, float cornerRadius)
        {
            var rect = new RectangularPolygon(-0.5f, -0.5f, imageWidth + 0.5f, imageHeight + 0.5f);

            return(rect.Clip(new EllipsePolygon(imageWidth * 0.5f, imageHeight * 0.5f, cornerRadius)));
        }