您还可以利用镜像;即对于所有t, Bezier([a,b,c],t)== Bezier([c,b,a],1.-t).如果您的结果未显示此行为,那么您的功能将不正确.如果我尝试运行代码,则会因尝试将元组乘以浮点数而收到TypeError;您可能需要扩展该代码,即 def Bezier(point_list,t):如果len(point_list)== 1:返回point_list [0]别的:P1 =贝塞尔(point_list [0:-1],t)P2 =贝塞尔(point_list [1:],t)nt = 1--t返回(nt * P1 [0] + t * P2 [0],nt * P1 [1] + t * P2 [1]) I am asked to design a recursive function called Bezier which parametres are a list of points given, and the point that must be evaluated t. It returns the point in the Bezier curve defined by the control points of the list of points.This is the algorithm that I have done:def Bezier(point_list, t): if len(point_list)==1: return point_list[0] else: P1=Bezier(point_list[0:-1],t) P2=Bezier(point_list[1:],t) P=(1-t)*P1 + t*P2 return Pand this is the list of points given:point_list=[ (0,0), (10,-1), (13,5), (-7,8), (2,2) ]How can I know if my function is correct? 解决方案 It looks correct; you could try it on some sets of points and see if the behavior matches what you expect (ie for control points along x=0, y=0, or x=y, all resulting points should lay along the same line).You can also take advantage of mirroring; ie for all t, Bezier([a, b, c], t) == Bezier([c, b, a], 1.-t). If your results do not show this behavior then your function cannot be correct.If I try to run the code, I get a TypeError for trying to multiply a tuple by a float; you may need to expand that code, iedef Bezier(point_list, t): if len(point_list)==1: return point_list[0] else: P1=Bezier(point_list[0:-1], t) P2=Bezier(point_list[1:], t) nt = 1. - t return (nt * P1[0] + t * P2[0], nt * P1[1] + t * P2[1]) 这篇关于贝塞尔曲线python的递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-17 14:29