Infinite loop on some inputs
prendradjaja opened this issue · comments
Hello! Thanks for this library. I was wondering if you'd be able to help me figure out why some inputs result in an infinite loop, e.g. here are two examples:
Example 1
const b = [{"x":1,"y":0},{"x":1.0000000000000002,"y":1},{"x":0.29289321881345287,"y":1.7071067811865477},{"x":0.2928932188134526,"y":0.7071067811865477}];
const c = [{"x":0.29289321881345254,"y":0.7071067811865476},{"x":1.0000000000000002,"y":1.414213562373095},{"x":2.220446049250313e-16,"y":1.4142135623730951},{"x":-0.7071067811865476,"y":0.7071067811865479}];
intersect(b, c); // infinite loop
In case it's relevant: I've found that if I multiply all the coordinates by 100, this example does return the correct intersection:
const b2 = b.map(p => ({x:p.x*100, y:p.y*100}));
const c2 = c.map(p => ({x:p.x*100, y:p.y*100}));
intersect(b2, c2); // returns the correct result
Example 2
const square = [{"x":99.99999999999999,"y":0},{"x":200,"y":-1.2246467991473532e-14},{"x":200,"y":99.99999999999999},{"x":100,"y":100.00000000000003}];
const octagon = [{"x":0,"y":0},{"x":200,"y":0},{"x":270.71067811865476,"y":70.71067811865476},{"x":270.71067811865476,"y":270.71067811865476},{"x":200,"y":341.4213562373095},{"x":0,"y":341.4213562373095},{"x":-70.71067811865476,"y":270.71067811865476},{"x":-70.71067811865476,"y":70.71067811865474}];
intersect(square, octagon); // infinite loop
My environment
I am using solution.js from the current latest version of the gh-pages branch (with debug code at the end commented out) : https://github.com/vrd/js-intersect/blob/fd759a8d5c21d63b14120eae7c621364dc587858/solution.js
I've tested intersect(b, c)
, intersect(b2, c2)
, and intersect(square, octagon)
on Chrome and Firefox on MacOS.
Fixed example 1. Will look into example 2 later.
Nice! Thanks for taking a look (& quickly!) :)
Line 140 in 99fc6e8
.toPrecision() may be changing the type there?