mathandy / svgpathtools

A collection of tools for manipulating and analyzing SVG Path objects and Bezier curves.
MIT License
557 stars 142 forks source link

Cannot get the intersection between two Bezier Curves #196

Closed leviethung2103 closed 1 year ago

leviethung2103 commented 1 year ago

Currently, I cannot get the intersection between two Bezier Curevs.

from svgpathtools import parse_path
path1 = parse_path("M52.75,10.1c1.14,1.14,1.81,2.53,1.81,4.18c0,4.21-0.06,5.09-0.06,7.72")
path2 = parse_path("M18.25,25.04c3.5,0.71,5.92,0.75,9.11,0.39c13.77-1.55,40.1-3.75,54.65-4.35c2.83-0.12,6.3-0.22,9,0.78")
path1.intersect(path2)

This program returns empty list

Here is the image of two paths

CleanShot 2023-03-15 at 13 56 13@2x
mathandy commented 1 year ago

I think there's no intersection -- it just looks that way cause of the thickness. Try

from svgpathtools import parse_path, disvg
path1 = parse_path("M52.75,10.1c1.14,1.14,1.81,2.53,1.81,4.18c0,4.21-0.06,5.09-0.06,7.72")
path2 = parse_path("M18.25,25.04c3.5,0.71,5.92,0.75,9.11,0.39c13.77-1.55,40.1-3.75,54.65-4.35c2.83-0.12,6.3-0.22,9,0.78")
disvg([path1, path2])
image
leviethung2103 commented 1 year ago

Thank you for your answer. I think the main problem here is at stroke width. Can we set the stroke width to 2 or 3 and then find the intersection between two paths ?

Thank you in advance.

mathandy commented 1 year ago

svgpathtools doesn't have feature to do that. It's mostly designed for analysis of the curves themselves (with little consideration given to stroke width). You could make the two curves different colors and rasterize (e.g. save as a PNG). Then it would just be a matter of looking for red pixels that are next to green pixels (or whatever colors you used).

You might also be interested in the Path.radialrange() method, which can be used to find the nearest point in one path to another path. There's an example here: https://github.com/mathandy/svgpathtools/blob/master/examples/distance-between-two-svg-paths-example.py