5

I'm trying draw something like following shapes with 3 parameters

  • radius
  • center
  • cutOutLen

the cut out part is bottom of the circle.

enter image description here

I figured out that I can use

var path = new GraphicsPath();
path.AddEllipse(new RectangleF(center.X - radius, center.Y - radius, radius*2, radius*2))
// ....
g.DrawPath(path);

but, how can I draw such thing?

BTW, What is the name of that shape? I could't search previous questions or something due to lack of terminology.

Thanks.

Joonhwan
  • 495
  • 5
  • 11

3 Answers3

6

Here you go, place this in some paint event:

// set up your values
float radius = 50;
PointF center = new Point( 60,60);
float cutOutLen = 20;

RectangleF circleRect = 
           new RectangleF(center.X - radius, center.Y - radius, radius * 2, radius * 2);

// the angle
float alpha = (float) (Math.Asin(1f * (radius - cutOutLen) / radius) / Math.PI * 180);

var path = new GraphicsPath();
path.AddArc(circleRect, 180 - alpha, 180 + 2 * alpha);
path.CloseFigure();

e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.FillPath(Brushes.Yellow, path);
e.Graphics.DrawPath(Pens.Red, path);

path.Dispose();

Here is the result:

cut circle.

I'm not sure about the term of a cut circle; in effect it is a Thales Cirlce.

TaW
  • 53,122
  • 8
  • 69
  • 111
4

I think you could try to use AddArc and then CloseFigure

Denis Palnitsky
  • 18,267
  • 14
  • 46
  • 55
  • Thanks. One more thing. Can I can get the position that both arc and line intersects? This information is useful for me here. – Joonhwan Jul 18 '14 at 09:00
  • After trying to implement, I realized that i need sin/cos math. Is there another way to do this? for example, subtracting rect from circle or something. – Joonhwan Jul 18 '14 at 09:11
  • You may try Region.Xor http://msdn.microsoft.com/en-us/library/awbdfdhf(v=vs.110).aspx Sorry, I can't post/check any code now. – Denis Palnitsky Jul 18 '14 at 09:29
  • I think it is Region.Exclude(rectangle); But that will only help with the filling.. – TaW Jul 18 '14 at 10:18
  • This works better than the copy-paste answer. – Galacticai May 13 '21 at 13:29
0

Here is my implementation of a method that draws the desired figure:

    void DrawCutCircle(Graphics g, Point centerPos, int radius, int cutOutLen)
    {
        RectangleF rectangle = new RectangleF(
                        centerPos.X - radius,
                        centerPos.Y - radius,
                        radius * 2,
                        radius * 2);

        // calculate the start angle
        float startAngle = (float)(Math.Asin(
            1f * (radius - cutOutLen) / radius) / Math.PI * 180);

        using (GraphicsPath path = new GraphicsPath())
        {
            path.AddArc(rectangle, 180 - startAngle, 180 + 2 * startAngle);
            path.CloseFigure();

            g.FillPath(Brushes.Yellow, path);
            using (Pen p = new Pen(Brushes.Yellow))
            {
                g.DrawPath(new Pen(Brushes.Blue, 3), path);
            }
        }
    }

You can use it in the following way in your control:

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        DrawCutCircle(e.Graphics, new Point(210, 210), 200, 80);
    }

This is as it looks like:

enter image description here

Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219