pythonrecursionbezier

Recursive function of Bezier Curve python


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?


Solution

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