Recursive function of Bezier Curve python

别来无恙 提交于 2021-02-19 05:44:17

问题


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?


回答1:


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])


来源:https://stackoverflow.com/questions/20334975/recursive-function-of-bezier-curve-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!