/// <summary>
 /// Adds a round rectangle to the graphics path where the integer radius specified determines how rounded the rectangle should become.
 /// This can be thought of rounded arcs connected by straight lines.
 /// </summary>
 /// <param name="self"></param>
 /// <param name="bounds"></param>
 /// <param name="radius"></param>
 public static void AddRoundedRectangle(this GraphicsPath self, Rectangle bounds, int radius)
 {
     if (radius * 2 > bounds.Width)
     {
         self.AddEllipse(bounds);
         return;
     }
     if (radius <= 0)
     {
         self.AddRectangle(bounds);
         return;
     }
     int w = radius * 2;
     Rectangle br = new Rectangle(bounds.Right - w, bounds.Bottom - w, w, w);
     Rectangle bl = new Rectangle(bounds.Left, bounds.Bottom - w, w, w);
     Rectangle tl = new Rectangle(bounds.Left, bounds.Top, w, w);
     Rectangle tr = new Rectangle(bounds.Right - w, bounds.Top, w, w);
     self.AddArc(br, 0, 90F);
     self.AddLine(new Point(bounds.Right - radius, bounds.Bottom), new Point(bounds.Left + radius, bounds.Bottom));
     self.AddArc(bl, 90F, 90F);
     self.AddLine(new Point(bounds.Left, bounds.Bottom - radius), new Point(bounds.Left, bounds.Top + radius));
     self.AddArc(tl, 180F, 90F);
     self.AddLine(new Point(bounds.Left + radius, bounds.Top), new Point(bounds.Right - radius, bounds.Top));
     self.AddArc(tr, 270F, 90F);
     self.AddLine(new Point(bounds.Right, bounds.Top + radius), new Point(bounds.Right, bounds.Bottom - radius));
     self.CloseFigure();
 }
        /// <summary>
        /// Adds a rectangle with round corners to the current GraphicsPath.
        /// </summary>
        /// <param name="gp">The current GraphicsPath object.</param>
        /// <param name="point">The coordinates of the upper left corner.</param>
        /// <param name="size">The dimension of the rectangle.</param>
        /// <param name="radius">The radius of the different corners starting with the upper left corner.</param>
        /// <returns>The current GraphicsPath object.</returns>
        public static GraphicsPath AddRoundRectangle(this GraphicsPath gp, PointF point, SizeF size, params float[] radius)
        {
            var r = new float[4];
            var d = new float[4];
            var bottom = point.Y + size.Height;
            var right = point.X + size.Width;

            for (int i = 0; i < r.Length; i++)
            {
                r[i] = radius.Length > 0 ? radius[i % radius.Length] : 0f;
                d[i] = 2f * r[i];
            }

            if (r[0] > 0f)
                gp.AddArc(new RectangleF(point.X, point.Y, d[0], d[0]), 180, 90);

            gp.AddLine(new PointF(point.X + r[0], point.Y), new PointF(right - r[1], point.Y));

            if (r[1] > 0f)
                gp.AddArc(new RectangleF(right - d[1], point.Y, d[1], d[1]), 270, 90);

            gp.AddLine(new PointF(right, point.Y + r[1]), new PointF(right, bottom - r[2]));

            if (r[2] > 0f)
                gp.AddArc(new RectangleF(right - d[2], bottom - d[2], d[2], d[2]), 0, 90);

            gp.AddLine(new PointF(right - r[2], bottom), new PointF(point.X + r[3], bottom));

            if (r[3] > 0f)
                gp.AddArc(new RectangleF(point.X, bottom - d[3], d[3], d[3]), 90, 90);

            gp.AddLine(new PointF(point.X, bottom - r[3]), new PointF(point.X, point.Y + r[0]));
            gp.CloseFigure();
            return gp;
        }
