C # Draw Circle with GraphicsPath, part of which is cut out

I am trying to do something like the following figures with three parameters

  • radius
  • Centre
  • cutOutLen

the cut out part is the bottom of the circle.

enter image description here

I realized 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 do this?

By the way, what is the name of this form? I could not look for previous questions or something due to lack of terminology.

Thank.

+4
source share
3 answers

Here you go, put this in some kind of drawing 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 am not sure about the term cut circle; this is actually Thales Cirlce.

+4
source

I think you could try using AddArc and then CloseFigure

+3

, :

    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);
            }
        }
    }

:

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

:

enter image description here

0

All Articles