def curve_intersection(c1, c2, grid=<NUM_LIT:16>):
from scipy.optimize import minimize<EOL>from neuropythy.geometry import segment_intersection_2D<EOL>if c1.coordinates.shape[<NUM_LIT:1>] > c2.coordinates.shape[<NUM_LIT:1>]:<EOL><INDENT>(t1,t2) = curve_intersection(c2, c1, grid=grid)<EOL>return (t2,t1)<EOL><DEDENT>x1s  = c1.coordinates.T<EOL>x2s  = c2.coordinates<EOL>for (ts,te,xs,xe) in zip(c1.t[:-<NUM_LIT:1>], c1.t[<NUM_LIT:1>:], x1s[:-<NUM_LIT:1>], x1s[<NUM_LIT:1>:]):<EOL><INDENT>pts = segment_intersection_2D((xs,xe), (x2s[:,:-<NUM_LIT:1>], x2s[:,<NUM_LIT:1>:]))<EOL>ii = np.where(np.isfinite(pts[<NUM_LIT:0>]))[<NUM_LIT:0>]<EOL>if len(ii) > <NUM_LIT:0>:<EOL><INDENT>ii = ii[<NUM_LIT:0>]<EOL>def f(t): return np.sum((c1(t[<NUM_LIT:0>]) - c2(t[<NUM_LIT:1>]))**<NUM_LIT:2>)<EOL>t01 = <NUM_LIT:0.5>*(ts + te)<EOL>t02 = <NUM_LIT:0.5>*(c2.t[ii] + c2.t[ii+<NUM_LIT:1>])<EOL>(t1,t2) = minimize(f, (t01, t02)).x<EOL>return (t1,t2)<EOL><DEDENT><DEDENT>if pimms.is_vector(grid): (ts1,ts2) = [c.t[<NUM_LIT:0>] + (c.t[-<NUM_LIT:1>] - c.t[<NUM_LIT:0>])*grid for c in (c1,c2)]<EOL>else:                     (ts1,ts2) = [np.linspace(c.t[<NUM_LIT:0>], c.t[-<NUM_LIT:1>], grid) for c in (c1,c2)]<EOL>(pts1,pts2) = [c(ts) for (c,ts) in zip([c1,c2],[ts1,ts2])]<EOL>ds = np.sqrt([np.sum((pts2.T - pp)**<NUM_LIT:2>, axis=<NUM_LIT:1>) for pp in pts1.T])<EOL>(ii,jj) = np.unravel_index(np.argmin(ds), ds.shape)<EOL>(t01,t02) = (ts1[ii], ts2[jj])<EOL>ttt = []<EOL>def f(t): return np.sum((c1(t[<NUM_LIT:0>]) - c2(t[<NUM_LIT:1>]))**<NUM_LIT:2>)<EOL>(t1,t2) = minimize(f, (t01, t02)).x<EOL>return (t1,t2)<EOL>
curve_intersect(c1, c2) yields the parametric distances (t1, t2) such that c1(t1) == c2(t2).

The optional parameter grid may specify the number of grid-points
to use in the initial search for a start-point (default: 16).