Esempio n. 3
0
 /// <summary>
 /// Adds the unclosed set of lines that are the top and left of the shape
 /// </summary>
 /// <param name="self"></param>
 /// <param name="bounds"></param>
 /// <param name="radius"></param>
 public static void AddRoundedRectangleTopLeft(this GraphicsPath self, Rectangle bounds, int radius)
 {
     if (radius * 2 > bounds.Width)
     {
         self.AddArc(bounds, 180F, 90F);
         return;
     }
     int w = radius * 2;
     Rectangle tl = new Rectangle(bounds.Left, bounds.Top, w, w);
     self.AddLine(new Point(bounds.Left, bounds.Bottom - radius), new Point(bounds.Left, bounds.Top + radius));
     self.AddArc(tl, 180F, 90F);
     self.AddLine(new Point(bounds.Left + radius, bounds.Top), new Point(bounds.Right - radius, bounds.Top));
 }
Esempio n. 4
0
        public static void AddRoundedRectangle(this GraphicsPath graphicsPath, RectangleF rect, float radius, float penWidth = 1)
        {
            if (radius <= 0f)
            {
                graphicsPath.AddRectangleProper(rect);
            }
            else
            {
                if (penWidth == 1)
                {
                    rect = new RectangleF(rect.X, rect.Y, rect.Width - 1, rect.Height - 1);
                }

                // If the corner radius is greater than or equal to
                // half the width, or height (whichever is shorter)
                // then return a capsule instead of a lozenge
                if (radius >= (Math.Min(rect.Width, rect.Height) / 2.0f))
                {
                    graphicsPath.AddCapsule(rect);
                }
                else
                {
                    // Create the arc for the rectangle sides and declare
                    // a graphics path object for the drawing
                    float diameter = radius * 2.0f;
                    SizeF size = new SizeF(diameter, diameter);
                    RectangleF arc = new RectangleF(rect.Location, size);

                    // Top left arc
                    graphicsPath.AddArc(arc, 180, 90);

                    // Top right arc
                    arc.X = rect.Right - diameter;
                    graphicsPath.AddArc(arc, 270, 90);

                    // Bottom right arc
                    arc.Y = rect.Bottom - diameter;
                    graphicsPath.AddArc(arc, 0, 90);

                    // Bottom left arc
                    arc.X = rect.Left;
                    graphicsPath.AddArc(arc, 90, 90);

                    graphicsPath.CloseFigure();
                }
            }
        }
