Pomax / bezierjs

A nodejs and client-side library for (cubic) Bezier curve work
MIT License
1.73k stars 233 forks source link

Curve intersections often give multiple almost identical results #184

Closed mblinsitu closed 1 year ago

mblinsitu commented 1 year ago

When intersecting two Bezier segments, I often get 2 or 3 intersections that are very close to each other instead of a single one. For example:

        let b1 = new Bezier(100, 100, 200, 150, 400, 600, 500, 300)
        let b2 = new Bezier(100, 500, 150, 550, 400, 100, 500, 100)
        b1.intersects(b2)
        // ["0.466/0.50723", "0.46608/0.50723", "0.46615/0.50723"]

Is this a bug or a feature? I tried to play with the curveIntersectionThreshold parameter but it didn't help.

Thanks!

Pomax commented 1 year ago

This is unfortunately a consequence of the "divide and conquer" algorithm. At the scale of "this bounding box constitutes a little less than a pixel" we get one or more adjacent-but-not-overlapping bounding boxes on one curve, overlapping a similar set of one or more bounding boxes on the other curve. So you can end up with quite a few intersection points that are nearly identical (while at the mathematical level being completely distinct intersections, of course).