How does GraphicsPath.AddArc use startAngle and sweepAngle parameters?

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

alt text

+4
source share
2 answers

GraphicsPath.AddArc does exactly what you ask for - it is the arc to the line projected from the center of the ellipse, at a tone angle of 135 degrees clockwise from the x axis.

Unfortunately, this does not help when you use the angle as a direct part of the slice of the pie chart you want to draw. To find out the angle B, you need to use with AddArc, given the angle A that works on the circle, in radians, use:

 B = Math.Atan2(sin(A) * height / width, cos(A)) 

Where width and height is an ellipse.

In your sample code, try adding the following at the end of Form1_Paint:

 ellipticalPath = new System.Drawing.Drawing2D.GraphicsPath(); ellipticalPath.AddArc( ellipseBounds, 0.0F, (float) (180.0 / Math.PI * Math.Atan2( Math.Sin(135.0 * Math.PI / 180.0) * ellipseBounds.Height / ellipseBounds.Width, Math.Cos(135.0 * Math.PI / 180.0)))); e.Graphics.DrawPath(Pens.Black, ellipticalPath); 

The result should look like this: alt text http://img216.imageshack.us/img216/1905/arcs.png

+3
source

enter image description here I was confused about how GraphicsPath.AddArc worked, and I could not find decent diagrams, so I drew them. Just in case, if someone else is suffering! http://imgur.com/lNBewKZ

+2
source

All Articles