C# Drawing Arc with 3 Points

后端 未结 2 1587
心在旅途
心在旅途 2020-12-16 18:52

I need to draw an arc using GraphicsPath and having initial, median and final points. The arc has to pass on them.

I tried .DrawCurve and .DrawBezier but the result

相关标签:
2条回答
  • 2020-12-16 19:28

    Have you tried the DrawArc method and seeing if u can manipulate your 3 points somehow?

    maybe

    Pen blackPen= new Pen(Color.Black, 3);
    // Create rectangle to bound ellipse.
    Rectangle rect = new Rectangle(initial x, initial y, final x, median y);
    // Create start and sweep angles on ellipse.
    float startAngle =  0F;
    float sweepAngle = 270.0F;
    // Draw arc to screen.
    e.Graphics.DrawArc(blackPen, rect, startAngle, sweepAngle);
    

    http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawarc%28VS.71%29.aspx

    0 讨论(0)
  • 2020-12-16 19:30

    I would use DrawArc() as suggested by ANC_Michael. To find an arc that passes through 3 points you want to calculate the circumcircle of the triangle formed by the points.

    Once you have the circumcircle calculate a bounding box for the circle to use with DrawArc using the min/max dimensions (center +/- radius). Now calculate your start and stop angles by translating the points so that the circumcircle is centered on the origin (translate by -circumcenter) and take the dot-product of the normalized start and end vectors with the X-axis:

    double startAngle = Math.Acos(VectorToLeftPoint.Dot(XAxis));
    double stopAngle = Math.Acos(VectorToRightPoint.Dot(XAxis));
    

    Note that DrawArc expects angles clockwise from the X-axis so you should add Math.PI if the calculated vector is above the x-axis. That should be enough information to call DrawArc().

    Edit: This method will find a circular arc and not necessarily the 'best fit' arc depending on your expected endpoint behavior.

    0 讨论(0)
提交回复
热议问题