3

I am having some trouble trying to implement drawing an arc. Basically it's supposed to be drawn exactly like how you can draw a 3 point arc in AutoCAD. I can get the arc to draw with start and end angles from from -180 degrees to 360, but if I try to go more than -180, the arc flips around to +180 degrees. My problem is that I need the arc to continue when the angle passes -180. I have everything right in the code (I think) except for calculating the start and end angles. Thanks in advance for any help. Here is my code:

private void DrawArc()
{
    //fyi linept is of type List<Point3D>();

    Point3D currentPoint = (Point3D)GetPoints(e);
    double rad;
    Point3D center = GetCenterOfCircle(linept.ElementAt(0), linept.ElementAt(1), currentPoint, out rad);
    PieSliceVisual3D circle = new PieSliceVisual3D();
    circle.Center = center;
    circle.OuterRadius = rad;
    circle.InnerRadius = circle.OuterRadius + 3;
    circle.StartAngle = (Math.Atan2(linept.ElementAt(0).Y - center.Y, linept.ElementAt(0).X - center.X) * 180 / Math.PI);
    circle.EndAngle = (Math.Atan2(currentPoint.Y - center.Y, currentPoint.X - center.X) * 180 / Math.PI);

    //I've also tried these next 4 lines to no avail
    double startAngle = (Math.Atan2(linept.ElementAt(0).Y - center.Y, linept.ElementAt(0).X - center.X) * 180 / Math.PI);
    circle.StartAngle = (startAngle > 0.0 ? startAngle : (360.0 + startAngle));
    double endAngle = (Math.Atan2(currentPoint.Y - center.Y, currentPoint.X - center.X) * 180 / Math.PI);
    circle.EndAngle = (endAngle > 0.0 ? endAngle : (360.0 + endAngle));
}
Just Ask
  • 329
  • 2
  • 6
  • 22
  • How are you invoking `Graphics.DrawArc`? It accepts `startAngle` and `sweepAngle` (instead of `endAngle`) so can you show you perform the calculation? – Zong Jul 18 '12 at 23:37
  • What happens when your end point has a lower angle than your start point? Long ago when I implemented a function of this nature I found the best answer was to check for this and add 360 degrees if need be--the start was constrained to 0..359, the end 0..719, thus ensuring all arcs were drawn counterclockwise--a limit of the graphics library I was using. Could you be facing the same issue? – Loren Pechtel Jul 18 '12 at 23:45
  • 1
    I'm not using Graphics.DrawArc. I should've mentioned I'm using the Helix 3D Toolkit and the PieSliceVisual3D to draw my arcs. Sorry about the confusion. And the calculation to get my start and end angles are shown (the two ways that I've tried). – Just Ask Jul 18 '12 at 23:48
  • Loren, I tried that and it works fine when I want to draw an arc from 0-360 but if I want to bend the arc the other way it won't allow it to happen. – Just Ask Jul 18 '12 at 23:56
  • @JustAsk: What do you mean, bend the arc the other way? Start Angle > End Angle or do you mean one that wraps past the 0 point? It certainly sounds to me like you're hitting the same thing I did. – Loren Pechtel Jul 19 '12 at 03:17
  • It's so hard to explain in text. I want to have my arcs be drawn exactly like AutoCAD where you specify the start and middle points of the arc and then the arc bends to wherever the mouse is positioned. I can draw arcs fine if I make a horizontal line. But when my first two points are vertical or diagonal, the arc flips around in certain places. – Just Ask Jul 19 '12 at 16:28
  • So in your code you had this? If so, this is what I tried and it worked for only one case. if(endAngle < startAngle) endAngle += 360; – Just Ask Jul 19 '12 at 17:39
  • I Just happend to pass this topic. I think the problem is in the Math.Atan. This always returns within -PI or Pi (which is 180 degrees) so you need to check in qhich quadrant you are drawing. https://msdn.microsoft.com/en-us/library/system.math.atan2(v=vs.110).aspx – Alain Sep 07 '16 at 15:34

0 Answers0