title: Xamarin iOS CGPath draw polygon with curved/rounded corners
body: <p>I'm trying to create a class that draws a polygon in a custom <code>UIView</code> subclass. I have it working, but now I want to smooth the corners by rounding them off and I'm not sure how to do that. Here is what I have so far:</p>
  
  <pre><code>public class MyView : UIView
  {
      private List&lt;CGPoint&gt; points;
  
      public MyView(CGRect frame, List&lt;CGPoint&gt; points) : base (frame)
      {
          this.points = points;
      }
  
      public override Draw (CGRect rect)
      {
          CGContext context = UIGraphics.GetCurrentContext();
  
          context.SetLineWidth(2);
          UIColor.Black.SetStroke();
          UIColor.Green.SetFill();
  
          CGPath path = new CGPath();
          path.AddLines(points.ToArray());
          path.CloseSubpath();
  
          context.AddPath(path);
          context.Clip();
  
          using (CGColorSpace rgb = CGColorSpace.CreateDeviceRGB())
          {
              CGGradient gradient = new CGGradient (rgb, new CGColor[]
              {
                  new CGColor(0, 1, 0),
                  new CGColor(0, 0.5f, 0),
                  new CGColor(0, 1, 0),
                  new CGColor(0, 0.5f, 0)
              });
  
              context.DrawLinearGradient(gradient,
                  new CGPoint (path.BoundingBox.Left, path.BoundingBox.Top), 
                  new CGPoint (path.BoundingBox.Right, path.BoundingBox.Bottom), 
                  CGGradientDrawingOptions.DrawsBeforeStartLocation);
          }
      }
  }
  </code></pre>
  
  <p>With the points I'm throwing in I get this: </p>
  
  <p><a href="https://i.stack.imgur.com/uwsjo.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uwsjo.jpg" alt="enter image description here"></a></p>
  
  <p>I've tried using <code>path.AddCurveToPoint</code> and <code>path.AddArc</code> but I can't seem to get them to work the way I want. Any help would be appreciated.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I played around with <code>path.AddCurveToPoint</code> and now it looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/ioIBz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ioIBz.jpg" alt="enter image description here"></a></p>
  
  <p>It starts off correctly, but then just completely goes nuts. Not sure what I'm doing wrong here. Here is my updated <code>Draw</code> override:</p>
  
  <pre><code>public override Draw (CGRect rect)
  {
      CGContext context = UIGraphics.GetCurrentContext();
  
      context.SetLineWidth(2);
      UIColor.Black.SetStroke();
      UIColor.Green.SetFill();
  
      CGPath path = new CGPath();
      path.AddLines(points.ToArray());
      path.CloseSubpath();
      // updated section
      int count = points.Count;
  
      for (int x = 1; x &lt; count; x++)
      {
          var p = points[x];
          if (x != 0)
          {
              var prev = points[x - 1];
              if (prev.Y != p.Y)
              {
                  if (prev.Y &gt; p.Y)
                  {
                      path.AddCurveToPoint(prev, new CGPoint(p.X - prev.X, p.Y), p);
                  }
                  else
                  {
                      //???
                  }
              }
          }
      }
      // end updated section
      context.AddPath(path);
      context.Clip();
  
      using (CGColorSpace rgb = CGColorSpace.CreateDeviceRGB())
      {
          CGGradient gradient = new CGGradient (rgb, new CGColor[]
          {
              new CGColor(0, 1, 0),
              new CGColor(0, 0.5f, 0),
              new CGColor(0, 1, 0),
              new CGColor(0, 0.5f, 0)
          });
  
          context.DrawLinearGradient(gradient,
              new CGPoint (path.BoundingBox.Left, path.BoundingBox.Top), 
              new CGPoint (path.BoundingBox.Right, path.BoundingBox.Bottom), 
              CGGradientDrawingOptions.DrawsBeforeStartLocation);
      }
  }
  </code></pre>