alexbol99 / flatten-js

Javascript library for 2d geometry
MIT License
546 stars 56 forks source link

boolean operations on polygons with collinear edges raise exception #115

Closed lambertwx closed 2 years ago

lambertwx commented 2 years ago

I'm excited to use this library, but my application involves boolean operations on polygons A and B that often have an edge from B lying exactly on an edge from A. E.g., image

I forked your Boolean Operations notebook in Observable to https://observablehq.com/d/583407ca72e68684 so I could try this out, and quickly found that in such situations all of flatten-js's boolean polygon operations raise an exception

main.umd.js:189 Uncaught TypeError: Cannot read properties of null (reading 'length')
    at addToIntPoints (main.umd.js:189:28)
    at getIntersections (main.umd.js:169:21)
    at clip (main.umd.js:84:29)
    at booleanOpBinary (main.umd.js:75:16)
    at subtract (main.umd.js:48:24)
    at apply (observablehq-133:25:17)
    at HTMLButtonElement.form.querySelector.onclick (observablehq-44:38:9)

Is this a known limitation of your implementation of Atherton Weiler polygon clipping? Or are these routines intended to handle collinear edges and this is just a bug?

alexbol99 commented 2 years ago

Hello, @lambertwx

The notebook your forked use deprecated package @flatten-js/boolean-op Now Boolean operations are part of the @flatten-js/core package under Flatten.BooleanOperations namespace I updated your notebook, now it works as expected: https://observablehq.com/d/bd5f6d4c6bb942af

Best, Alex

lambertwx commented 2 years ago

Thank you for your rapid response and easy solution!