Skip to content

Instantly share code, notes, and snippets.

@danmackinlay
Created January 16, 2012 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danmackinlay/1619688 to your computer and use it in GitHub Desktop.
Save danmackinlay/1619688 to your computer and use it in GitHub Desktop.
pure python bezier smoothing for matplotlib
"""
For http://stackoverflow.com/q/5255932
"""
def tangent_interpolate(index, x_array, y_array, dist=0.5):
before_index = max(index - 1, 0)
after_index = min(index + 1, len(x_array))
chord_vector_x = x_array[after_index] - x_array[before_index]
chord_vector_y = y_array[after_index] - y_array[before_index]
if dist>0:
next_point_x_dist = x_array[after_index] - x_array[index]
else:
next_point_x_dist = x_array[index] - x_array[before_index]
scale_factor = float(next_point_x_dist)/chord_vector_x
return x_array[index] + dist * scale_factor * chord_vector_x,\
y_array[index] + dist * scale_factor * chord_vector_y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment