贝塞尔曲线python的递归函数 [英] Recursive function of Bezier Curve python
问题描述
要求我设计一个称为Bezier的递归函数,其参数是给定点的列表,以及必须求出的点t.它返回由点列表的控制点定义的Bezier曲线中的点.
这是我完成的算法:
def Bezier(point_list,t):如果len(point_list)== 1:返回point_list [0]别的:P1 =贝塞尔(point_list [0:-1],t)P2 =贝塞尔(point_list [1:],t)P =(1-t)* P1 + t * P2返回P
这是给出的要点列表:
point_list = [(0,0),(10,-1),(13,5),(-7,8),(2,2)]
我怎么知道我的功能是否正确?
看起来正确;您可以在一些点集上进行尝试,看看行为是否符合您的期望(即,对于x = 0,y = 0或x = y上的控制点,所有得到的点都应位于同一条线上).>
您还可以利用镜像;即对于所有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 P
and 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, ie
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)
nt = 1. - t
return (nt * P1[0] + t * P2[0], nt * P1[1] + t * P2[1])
这篇关于贝塞尔曲线python的递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!