AngusJohnson / Clipper2

Polygon Clipping and Offsetting - C++, C# and Delphi
Boost Software License 1.0
1.34k stars 248 forks source link

Regression when removing type casting in IsCollinear() #831

Closed here-abarany closed 2 months ago

here-abarany commented 2 months ago

When updating to the latest revision of Clipper2, we encountered a regression due to #825 for removing the type casting in IsCollinear().

In this test case, we have four points that cross over in an hourglass shape. We call Execute(Union, EvenOdd) to "repair" this invalid polygon into a pair of triangles that touch at a point.

These values, which are intended for single-precision floats, work as intended:

Input (with AddSubject()):

-536870912, -536870912
536870912, -536870912
-536870912, 536870912
536870912, 536870912

Output (2 paths):

0, 0
-536870912, 536870912
536870912, 536870912

536870912, -536870912
-536870912, -536870912
0, 0

However, these values, which are intended for doubles, break with #825:

-1125899906842624, -1125899906842624
1125899906842624, -1125899906842624
-1125899906842624, 1125899906842624
1125899906842624, 1125899906842624

With the latest revision, the call to Execute() gives an empty result rather than two paths similar to the smaller integer values.

AngusJohnson commented 2 months ago

Aaron, thank you for your very clear and concise bug report. I'll check it out and hopefully have a fix soon (<24hrs).

here-abarany commented 2 months ago

Thanks. I should also clarify that I am using the C++ library.

reunanen commented 2 months ago

Can reproduce. Yeah, with these inputs (which are exactly ±0x4000000000000) and using integer math, a*b wraps to 0, as does c*d. So perhaps an error in this direction is more critical than an error in the other direction after all (see the comment here).

AngusJohnson commented 2 months ago

This has been fixed in the latest repository revision.