Это Bezier curve Python code
, и я напечатал координаты X
и Y
из Bezier cruve
. Это означает, что точки на кривой, как таблица, показана ниже, но я не смог напечатать t
. Любая помощь, пожалуйста?
X Y t
==============
Как я могу напечатать t?.
Код ниже:
import numpy as np
import scipy.special
import matplotlib.pyplot as plt
import csv
def calc_bezier_path(control_points, n_points=100):
"""
Compute bezier path (trajectory) given control points.
:param control_points: (numpy array)
:param n_points: (int) number of points in the trajectory
:return: (numpy array)
"""
traj = []
for t in np.linspace(0, 1, n_points):
traj.append(bezier(t, control_points))
return np.array(traj)
def bernstein_poly(n, i, t):
"""
Bernstein polynom.
:param n: (int) polynom degree
:param i: (int)
:param t: (float)
:return: (float)
"""
return scipy.special.comb(n, i) * t ** i * (1 - t) ** (n - i)
def bezier(t, control_points):
"""
Return one point on the bezier curve.
:param t: (float) number in [0, 1]
:param control_points: (numpy array)
:return: (numpy array) Coordinates of the point
"""
n = len(control_points) - 1
return np.sum([bernstein_poly(n, i, t) * control_points[i] for i in range(n + 1)], axis=0)
visx, visy = [1,2,10,15,20,25,21], [0,5,1,4,2,3,3]
vis = np.column_stack((visx,visy))
print(vis[0])
path_x, path_y = np.array([]),np.array([])
if len(vis) > 2:
current_control = vis[0]
for x in range(len(vis)-2):
if x != (len(vis)-3):
mid_control = [(vis[x+1,0]+vis[x+2,0])/2, (vis[x+1,1]+vis[x+2,1])/2]
plt.annotate(f"M{x}", mid_control)
bezier_line = calc_bezier_path(np.array([current_control,vis[x+1], mid_control]))
path_x = np.append(path_x, bezier_line.T[0])
path_y = np.append(path_y, bezier_line.T[1])
current_control = mid_control
else:
bezier_line = calc_bezier_path(np.array([current_control, vis[x+1], vis[x+2]]))
path_x = np.append(path_x, bezier_line.T[0])
path_y = np.append(path_y, bezier_line.T[1])
else:
path_x, path_y = visx, visy
with open('my_csv.csv','w',newline='') as f:
fieldnames = ['visx','visy','t']
thewriter = csv.DictWriter(f,fieldnames=fieldnames)
thewriter.writeheader()
for jjj in range(len(path_x)):
thewriter.writerow({'visx':path_x[jjj],'visy':path_y[jjj]})
plt.plot(path_x, path_y)
plt.plot(visx, visy, "--o")
for xy in range(len(vis)):
plt.annotate(f"P{xy}", vis[xy])
plt.show()