简体   繁体   中英

Save bezier curve to a file in python

I am using bezier to generate a Bezier curve. Below is the sample code:

import bezier
nodes = np.array([
     [0.0 ,  0.0],
     [0.25,  2.0],
     [0.5 , -2.0],
     [0.75,  2.0],
     [1.0 ,  0.3],
 ])
curve = bezier.Curve.from_nodes(nodes)

import matplotlib.pyplot as plt
curve.plot(num_pts=256)
plt.show()

Please see below the generated plot. 在此处输入图片说明

I want to save this trajectory (let say to a file). In other words, I want to save x,y value of each point in this curve. I was expecting it to return a numpy array but it is not. kindly suggest.

ax = plt.gca() 
line = ax.lines[0]
x = line.get_xdata()
y = line.get_ydata()
xy = np.vstack([x,y]).transpose()
np.save('bezier_data', xy)

I worked it out from the answer here . Those xvals and yvals are NumPy arrays you can put them into one if you need it.

Might help to not execute plt.show() before these lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM