Diff with Non-Intersecting Polygons
tp opened this issue · comments
Not sure whether this is a bug or a lack in my understanding:
I got surprised that I get an null
result for the diff of non-intersecting polygons.
Is this expected and should my code first check whether the polygons are intersecting and only then try to calculate the diff?
Examples
const gh = require('greiner-hormann')
const pencilPolygon = [{"x":0.11612903225806452,"y":0.33455451259745367},{"x":0.11720430107526882,"y":0.33455451259745367},{"x":0.11720430107526882,"y":0.33064826259745367},{"x":0.11612903225806452,"y":0.33064826259745367}];
const eraserPolygon = [{"x":0.14516129032258066,"y":0.4109600939072074},{"x":0.14623655913978495,"y":0.4109600939072074},{"x":0.14623655913978495,"y":0.3718975939072074},{"x":0.14516129032258066,"y":0.3718975939072074}];
gh.diff(pencilPolygon, eraserPolygon); // -> null
gh.intersection(pencilPolygon, eraserPolygon); // -> null
I would assume either one or the other to return something. In this case diff returning the pencilPolygon
.
That's strange. True, diff should return first polygon in this case.
Interestingly "martinez" does:
const m = require('martinez-polygon-clipping');
const p2arr = (p) => ([p.x, p.y])
m.diff([ pencilPolygon.map(p2arr) ], [ eraserPolygon.map(p2arr) ]);
[ [ [ 0.11612903225806452, 0.33455451259745367 ],
[ 0.11720430107526882, 0.33455451259745367 ],
[ 0.11720430107526882, 0.33064826259745367 ],
[ 0.11612903225806452, 0.33064826259745367 ] ] ]
Maybe it would make sense to compare the two libs in test to find such discrepancies? They should always return the same results, right?
No, you already found it) Greiner-Hormann is a very primitive library with its own problems, so this just has to be fixed in the output here https://github.com/w8r/GreinerHormann/blob/master/src/polygon.js#L273