none
How to draw a circle with a line that will point to a value between 0 and 360 in C#? RRS feed

  • Question

  • I have a code to draw a circle. I want to draw a line at an angle based on the value I get. The value range will be between 0 and 360.

    Graphics g = this.pictureBox1.CreateGraphics();
                System.Drawing.RectangleF PictBoxRect = new RectangleF();
                PictBoxRect.Size = pictureBox1.Size;
                PictBoxRect.Height = pictureBox1.Height - 5;
                PictBoxRect.Width = pictureBox1.Width - 5;
                PictBoxRect.X = 0;
                PictBoxRect.Y = 0;
                Pen pen = new Pen(Color.Black);
                pen.Width = 10;
                g.DrawEllipse(Pens.Black, PictBoxRect);

    Thursday, February 13, 2020 7:48 PM

Answers

  • You need to do a bit of Analytical Geometry. Since the DrawLine method takes two points as parameters, you need to calculate the two points that determine the line segment.

    If you want the line to start at the center of the circle, you can calculate the center from the bounding rectangle of the circle:

    double x1 = PictBoxRect.Left + PictBoxRect.Width/2;
    double y1 = PictBoxRect.Top + PictBoxRect.Height/2;

    The second point will be on the circumference itself. Given the center of the circle, project the point on the X and Y axes by multiplying its length (the radius of the circle) by the sine and cosine of the desired angle:

    double radius = Math.Sqrt(PictBoxRect.Width*PictBoxRect.Width+PictBoxRect.Height*PictBoxRect.Height)/2;
    double x2 = x1+radius*Math.Sin(angle);
    double y2 = y1+radius*Math.Cos(angle);

    Then, draw the line using these points:

    g.DrawLine(Pens.Black, (float)x1, (float)y1, (float)x2, (float)y2);

    The angle is measured in radians starting from the horizontal. If you need a range between 0 and 360, I hope that you remember how to get radians from degrees (hint: divide by 2*Math.Pi). Also, if you want your angle measured from a different starting point, just add or subtract 90 degrees as needed.

    Thursday, February 13, 2020 9:41 PM
    Moderator

All replies

  • You need to do a bit of Analytical Geometry. Since the DrawLine method takes two points as parameters, you need to calculate the two points that determine the line segment.

    If you want the line to start at the center of the circle, you can calculate the center from the bounding rectangle of the circle:

    double x1 = PictBoxRect.Left + PictBoxRect.Width/2;
    double y1 = PictBoxRect.Top + PictBoxRect.Height/2;

    The second point will be on the circumference itself. Given the center of the circle, project the point on the X and Y axes by multiplying its length (the radius of the circle) by the sine and cosine of the desired angle:

    double radius = Math.Sqrt(PictBoxRect.Width*PictBoxRect.Width+PictBoxRect.Height*PictBoxRect.Height)/2;
    double x2 = x1+radius*Math.Sin(angle);
    double y2 = y1+radius*Math.Cos(angle);

    Then, draw the line using these points:

    g.DrawLine(Pens.Black, (float)x1, (float)y1, (float)x2, (float)y2);

    The angle is measured in radians starting from the horizontal. If you need a range between 0 and 360, I hope that you remember how to get radians from degrees (hint: divide by 2*Math.Pi). Also, if you want your angle measured from a different starting point, just add or subtract 90 degrees as needed.

    Thursday, February 13, 2020 9:41 PM
    Moderator
  • Thank you Alberto,it worked.
    Thursday, February 13, 2020 10:20 PM