如何使用ArcSegment绘制具有一致半径的半圆的一段

时间:2016-02-23 15:52:16

标签: c# wpf path geometry

我正在尝试使用WPF编写自己的饼图控件。我创建了一个函数,它返回一个Path对象,表示饼图的一部分。当我使用它来生成一个图形,其中最大的切片小于或等于图形的50%时,它渲染得很好,看起来像这样: Working Chart

但是当其中一个弧段的角度大于PI弧度时,半径不会一直相等,看起来像这样:

Broken Chart

Segment over PI radians

以下是我为绘制图表的每个部分而编写的代码:

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

任何人都可以帮我弄清楚如何绘制一个半径均匀的弧段。

1 个答案:

答案 0 :(得分:1)

在这些情况下应设置IsLargeArc属性以告知弧大于180度。看看你的数字,它将度数限制在180并改变弧的预期中心点。