Search code examples
c#filterdxf

Filtering Arc values in c#


I'm having trouble filtering an Arc value.

When I draw a bitmap (dxf file into bmp) there is always extra arcs with the same values, no matter the file im working with. The extra arc

Library : netDxf

 else if (entity is Arc arc)
 {
     
     double sweepAngle = arc.EndAngle - arc.StartAngle;
     arc.EndAngle = sweepAngle;
                 
     
     entityInfo = $"Processing Arc: Start Angle=({arc.StartAngle}), End Angle=({arc.EndAngle}), Radius={arc.Radius}, Sweep Angle =({sweepAngle})";

     graphics.DrawArc(Pens.Black, (float)(arc.Center.X - arc.Radius), (float)(arc.Center.Y - arc.Radius),

     (float)(2 * arc.Radius), (float)(2 * arc.Radius), (float)arc.StartAngle, (float)arc.EndAngle); 
 
                         
 }

I'll remove the entityInfo afterwards, I used it just to get the arc.StartAngle value of the extra arcs.

I already tried this if (arc.StartAngle == 270.565033310363) return ;, expecting it to enter the if but it didn't.


Solution

  • Floating-point numbers and equality don't fit well, at least not in that way you expect it to. The point here is, that not every decimal number (e.g. 270.565033310363) can accurately be represented as floating-point number. You will allmost allways get an approximation for that number, e.g 270.565033310364.

    So in order to compare floating-point-numbers (e.g. numbers with type double), you need to handle a very small range within that you consider two numbers to be equal, often called epsilon.

    So you may use this instead:

    var epsilon = 0.000001;
    if (Math.Abs(arc.StartAngle - 270.565033310363) < epsilon)
        return;