【问题标题】:Arc Segment between two points and a radius两点和半径之间的弧段
【发布时间】:2017-02-15 15:00:15
【问题描述】:

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

我有两个给定的圆弧点(P1 和 P2),我还有圆心和半径。

【问题讨论】:

    标签: c# wpf geometry


    【解决方案1】:

    创建一个 P1 为 StartPoint 的 PathFigure 和一个 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"/>
    

    【讨论】:

      【解决方案2】:

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

      大纲:

      • 创建一条路径并将“左上角”设置为 0,0(如果需要,您仍然可以将中心设为负值 - 这仅用于路径参考)
      • 设置样板 PathGeo 和 PathFigure(这些是 Word 等中多段路径的构建块)
      • 进行角度检查
      • 如果 > 180 度(pi 弧度),则称其为“大角度”
      • 找到起点和终点并设置它们
      • 根据数学,它是顺时针旋转(记住正 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);
        }
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-29
        • 1970-01-01
        相关资源
        最近更新 更多