python生成十字导杆形平面四杆桃形线

python生成十字导杆形平面四杆桃形线_第1张图片

import sympy as sy
'''
OC=BP=d
OP=p
OB=dcos(fi)
'''
d,p,fi,k= sy.symbols('d,p,fi,k')
#type1
xp=d*(1+sy.cos(fi))*sy.cos(fi)
yp=d*(1+sy.cos(fi))*sy.sin(fi)
xp_c=xp.subs({d:600})
yp_c=yp.subs({d:600})
sy.plot_parametric(xp_c,yp_c,(fi,0,2*sy.pi))

python生成十字导杆形平面四杆桃形线_第2张图片

python生成十字导杆形平面四杆桃形线_第3张图片

​
import sympy as sy
'''
OC=BP=d
OP=p
OB=dcos(fi)
'''
d,p,fi,k= sy.symbols('d,p,fi,k')
#type2
xp=k*d*(1+sy.cos(fi))*sy.sin(fi)
yp=-k*d*(1+sy.cos(fi))*sy.cos(fi)
xp_c=xp.subs({d:600,k:2.85})
yp_c=yp.subs({d:600,k:2.85})
sy.plot_parametric(xp_c,yp_c,(fi,0,2*sy.pi))

python生成十字导杆形平面四杆桃形线_第4张图片

绘制动画

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
class drawAni():
    # func为参数方程
    def __init__(self,traceFuncs, para, txtFunc,ts,xlim,ylim,figsize=(16,9),iniFunc=None):
        self.funcs = traceFuncs     # 画图代码
        self.para = para            # 画图参数
        self.txtFunc = txtFunc
        self.fig = plt.figure(figsize=figsize)
        ax = self.fig.add_subplot(autoscale_on=False,xlim=xlim,ylim=ylim)
        ax.grid()
        if iniFunc!=None:initFunc(ax)
        self.N = len(para)      # 轨迹个数
        self.traces = [ax.plot([],[],p['flag'], lw=p['lw'])[0]
            for p in para]
        self.text = ax.text(0.02,0.85,'',transform=ax.transAxes)
        self.initXY(self.N)
        self.ts = ts    #此为自变量
        self.run(ts)
    def initXY(self,N):
        self.xs = [[] for _ in range(N)]
        self.ys = [[] for _ in range(N)]
    def animate(self,t):
        if(t==self.ts[0]):
            self.initXY(self.N)
        for i in range(self.N):
            x,y = self.funcs[i](t)
            if self.para[i]['flush']==False:
                self.xs[i].append(x)
                self.ys[i].append(y)
                self.traces[i].set_data(self.xs[i], self.ys[i])
            else:
                self.traces[i].set_data(x,y)
        self.text.set_text(self.txtFunc(t))
        return self.traces+[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.1)
        plt.show()
    def save(self,saveName):
        self.ani.save(saveName,writer='pillow',fps=20)
k,d = 2.85,600
​
def traceFunc(theta):
    return k*d*(1+np.cos(theta))*np.sin(theta), -k*d*(1+np.cos(theta))*np.cos(theta)
​
def lineFunc(theta):
    x,y = traceFunc(theta)
    return [0,x], [0,y]
​
def txtFunc(theta):
    th = 180*theta/np.pi
    x,y = traceFunc(theta)
    len = np.sqrt(x**2+y**2)
    txt = f'theta={th:.2f}\nlen={len:.2f}'
    txt += f'len={len:.2f}'
    return txt
​
xlim,ylim = (-2500,2500), (-4000,1000)
ts =  np.linspace(0,6.28,200)
funcs = [lineFunc, traceFunc]
tracePara = [
    {'flag':'o-','lw':2, 'flush':False},
    {'flag':'-','lw':1, 'flush':False}
]
​
an = drawAni(funcs, tracePara, txtFunc,ts, xlim, ylim, (12,9))
an.save("test.gif")

你可能感兴趣的:(python,numpy)