I am trying to use System.Drawing.Drawing2D.GraphicsPath.AddArc to draw an ellipse arc starting at 0 degrees and sweeping up to 135 degrees.
The problem I am facing is that for an ellipse, the drawn arc does not match what I expect.
For example, the following code generates the image below. Green circles are where I expect the endpoints of the arc to use the formula for the point along the ellipse. My formula works for circles, but not for ellipses.
Does this have anything to do with polar or cartesian coordinates?
private PointF GetPointOnEllipse(RectangleF bounds, float angleInDegrees) { float a = bounds.Width / 2.0F; float b = bounds.Height / 2.0F; float angleInRadians = (float)(Math.PI * angleInDegrees / 180.0F); float x = (float)(( bounds.X + a ) + a * Math.Cos(angleInRadians)); float y = (float)(( bounds.Y + b ) + b * Math.Sin(angleInRadians)); return new PointF(x, y); } private void Form1_Paint(object sender, PaintEventArgs e) { Rectangle circleBounds = new Rectangle(250, 100, 500, 500); e.Graphics.DrawRectangle(Pens.Red, circleBounds); System.Drawing.Drawing2D.GraphicsPath circularPath = new System.Drawing.Drawing2D.GraphicsPath(); circularPath.AddArc(circleBounds, 0.0F, 135.0F); e.Graphics.DrawPath(Pens.Red, circularPath); PointF circlePoint = GetPointOnEllipse(circleBounds, 135.0F); e.Graphics.DrawEllipse(Pens.Green, new RectangleF(circlePoint.X - 5, circlePoint.Y - 5, 10, 10)); Rectangle ellipseBounds = new Rectangle(50, 100, 900, 500); e.Graphics.DrawRectangle(Pens.Blue, ellipseBounds); System.Drawing.Drawing2D.GraphicsPath ellipticalPath = new System.Drawing.Drawing2D.GraphicsPath(); ellipticalPath.AddArc(ellipseBounds, 0.0F, 135.0F); e.Graphics.DrawPath(Pens.Blue, ellipticalPath); PointF ellipsePoint = GetPointOnEllipse(ellipseBounds, 135.0F); e.Graphics.DrawEllipse(Pens.Green, new RectangleF(ellipsePoint.X - 5, ellipsePoint.Y - 5, 10, 10)); }
