Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

C# Why DrawArc/DrawPie angle point differs from actual Arc point for the same angle?

$
0
0

When I draw an Ellipse or an Arc using C# DrawArc/DrawEllipse or DrawPie GDI functions, I believed that it draws for the exact angle what I have given. But, when I did a test on it by  writing a small program, I found 225 degrees sweep angle in DrawArc is actually not 225 degrees in view. My test rogram draws a line from 0 degree to 360 degree every second (like a clock seconds hand) and in parallel Drawing arc using DrawArc function for the same angle. 

Below function is used to get point on angle given start/end point and angle. Can somebody please explain why is this difference? I tried to find the end point of the drawn arc by DrawArc(). I can achieve it in different way. But, I dont understand why the DrawArc function is working this way?

public static Point PointOnEllipseFromAngle(Point center, int radiusX, int radiusY, int angle)
{
        double x = center.X + radiusX * Math.Cos(angle * (Math.PI / 180.0));
        double y = center.Y + radiusY * Math.Sin(angle * (Math.PI / 180.0));
        return new Point((int)x, (int)y);
}

Form Paint function goes like this

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Rectangle rect = Bounds;
            rect.Inflate(-50, -50);
            // Mid point
            Point mid = new Point(rect.Left+(rect.Width / 2), rect.Top+(rect.Height / 2));

            // Arc point for the given angle (angle is incremented in timer every second)
            Point p1 = PointOnEllipseFromAngle(new Point(rect.Left+(rect.Width / 2), rect.Top+(rect.Height / 2)), rect.Width / 2, rect.Height / 2, angle); 

            // Line between mid and arc point
            e.Graphics.DrawLine(new Pen(Color.Blue, 2), mid, p1);
            e.Graphics.DrawString(angle.ToString(), new Font("Arial", 18), new SolidBrush(Color.Red), p1);
            e.Graphics.FillEllipse(new SolidBrush(Color.Red), new Rectangle(p1.X - 5, p1.Y - 5, 10, 10)); // red circle at edge of the line

            // DrawArc for the same angle
            e.Graphics.DrawArc(new Pen(Color.Green,2), rect, 0, angle);

            // Just Drawing axis lines (horizontal, vertical, diagonal)
            e.Graphics.DrawLine(new Pen(Color.Black, 2), mid, new Point(rect.Left+rect.Width,rect.Top+(rect.Height/2)));
            e.Graphics.DrawLine(new Pen(Color.Black, 2), mid, new Point(rect.Left, rect.Top + (rect.Height / 2)));
            e.Graphics.DrawLine(new Pen(Color.Black, 2), mid, new Point(rect.Left + (rect.Width/2), rect.Top + rect.Height));
            e.Graphics.DrawLine(new Pen(Color.Black, 2), mid, new Point(rect.Left + (rect.Width / 2), rect.Top));
            e.Graphics.DrawLine(new Pen(Color.Black, 2), rect.Left,rect.Top,rect.Right,rect.Bottom);
            e.Graphics.DrawLine(new Pen(Color.Black, 2), rect.Left, rect.Bottom, rect.Right, rect.Top);
        }


Thanks,

Naren N


Viewing all articles
Browse latest Browse all 12583

Trending Articles