Timmoth
Timmoth

Reputation: 510

How can I draw a segment of a semi circle with a consistant radius using an ArcSegment

I am trying to write my own pie chart control using WPF. I have created a function that returns a Path object representing a segment of the pie chart. When I use this to generate a graph where the largest slice is less then or equal to 50% of the graph it renders fine and looks like so: Working Chart

But when one of the arcsegments has an angle of more then PI radians the radius is not equal all the way round and it looks like this:

Broken Chart

Segment over PI radians

Here is the code that I have written to draw each segment of the chart:

        public Path CreateSlice(Point centerPoint, double radius, ref Point initialPoint, ref double totalAngle, double sliceAngle, Color sliceColor)
        {
            //A set of connected simple graphics objects that makes up the Slice graphic
            Path slicePath = new Path();
            slicePath.Fill = new SolidColorBrush(sliceColor);
            slicePath.Stroke = new SolidColorBrush(Colors.White);
            slicePath.StrokeThickness = 2;

            PathGeometry pathGeometry = new PathGeometry();
            PathFigureCollection pathFigureCollection = new PathFigureCollection();
            PathSegmentCollection sliceSegmentCollection = new PathSegmentCollection();

            //The path figure describes the shape in the slicePath object using geometry
            PathFigure pathFigure = new PathFigure();
            pathFigure.StartPoint = centerPoint;//Set the start point to the center of the pie chart

            //Representing the first line of the slice from the centerpoint to the initial point 
            //I.E one length of the slice
            LineSegment firstLineSegment = new LineSegment();
            firstLineSegment.Point = initialPoint;
            sliceSegmentCollection.Add(firstLineSegment);

            //Calculate the next point along the circle that the slice should arc too.
            //This point is calculated using the total angle used of the pie chart including this slice
            totalAngle += sliceAngle;
            //Calculate the X and Y coordinates of the next point
            double x = centerPoint.X + radius * Math.Cos(totalAngle);
            double y = centerPoint.Y + radius * Math.Sin(totalAngle);
            initialPoint = new Point(x, y);

            //Represents the arc segment of the slice
            ArcSegment sliceArcSegment = new ArcSegment();
            sliceArcSegment.Point = initialPoint;
            sliceArcSegment.SweepDirection = SweepDirection.Clockwise;
            sliceArcSegment.Size = new Size(radius, radius);
            sliceSegmentCollection.Add(sliceArcSegment);

            //Representing the second line of the slice from the second point back to the center
            LineSegment secondLineSegment = new LineSegment();
            secondLineSegment.Point = centerPoint;
            sliceSegmentCollection.Add(secondLineSegment);

            pathFigure.Segments = sliceSegmentCollection;
            pathFigureCollection.Add(pathFigure);
            pathGeometry.Figures = pathFigureCollection;
            slicePath.Data = pathGeometry;
            return slicePath;
        }

Could anyone help me figure out how to draw a arcsegment with a uniform radius all the way round please.

Upvotes: 2

Views: 1858

Answers (1)

John Garrard
John Garrard

Reputation: 167

The IsLargeArc property should be set in these cases to tell the arc to be greater than 180 degrees. Looking at your figure it is capping the degrees at 180 and shifting the expected center point of the Arc.

Upvotes: 1

Related Questions