Изо всех сил пытался реализовать в моем коде более изящные квадратики и задавался вопросом, может ли кто-нибудь взглянуть на это? - PullRequest
2 голосов
/ 06 февраля 2020

Итак, вот что я попробовал:

def bezier(a):
# find order of curve from number of control points
n = np.shape(a)[0]-1
# initialise arrays
B = np.zeros([101, 2])
terms = np.zeros([n+1, 2])
# create an array of values for t from 0 to 1 in 101 steps
t = np.linspace(0, 1, 101)
# loop through all t values
for i in range(0, 101):
    #calculate terms inside sum in equation 13
    for j in range(0, n + 1):

        terms[j,:] = (1 - t[i])**2 * a[0,:] + 2 * t[i] * (1 - t[i]) * a[1,:] + t[i]**2 * a[2,:]
    #sum terms to find Bezier curve
    B[i, :] = sum(terms, 0)
# plot Bezier
pl.plot(B[:, 0], B[:, 1])
# plot control points
pl.plot(a[:, 0], a[:, 1],'ko')
# plot control polygon
pl.plot(a[:, 0], a[:, 1],'k')
return B

с:

a = np.array([[0, 0], [0.5, 1], [1, 0]])
B = bezier(a)

и он возвращает: этот график , который, как вы можете видеть, делает не соответствуют моим контрольным точкам

Любая помощь приветствуется, спасибо.

1 Ответ

0 голосов
/ 27 марта 2020

Сумма свыше j является избыточной. Получается, что вы создаете свою кривую Безье, но суммируете ее три раза, таким образом получая что-то, что в три раза больше необходимого.

import numpy as np
import matplotlib.pyplot as pl

def bezier(a):
    # find order of curve from number of control points
    n = np.shape(a)[0]-1
    # initialise arrays
    B = np.zeros([101, 2])
    terms = np.zeros([n+1, 2])
    # create an array of values for t from 0 to 1 in 101 steps
    t = np.linspace(0, 1, 101)
    # loop through all t values
    for i in range(0, 101):
        #calculate terms inside sum in equation 13
        B[i, :] = (1 - t[i])**2 * a[0,:] + 2 * t[i] * (1 - t[i]) * a[1,:] + t[i]**2 * a[2,:]
    # plot Bezier
    pl.plot(B[:, 0], B[:, 1])
    # plot control points
    pl.plot(a[:, 0], a[:, 1],'ko')
    # plot control polygon
    pl.plot(a[:, 0], a[:, 1],'k')
    return B

a = np.array([[0, 0], [0.5, 1], [1, 0]])
B = bezier(a)
pl.show()

Graph of the correct curve

Я бы также рекомендовал переименовать a во что-то более наглядное, например controlPts.

.
...