今天是2月14号,情人节,作为学python的ICer,可以用python来表达你对Ta的爱。下面我们就来学习这个“爱心操作”
原理介绍
在数学上,心形线用数学公式表示如下:
极坐标方程
水平方向: r=a(1-cosθ) 或 r=a(1+cosθ) (a>0)
垂直方向: r=a(1-sinθ) 或 r=a(1+sinθ) (a>0)
直角坐标方程
x^2+y^2+a*x=a*sqrt(x^2+y^2) 和 x^2+y^2-a*x=a*sqrt(x^2+y^2)
参数方程
x=a*(2*cos(t)-cos(2*t))
y=a*(2*sin(t)-sin(2*t))
代码
运行截图
动态效果
画图保存mp4的方法
先从ffmpeg官网(https://ffmpeg.zeranoe.com/builds)下载,解压并设置环境变量PATH到C:\ffmpeg-20180213-474194a-win64-static\bin。然后在python里用animation.save()来写mp4视频。代码如下:
from matplotlib import animation
ani = animation.FuncAnimation(figure, animate, init_func=init, frames=14, interval=200)
ani.save('heart.mp4')
源码下载
http://www.exasic.com/example/heart.py
欢迎关注ExASIC
分享数字集成电路设计中的经验和方法
Sharing makes work smoother
领取专属 10元无门槛券
私享最新 技术干货