Пример #1
0
        /// <summary>
        /// Transforms the rectangle with the current matrix.
        /// </summary>
        /// <remarks>
        /// This returns a rectangle that encompasses the specified <paramref name="rect"/> after it is translated.
        /// When rotating, this means that the new rectangle may be larger in size to encompass the translated rectangle.
        /// </remarks>
        /// <returns>A new rectangle that encompasses the translated <paramref name="rect"/>.</returns>
        /// <param name="matrix">Matrix to transform each point of the rectangle.</param>
        /// <param name="rect">Rectangle to transform.</param>
        public static RectangleF TransformRectangle(this IMatrix matrix, RectangleF rect)
        {
            var points = new[]
            {
                matrix.TransformPoint(rect.TopLeft),
                matrix.TransformPoint(rect.TopRight),
                matrix.TransformPoint(rect.BottomLeft),
                matrix.TransformPoint(rect.BottomRight)
            };

            return(RectangleF.FromSides(points.Min(r => r.X), points.Min(r => r.Y), points.Max(r => r.X), points.Max(r => r.Y)));
        }
Пример #2
0
        /// <summary>
        /// Creates a rounded rectangle using the specified corner radius
        /// </summary>
        /// <param name="rectangle">Rectangle to round</param>
        /// <param name="nwRadius">Radius of the north east corner</param>
        /// <param name="neRadius">Radius of the north west corner</param>
        /// <param name="seRadius">Radius of the south east corner</param>
        /// <param name="swRadius">Radius of the south west corner</param>
        /// <returns>GraphicsPath with the lines of the rounded rectangle ready to be painted</returns>
        public static IGraphicsPath GetRoundRect(RectangleF rectangle, float nwRadius, float neRadius, float seRadius, float swRadius)
        {
            //  NW-----NE
            //  |       |
            //  |       |
            //  SW-----SE

            var result = GraphicsPath.Create();

            nwRadius *= 2;
            neRadius *= 2;
            seRadius *= 2;
            swRadius *= 2;

            //NW ---- NE
            result.AddLine(new PointF(rectangle.X + nwRadius, rectangle.Y), new PointF(rectangle.Right - neRadius, rectangle.Y));

            //NE Arc
            if (neRadius > 0f)
            {
                var rect = RectangleF.FromSides(rectangle.Right - neRadius, rectangle.Top, rectangle.Right, rectangle.Top + neRadius);
                result.AddArc(rect, -90, 90);
            }

            // NE
            //  |
            // SE
            result.AddLine(new PointF(rectangle.Right, rectangle.Top + neRadius), new PointF(rectangle.Right, rectangle.Bottom - seRadius));

            //SE Arc
            if (seRadius > 0f)
            {
                var rect = RectangleF.FromSides(rectangle.Right - seRadius, rectangle.Bottom - seRadius, rectangle.Right, rectangle.Bottom);
                result.AddArc(rect, 0, 90);
            }

            // SW --- SE
            result.AddLine(new PointF(rectangle.Right - seRadius, rectangle.Bottom), new PointF(rectangle.Left + swRadius, rectangle.Bottom));

            //SW Arc
            if (swRadius > 0f)
            {
                var rect = RectangleF.FromSides(rectangle.Left, rectangle.Bottom - swRadius, rectangle.Left + swRadius, rectangle.Bottom);
                result.AddArc(rect, 90, 90);
            }

            // NW
            // |
            // SW
            result.AddLine(new PointF(rectangle.Left, rectangle.Bottom - swRadius), new PointF(rectangle.Left, rectangle.Top + nwRadius));

            //NW Arc
            if (nwRadius > 0f)
            {
                var rect = RectangleF.FromSides(rectangle.Left, rectangle.Top, rectangle.Left + nwRadius, rectangle.Top + nwRadius);
                result.AddArc(rect, 180, 90);
            }

            result.CloseFigure();

            return(result);
        }