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

python 复制代码
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 复制代码
​
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 复制代码
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")
相关推荐
坊钰1 分钟前
【LangGraph框架入门级】4. 支持联网搜索的智能代理系统
python·ai编程·langgraph
运维@小兵15 分钟前
安装Pycharm并破解
ide·python·pycharm
冬、不眠的夜21 分钟前
Nuitka 将 python 程序编译成 exe 指南
开发语言·python
范中勤36 分钟前
Python Asyncio 协程与 LLM 异步并发原理技术文档(与 Java 对比|彻底澄清所有误区)
python·llm·协程·并发编程·asyncio
Dmz丶37 分钟前
前端转 AI 100 天 Day 21:FastAPI 入门——从命令行到 HTTP 服务
python
QQ142207844938 分钟前
基于 Flask 的「自动任务控制台」设计与踩坑实录
后端·python·flask
吴佳浩 Alben43 分钟前
一文讲透推理引擎性能指标:TTFT、TPOT、KV命中率、并发,到底对应 vLLM / SGLang 的哪个参数?
人工智能·python·语言模型·架构·ai编程·vllm·sglang
用户2986985301444 分钟前
Python 实现 Word 转 XPS、PostScript 与 OFD 格式
后端·python·api
chde2Wang1 小时前
numpy学习2
学习·numpy
Patrick在香港1 小时前
你写的「比较 token」函数,正在用 36 纳秒泄露密码:时序攻击实测
python·安全·标准库·时序攻击·常量时间