Esempio n. 5
0
        public static void AddCapsule(this GraphicsPath graphicsPath, RectangleF rect)
        {
            float diameter;
            RectangleF arc;

            try
            {
                if (rect.Width > rect.Height)
                {
                    // Horizontal capsule
                    diameter = rect.Height;
                    SizeF sizeF = new SizeF(diameter, diameter);
                    arc = new RectangleF(rect.Location, sizeF);
                    graphicsPath.AddArc(arc, 90, 180);
                    arc.X = rect.Right - diameter;
                    graphicsPath.AddArc(arc, 270, 180);
                }
                else if (rect.Width < rect.Height)
                {
                    // Vertical capsule
                    diameter = rect.Width;
                    SizeF sizeF = new SizeF(diameter, diameter);
                    arc = new RectangleF(rect.Location, sizeF);
                    graphicsPath.AddArc(arc, 180, 180);
                    arc.Y = rect.Bottom - diameter;
                    graphicsPath.AddArc(arc, 0, 180);
                }
                else
                {
                    // Circle
                    graphicsPath.AddEllipse(rect);
                }
            }
            catch
            {
                graphicsPath.AddEllipse(rect);
            }

            graphicsPath.CloseFigure();
        }
        public static void AddRoundedRectangle(this GraphicsPath path, RectangleF rect, int cornerRadius)
        {
            if (cornerRadius > 0)
            {
                path.StartFigure();
                path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
                path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
                path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
                path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);

                path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);

                path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
                path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
                path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
                path.CloseFigure();
            }
            else
            {
                path.AddRectangle(rect);
            }
        }
        public static void AddBottomRoundedRect(this CGContext c, RectangleF b, float r)
        {
            c.MoveTo (b.Left, b.Top + r);
            c.AddLineToPoint (b.Left, b.Bottom - r);

            c.AddArc (b.Left + r, b.Bottom - r, r, (float)(Math.PI), (float)(Math.PI / 2), true);

            c.AddLineToPoint (b.Right - r, b.Bottom);

            c.AddArc (b.Right - r, b.Bottom - r, r, (float)(-3 * Math.PI / 2), (float)(0), true);

            c.AddLineToPoint (b.Right, b.Top);

            c.AddLineToPoint (b.Left, b.Top);
        }
        public static void AddPathFigureSegment(
            this D2D.GeometrySink sink, Jupiter.Media.PathSegment segment)
        {
            var bezierSegment = segment as BezierSegment;

            if (bezierSegment != null)
            {
                sink.AddBezier(
                    new D2D.BezierSegment
                    {
                        Point1 = bezierSegment.Point1.ToSharpDX(),
                        Point2 = bezierSegment.Point2.ToSharpDX(),
                        Point3 = bezierSegment.Point3.ToSharpDX()
                    });
                return;
            }

            var lineSegment = segment as LineSegment;

            if (lineSegment != null)
            {
                sink.AddLine(
                    lineSegment.Point.ToSharpDX());
                return;
            }

            var polyBezierSegment = segment as PolyBezierSegment;

            if (polyBezierSegment != null)
            {
                var beziers = new D2D.BezierSegment[polyBezierSegment.Points.Count / 3];

                for (int i = 0; i < beziers.Length; i++)
                {
                    beziers[i].Point1 = polyBezierSegment.Points[i * 3].ToSharpDX();
                    beziers[i].Point2 = polyBezierSegment.Points[i * 3 + 1].ToSharpDX();
                    beziers[i].Point3 = polyBezierSegment.Points[i * 3 + 2].ToSharpDX();
                }

                sink.AddBeziers(beziers);
                return;
            }

            var polyLineSegment = segment as PolyLineSegment;

            if (polyLineSegment != null)
            {
                var lines = new SharpDX.DrawingPointF[polyLineSegment.Points.Count];

                for (int i = 0; i < lines.Length; i++)
                {
                    lines[i] = polyLineSegment.Points[i].ToSharpDX();
                }

                sink.AddLines(lines);
                return;
            }

            var quadraticBezierSegment = segment as QuadraticBezierSegment;

            if (quadraticBezierSegment != null)
            {
                sink.AddQuadraticBezier(
                    new D2D.QuadraticBezierSegment
                    {
                        Point1 = quadraticBezierSegment.Point1.ToSharpDX(),
                        Point2 = quadraticBezierSegment.Point2.ToSharpDX()
                    });
                return;
            }

            var polyQuadraticBezierSegment = segment as PolyQuadraticBezierSegment;

            if (polyQuadraticBezierSegment != null)
            {
                var quadraticBeziers = new D2D.QuadraticBezierSegment[polyBezierSegment.Points.Count / 2];

                for (int i = 0; i < quadraticBeziers.Length; i++)
                {
                    quadraticBeziers[i].Point1 = polyBezierSegment.Points[i * 2].ToSharpDX();
                    quadraticBeziers[i].Point2 = polyBezierSegment.Points[i * 2 + 1].ToSharpDX();
                }

                sink.AddQuadraticBeziers(quadraticBeziers);
                return;
            }

            var arcSegment = segment as ArcSegment;

            if (arcSegment != null)
            {
                sink.AddArc(
                    new D2D.ArcSegment
                    {
                        Point = arcSegment.Point.ToSharpDX(),
                        Size = arcSegment.Size.ToSharpDX(),
                        RotationAngle = (float)arcSegment.RotationAngle,
                        SweepDirection = arcSegment.SweepDirection.ToSharpDX(),
                        ArcSize = arcSegment.IsLargeArc ? D2D.ArcSize.Large : D2D.ArcSize.Small
                    });
                return;
            }
        }
        public static void AddEllipseGeometry(
            this D2D.GeometrySink sink, Jupiter.Media.EllipseGeometry ellipseGeometry)
        {
            // Start the ellipse at 9 o'clock.
            sink.BeginFigure(
                new DrawingPointF(
                    (float)(ellipseGeometry.Center.X - ellipseGeometry.RadiusX),
                    (float)(ellipseGeometry.Center.Y)),
                    D2D.FigureBegin.Filled);


            // Do almost full ellipse in one arc (there is .00001 pixel size missing)
            sink.AddArc(
                new D2D.ArcSegment
                {
                    Point = new DrawingPointF(
                        (float)(ellipseGeometry.Center.X - ellipseGeometry.RadiusX),
                        (float)(ellipseGeometry.Center.Y + 0.00001)),
                    Size = new DrawingSizeF(
                        (float)(ellipseGeometry.RadiusX * 2),
                        (float)(ellipseGeometry.RadiusY * 2)),
                    RotationAngle = 0,
                    SweepDirection = D2D.SweepDirection.Clockwise,
                    ArcSize = D2D.ArcSize.Large
                });

            // Close the ellipse
            sink.EndFigure(D2D.FigureEnd.Closed);
        }
 public static void AddCircleFigure(this CanvasPathBuilder builder, Vector2 center, float radius)
 {
     builder.BeginFigure(center + Vector2.UnitX * radius);
     builder.AddArc(center, radius, radius, 0, (float)Math.PI * 2);
     builder.EndFigure(CanvasFigureLoop.Closed);
 }
