Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168730
Image: ubuntu2004
#Exploring second degree Bezier curves with weigths
#The control points defining the curve b0=vector([1,0]) b1=vector([1,1]) b2=vector([0,1])
#The Bernstein polynomial def Bpoly(t,n,i): return binomial(n,i)*t^i*(1-t)^(n-i)
#The Bezier curve of degree 2 defined by the control points var('t') bez=Bpoly(t,2,0)*b0+Bpoly(t,2,1)*b1+Bpoly(t,2,2)*b2 print bez
((t - 1)^2 - 2*(t - 1)*t, -2*(t - 1)*t + t^2)
#Plotting the Bezier curve, t in [0,1] parametric_plot(bez,(0,1),aspect_ratio=1)
#We now want to explore how the curve changes when we introduce weights
#Defining the weights as variables, and using them to define the new control points. Note that the points are now in projective 3-space. var('w0 w1 w2') b0_=w0*vector([1,b0[0],b0[1]]) b1_=w1*vector([1,b1[0],b1[1]]) b2_=w2*vector([1,b2[0],b2[1]]) print b0_, b1_, b2_
(w0, w0, 0) (w1, w1, w1) (w2, 0, w2)
#The Bezier curve defined by the points: var('t') bez_=Bpoly(t,2,0)*b0_+Bpoly(t,2,1)*b1_+Bpoly(t,2,2)*b2_ print bez_
((t - 1)^2*w0 - 2*(t - 1)*t*w1 + t^2*w2, (t - 1)^2*w0 - 2*(t - 1)*t*w1, -2*(t - 1)*t*w1 + t^2*w2)
#In order to plot the curve, we project the affine plane (using the first coordinate) def projection(p_): return vector([p_[1]/p_[0],p_[2]/p_[0]])
#Projecting the Bezier curve bez1=projection(bez_) print bez1
(((t - 1)^2*w0 - 2*(t - 1)*t*w1)/((t - 1)^2*w0 - 2*(t - 1)*t*w1 + t^2*w2), -(2*(t - 1)*t*w1 - t^2*w2)/((t - 1)^2*w0 - 2*(t - 1)*t*w1 + t^2*w2))
#We also need to assign a value to the "wi"s before we can plot bez2=bez1(w0=1,w1=1,w2=1)
#Finally we can plot the curve: parametric_plot(bez2,(0,1),aspect_ratio=1)
#We now want to be able to manipulate the weights and see the effect on the curve.
@interact def weighted_bezier_curve( W0=(0.001,5), W1=(0,5), W2=(0,5) ): graph=parametric_plot(bez1(w0=W0,w1=W1,w2=W2),(0,1),aspect_ratio=1) graph.show(figsize=3) #The minimum for W0 is >0 to avoid error message on first display.
W0 
W1 
W2 
[removed]
[removed]
[removed]
[removed]