m157: Bezier Curves

1961 days ago by jonesbe

t = var('t') # define a symbolic variable 't' # # returns parametric equations for the Bezier curve through # control points P0, ..., P4 # def bezier_curve(P0, P1, P2, P3): x(t) = P0[0]*(1-t)^3 + 3*P1[0]*t*(1-t)^2 + 3*P2[0]*t^2*(1-t) + P3[0]*t^3 y(t) = P0[1]*(1-t)^3 + 3*P1[1]*t*(1-t)^2 + 3*P2[1]*t^2*(1-t) + P3[1]*t^3 return [x,y] 
       
# define your points P0 = (4,1) P1 = (50, 10) P2 = (50, 42) P3 = (40, 5) # create and display a plot of the Bezier curve, points, and connecting line segments s = 20 PLOT = point(P0, size=s) + point(P1, size=s) + point(P2, size=s) + point(P3, size=s) PLOT += line([P0, P1, P2, P3], color='green') PLOT += parametric_plot(bezier_curve(P0, P1, P2, P3), (t,0,1), color='red') show(PLOT, figsize=4) 
       
# an example of two Bezier curves plotted on the same screen P0 = (4,1) P1 = (50, 10) P2 = (50, 42) P3 = (40, 5) # create and display a plot of the Bezier curve, points, and connecting line segments PLOT = Graphics() # creates a blank graphics object s = 20 PLOT += point(P0, size=s) + point(P1, size=s) + point(P2, size=s) + point(P3, size=s) PLOT += line([P0, P1, P2, P3], color='green', linestyle='dashed') PLOT += parametric_plot(bezier_curve(P0, P1, P2, P3), (t,0,1), color='red') # The second set of points for curve #2... Q0 = (40,5) Q1 = (50, 10) Q2 = (55, 20) Q3 = (65, 0) # create and display a plot of the Bezier curve, points, and connecting line segments s = 20 PLOT += point(Q0, size=s) + point(Q1, size=s) + point(Q2, size=s) + point(Q3, size=s) PLOT += line([Q0, Q1, Q2, Q3], color='green', linestyle='dashed') PLOT += parametric_plot(bezier_curve(Q0, Q1, Q2, Q3), (t,0,1), color='red') show(PLOT, figsize=4)