Esempio n. 11
0
		/// <summary>
		/// Adds an arc to the path at the specified <paramref name="location"/>
		/// </summary>
		/// <param name="path">Path to add the arc to</param>
		/// <param name="location">Location of the bounding rectangle of the arc</param>
		/// <param name="startAngle">Start angle in degrees</param>
		/// <param name="sweepAngle">Sweep angle (positive or negative) in degrees</param>
		public static void AddArc (this IGraphicsPath path, RectangleF location, float startAngle, float sweepAngle)
		{
			path.AddArc (location.X, location.Y, location.Width, location.Height, startAngle, sweepAngle);
		}
Esempio n. 12
0
        /// <summary>
        /// Adds the unclosed set of lines that are the bottom and right of the shape
        /// </summary>
        /// <param name="self"></param>
        /// <param name="bounds"></param>
        /// <param name="radius"></param>
        public static void AddRoundedRectangleBottomRight(this GraphicsPath self, Rectangle bounds, int radius)
        {
            if (radius * 2 > bounds.Width)
            {
                self.AddArc(bounds, 0F, 90F);
            }
            int w = radius * 2;
            Rectangle br = new Rectangle(bounds.Right - w, bounds.Bottom - w, w, w);
            self.AddLine(new Point(bounds.Right, bounds.Top + radius), new Point(bounds.Right, bounds.Bottom - radius));
            self.AddArc(br, 0, 90F);
            self.AddLine(new Point(bounds.Right - radius, bounds.Bottom), new Point(bounds.Left + radius, bounds.Bottom));

        }
Esempio n. 13
0
        public static void AddRoundedRect(this CGContext c, RectangleF b, float r)
        {
            var x = b.X;
            var y = b.Y;
            var w = b.Width;
            var h = b.Height;
            if (w < 0) {
                x += w;
                w = Math.Abs (w);
            }
            if (h < 0) {
                y += h;
                h = Math.Abs (h);
            }

            var ri = x + w;
            var bo = y + h;

            c.MoveTo (x, y + r);
            c.AddLineToPoint (x, bo - r);

            c.AddArc (x + r, bo - r, r, (float)(Math.PI), (float)(Math.PI / 2), true);

            c.AddLineToPoint (ri - r, bo);

            c.AddArc (ri - r, bo - r, r, (float)(-3 * Math.PI / 2), (float)(0), true);

            c.AddLineToPoint (ri, y + r);

            c.AddArc (ri - r, y + r, r, (float)(0), (float)(-Math.PI / 2), true);

            c.AddLineToPoint (x + r, y);

            c.AddArc (x + r, y + r, r, (float)(-Math.PI / 2), (float)(Math.PI), true);
        }