Skip to content

Instantly share code, notes, and snippets.

@majkrzak
Created July 9, 2015 18:25
Show Gist options
  • Save majkrzak/1f2ef148ea40b30544f4 to your computer and use it in GitHub Desktop.
Save majkrzak/1f2ef148ea40b30544f4 to your computer and use it in GitHub Desktop.
from sympy import Matrix, Piecewise
from sympy.geometry import Curve
def cubic_beizer_poly(a,b,c,d,t):
return (Matrix([[a,b,c,d]])*Matrix([[-1,3,-3,1],[3,-6,3,0],[-3,3,0,0],[1,0,0,0]])*Matrix([[t**3],[t**2],[t],[1]])).det()
def cubic_beizer_curve(A,B,C,D,t):
return Curve((
cubic_beizer_poly(A[0],B[0],C[0],D[0],t),
cubic_beizer_poly(A[1],B[1],C[1],D[1],t)),
(t,0,1))
def cubic_beizer_multi_curve(P,t):
n = (len(P)-1)//3
return Curve((
Piecewise(*[
(cubic_beizer_poly(*([p[0] for p in P][3*i:3*i+4]),t=t-i),t<=i+1) for i in range(n)
]),
Piecewise(*[
(cubic_beizer_poly(*([p[1] for p in P][3*i:3*i+4]),t=t-i),t<=i+1) for i in range(n)
])),
(t,0,n))
if __name__ == '__main__':
from sympy.abc import t
from sympy.plotting import plot_parametric
p = [(21.011173,46.041281),(16.768532,27.454475),(10.707617,19.777315),(29.092393,30.484932),(47.477169,41.192549),(61.013214,54.122502),(40.810163,44.425038),(20.607112,34.727573),(59.703099,63.666328),(36.769552,60.991539),(2.1255008,56.950929),(22.875342,54.208117),(21.011173,46.041281)]
c = cubic_beizer_multi_curve(p,t)
plot_parametric(c.functions,c.limits,adaptive=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment