alexbol99 / flatten-js

Javascript library for 2d geometry
MIT License
533 stars 55 forks source link

Floating point issue #124

Open nine-fox opened 1 year ago

nine-fox commented 1 year ago

Hi Alex,

This is a really great project! As far as I can see, the implementation is cool and clear, and I am willing to use it in CAD projects.

However, I am struggling with the following issues, would you like to take a look? thanks,

1) usage of Flatten.Utils.EQ When I dig into the polygon intersect code, I found EQ(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY) return false, which is confusing, because in Math, these 2 things are equal.

The implementation in EQ explains, because POSITIVE_INFINITY - POSITIVE_INFINITY will be NaN and return false.

To make better understanding, should EQ return true in this case?

2) floating point issue Flatten.js is using float for each storage, like Point.x, Circle.r etc. It can be enhanced to use more accurate numbers in some CAD system. For example, to use decimal.js to accept more large numbers. I know the cost would be high, however, would it be better to provide big decimal option to user?

Thanks, nine-fox

mahaidong commented 3 weeks ago

https://github.com/alexbol99/flatten-js/blob/master/src/classes/box.js

contains(shape) {
        if (shape instanceof Flatten.Point) {
            return (shape.x >= this.xmin) && (shape.x <= this.xmax) && (shape.y >= this.ymin) && (shape.y <= this.ymax);
        }

I guess it should change to !Flatten.Utils.LT(shape.x, this.xmin) && ... , it will be more correct for floating point