两个点和半径之间的弧段

时间:2014-02-22 17:30:39

标签: c# wpf geometry

我正在尝试使用WPF绘制弧段,但我无法弄清楚如何使用ArcSegment-Element执行此操作。

我有两个弧点(P1和P2),我也有圆心和半径。

enter image description here

2 个答案:

答案 0 :(得分:5)

创建一个路径图,其中P1为StartPoint,P2为Point的ArcSegment和包含半径的二次Size

示例:P1 =(150,100),P2 =(50,50),半径= 100,即尺寸=(100,100):

<Path Stroke="Black">
    <Path.Data>
        <PathGeometry>
            <PathFigure StartPoint="150,100">
                <ArcSegment Size="100,100" Point="50,50"/>
            </PathFigure>
        </PathGeometry>
    </Path.Data>
</Path>

或更短:

<Path Stroke="Black" Data="M150,100 A100,100 0 0 0 50,50"/>

答案 1 :(得分:4)

我知道这有点旧,但这里有一个代码版本的中心和两个角度 - 可以很容易地适应起点和终点:

概要

  • 制作路径并设置&#34;左上角&#34;到0,0(如果你愿意,你仍然可以将中心设为负数 - 这仅供路径参考)
  • 设置锅炉板PathGeo和PathFigure(这些是Word等多段路径的构建块。)
  • 进行角度检查
  • 如果是> 180度(pi弧度),称之为&#34;大角度&#34;
  • 查找开始和结束点并设置它们
  • 根据数学计算,它是顺时针旋转(记住正Y是正数,正X是正确的)
  • 设置为画布

    public void DrawArc(ref Path arc_path, Vector center, double radius, double start_angle, double end_angle, Canvas canvas)
    {
        arc_path = new Path();
        arc_path.Stroke = Brushes.Black;
        arc_path.StrokeThickness = 2;
        Canvas.SetLeft(arc_path, 0);
        Canvas.SetTop(arc_path, 0);
    
        start_angle = ((start_angle % (Math.PI * 2)) + Math.PI * 2) % (Math.PI * 2);
        end_angle = ((end_angle % (Math.PI * 2)) + Math.PI * 2) % (Math.PI * 2);
        if(end_angle < start_angle){
            double temp_angle = end_angle;
            end_angle = start_angle;
            start_angle = temp_angle;
        }
        double angle_diff = end_angle - start_angle;
        PathGeometry pathGeometry = new PathGeometry();
        PathFigure pathFigure = new PathFigure();
        ArcSegment arcSegment = new ArcSegment();
        arcSegment.IsLargeArc = angle_diff >= Math.PI;
        //Set start of arc
        pathFigure.StartPoint = new Point(center.X + radius * Math.Cos(start_angle), center.Y + radius * Math.Sin(start_angle));
        //set end point of arc.
        arcSegment.Point = new Point(center.X + radius * Math.Cos(end_angle), center.Y + radius * Math.Sin(end_angle));
        arcSegment.Size = new Size(radius, radius);
        arcSegment.SweepDirection = SweepDirection.Clockwise;
    
        pathFigure.Segments.Add(arcSegment);
        pathGeometry.Figures.Add(pathFigure);
        arc_path.Data = pathGeometry;
        canvas.Children.Add(arc_path);
    }