title: Smooth spline representation of an arbitrary contour, f(length) --> x,y
body: <p>Suppose I have a set of x,y coordinates that mark points along contour. Is there a way that I can build a spline representation of the contour that I can evaluate at a particular position along its length and recover interpolated x,y coordinates?</p>
  
  <p>It is often not the case that there is a 1:1 correspondence between X and Y values, so univariate splines are no good to me. Bivariate splines would be fine, but as far as I can tell all of the functions for evaluating bivariate splines in <code>scipy.interpolate</code> take x,y values and return z, whereas I need to give z and return x,y (since x,y are points on a line, each z maps to a unique x,y).</p>
  
  <p>Here's a sketch of what I'd like to be able to do:</p>
  
  <pre><code>import numpy as np
  from matplotlib.pyplot import plot
  
  # x,y coordinates of contour points, not monotonically increasing
  x = np.array([ 2.,  1.,  1.,  2.,  2.,  4.,  4.,  3.])
  y = np.array([ 1.,  2.,  3.,  4.,  2.,  3.,  2.,  1.])
  
  # f: X --&gt; Y might not be a 1:1 correspondence
  plot(x,y,'-o')
  
  # get the cumulative distance along the contour
  dist = [0]
  for ii in xrange(x.size-1):
      dist.append(np.sqrt((x[ii+1]-x[ii])**2 + (y[ii+1]-y[ii])**2))
  d = np.array(dist)
  
  # build a spline representation of the contour
  spl = ContourSpline(x,y,d)
  
  # resample it at smaller distance intervals
  interp_d = np.linspace(d[0],d[-1],1000)
  interp_x,interp_y = spl(interp_d)
  </code></pre>