Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3260633
  • 博文数量: 287
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2110
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(287)

文章存档

2022年(60)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2022-05-23 17:08:48

a,b,c = 5,3,4

def traceFunc(theta):

    x = a*np.cos(theta)

    y = b*np.sin(theta)

    return x,y

def lineFunc(x,y):

    return [-c,x,c], [0,y,0]

def txtFunc(theta):

    th = 180*theta/np.pi

    x,y = traceFunc(theta)

    lenL = np.sqrt((x+c)**2+y**2)

    lenR = np.sqrt((x-c)**2+y**2)

    txt = f'theta={th:.2f}\nlenL={lenL:.2f},lenR={lenR:.2f}\n'

    txt += f'lenL+lenR={lenL+lenR:.2f}'

    return txt

xlim,ylim = (-a,a), (-b,b)

ts =  np.linspace(0,6.28,200)

class drawAni():

    # func为参数方程

    def __init__(self,lineFunc,traceFunc,txtFunc,

        ts,xlim,ylim,figsize=(16,9)):

        self.lineFunc 外汇跟单gendan5.com= lineFunc

        self.traceFunc = traceFunc

        self.txtFunc = txtFunc

        self.fig = plt.figure(figsize=figsize)

        ax = self.fig.add_subplot(autoscale_on=False,

            xlim=xlim,ylim=ylim)

        ax.grid()

        self.line, = ax.plot([],[],'o-',lw=2)

        self.trace, = ax.plot([],[],'-',lw=1)

        self.text = ax.text(0.02,0.85,'',transform=ax.transAxes)

        self.xs, self.ys, self.ts = [],[],ts

        self.run(ts)

    def animate(self,t):

        if(t==self.ts[0]):

            self.xs, self.ys = [],[]

        x,y = self.traceFunc(t)

        self.xs.append(x)

        self.ys.append(y)

        self.line.set_data(self.lineFunc(x,y))

        self.trace.set_data(self.xs,self.ys)

        self.text.set_text(self.txtFunc(t))

        return self.line, self.trace, self.text

    def run(self,ts):

        self.ani = animation.FuncAnimation(self.fig, self.animate, ts, interval=5, blit=True)

        plt.subplots_adjust(left=0.05, right=0.95, top=0.95, bottom=0.05)

        plt.show()

    def save(self,saveName):

        self.ani.save(saveName)

阅读(1498) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~