2 years ago

#60813

test-img

Shalev Levi Sagzan

How to find bezier coefficients without matrices?

The function get_cubic needs 4 points and i need to find b and c by calculation (a and d is given).

Here is my code and i need help specifically with get_bezier_coef

def get_bezier_coef(points):
    # since the formulas work given that we have n+1 points
    # then n must be this:
    n = len(points) - 1

    # build coefficents matrix
    C = 4 * np.identity(n)
    np.fill_diagonal(C[1:], 1)
    np.fill_diagonal(C[:, 1:], 1)
    C[0, 0] = 2
    C[n - 1, n - 1] = 7
    C[n - 1, n - 2] = 2

    # build points vector
    P = [2. * (2. * points[i] + points[i + 1]) for i in range(n)]
    P[0] = points[0] + 2 * points[1]
    P[n - 1] = 8 * points[n - 1] + points[n]


    # solve system, find a & b
    A = np.linalg.solve(C,P)
    B = [0] * n
    for i in range(n - 1):
        B[i] = 2. * points[i + 1] - A[i + 1]
    B[n - 1] = (A[n - 1] + points[n]) / 2.

    return A, B

# returns the general Bezier cubic formula given 4 control points
def get_cubic(a, b, c, d):
    return lambda t: np.power(1 - t, 3) * a + 3 * np.power(1 - t, 2) * t * b + 3 * (1 - t) * np.power(t,2) * c + np.power(t, 3) * d

# return one cubic curve for each consecutive points
def get_bezier_cubic(points):
    A, B = get_bezier_coef(points)
    return [
        get_cubic(points[i], A[i], B[i], points[i + 1])
        for i in range(len(points) - 1)
    ]

The function get_bezier_coef get list of points [(X0,Y0),(X1,Y1)....] and return the coefficients of the bezier (find the 2 control points between the start and end point). Is there anyway to calculate the coefficients without matrices? Or any other way that will reduce time.

python

interpolation

bezier

piecewise

numerical-analysis

0 Answers

Your Answer

Accepted video resources