Arc segment between two points and a radius

I am trying to draw an arc segment using WPF, but somehow I cannot figure out how to do this using ArcSegment-Element.

I have two arc points (P1 and P2), as well as the center of the circle and the radius.

enter image description here

+3
source share
2 answers

Create a PathFigure with P1 as StartPointwell as an ArcSegment with P2 as Pointwell as a quadratic Sizethat contains a radius.

Example: P1 = (150 100), P2 = (50.50), Radius = 100, i.e. Size = (100 100):

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

or shorter:

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

I know this is a little old, but here comes the version of the code for the center and two angles - it can easily adapt to the start and end points:

Structure:

  • " " 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);
    }
    
+1

All Articles