Bezier Curve Python


# using the explicit formula
def bezier_curve_explicit(def_points, speed=0.01): # WARNING: May run into floating-point issues
    n = len(def_points)
    points = []
    for t in [_ * speed for _ in range(int((1 + speed * 2) // speed))]: # get values between 0 and 1
        points.append([sum(math.comb(n - 1, i) * ((1 - t) ** (n - 1 - i)) * (t ** i) * def_points[i][0] for i in range(n)), sum(math.comb(n - 1, i) * ((1 - t) ** (n - 1 - i)) * (t ** i) * def_points[i][1] for i in range(n))])
    return points

# using the recursive formula

def _bezier_curve_point(point_list, t, x_or_y):
    if len(point_list) == 1:
        return point_list[0][x_or_y]
    return round(_bezier_curve_point(point_list[:-1], t, x_or_y) * (1 - t) + _bezier_curve_point(point_list[1:], t, x_or_y) * t, 5)


def bezier_curve_recursive(def_points, speed=0.01):
    points = []
    for t in [_ * speed for _ in range(int((1 + speed * 2) // speed))]:
        points.append([_bezier_curve_point(def_points, t, 0), _bezier_curve_point(def_points, t, 1)])
    return points

source: wikipedia article on bezier curves
NoneOne_