Skip to content

Instantly share code, notes, and snippets.

@mathandy
Created December 10, 2016 03:57
Show Gist options
  • Save mathandy/c85736a70b7a54ba301696aacfbc4dbb to your computer and use it in GitHub Desktop.
Save mathandy/c85736a70b7a54ba301696aacfbc4dbb to your computer and use it in GitHub Desktop.
An example of how to compute the distance between two svg path elements in Python with svgpathtools
from svgpathtools import *
# create some example paths
path1 = CubicBezier(1,2+3j,3-5j,4+1j)
path2 = path1.rotated(60).translated(3)
# find minimizer
from scipy.optimize import fminbound
def dist(t):
return path1.radialrange(path2.point(t))[0][0]
T2 = fminbound(dist, 0, 1)
# Let's do a visual check
pt2 = path2.point(T2)
T1 = path1.radialrange(pt2)[0][1]
pt1 = path1.point(T1)
disvg([path1, path2, Line(pt1, pt2)], 'grb', nodes=[pt1, pt2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment