【Python】笛卡尔心形线 您所在的位置:网站首页 笛卡尔的爱情表白公式 【Python】笛卡尔心形线

【Python】笛卡尔心形线

2023-09-16 01:51| 来源: 网络整理| 查看: 265

文章目录 简介演示安装方程参数方程极坐标方程 源码拓展仓库其它参考

简介

用python的matplotlib库绘制心形线。

演示 心-直角坐标心-极坐标系扁点的心动态绘制 安装 pip install matplotlib 方程 参数方程

{ x ( θ ) = a ( 1 − c o s θ ) s i n θ y ( θ ) = a ( 1 − c o s θ ) c o s θ \left\{\begin{matrix} x(\theta )=a\left(1-cos\theta \right) sin\theta \\ y(\theta )=a\left(1-cos\theta \right) cos\theta \end{matrix}\right. {x(θ)=a(1−cosθ)sinθy(θ)=a(1−cosθ)cosθ​

极坐标方程

r = a ( 1 − s i n θ ) r=a(1-sin\theta) r=a(1−sinθ)

源码 根据参数方程绘制心形线。 import matplotlib.pyplot as plt import numpy as np def cardioid_parametric(a): theta = np.linspace(0, 2*np.pi, 1000) x = a*(1-np.cos(theta))*np.sin(theta) y = a*(1-np.cos(theta))*np.cos(theta) plt.plot(x, y, c='r') plt.axis('off') plt.savefig('./img/heart.png') plt.show() if __name__ == '__main__': cardioid_parametric(1) # draw a cardioid according to the parametric equation 根据极坐标方程绘制心形线。 import matplotlib.pyplot as plt import numpy as np def cardioid_polar(a): theta = np.linspace(0, 2*np.pi, 1000) r = a*(1 - np.sin(theta)) graph = plt.subplot(111, polar=True) graph.plot(theta, r, color='red') plt.savefig('./img/heart-polar.png') plt.show() if __name__ == '__main__': cardioid_polar(1) # draw a cardioid according to the polar equation 绘制一个扁点的心形线。 import matplotlib.pyplot as plt import numpy as np def cardioid_flat(): t = np.linspace(0, np.pi, 1000) x = np.sin(t) y = np.cos(t) + np.power(x, 2/3) plt.plot(x, y, color='r') plt.plot(-x, y, c='r') plt.axis('off') plt.savefig('./img/heart-flat.png') plt.savefig('./img/heart-flat.png') plt.show() if __name__ == '__main__': cardioid_flat() # darw a flat cardioid 动态绘制心形线。 import numpy as np import matplotlib.pyplot as plt from matplotlib import animation figure = plt.figure() line1, = plt.axes(xlim=(-1.5, 1.5), ylim=(-2.2, 0.45)).plot([], [], c='r') line2, = plt.axes(xlim=(-1.5, 1.5), ylim=(-2.2, 0.45)).plot([], [], c='r') def init(): line1.set_data([], []) line2.set_data([], []) return line1, line2 def update(i, a): theta = np.linspace(0, i/np.pi, 100) x = a*(1-np.cos(theta))*np.sin(theta) y = a*(1-np.cos(theta))*np.cos(theta) line1.set_data(x, y) line2.set_data(-x, y) return line1, line2 def cardioid_animate(a): ani = animation.FuncAnimation(figure, update, init_func=init, frames=11, fargs=(a,), blit=True) plt.axis('off') ani.save('./img/heart.gif') plt.show() if __name__ == '__main__': cardioid_animate(1) # darw a cardioid dynamically 拓展

1650年,斯德哥尔摩街头,52岁的笛卡尔邂逅了18岁瑞典公主克莉丝汀。笛卡尔落魄无比、穷困潦倒又不愿意请求别人的施舍,每天只是拿着破笔破纸研究数学题。有一天,克莉丝汀的马车路过街头发现了笛卡尔是在研究数学。公主便下车询问,最后笛卡尔发现公主很有数学天赋,道别后的几天笛卡尔收到通知,国王要求他做克莉丝汀公主的数学老师,其后几年中相差34岁的笛卡尔和克莉丝汀相爱,国王发现并处死了笛卡尔。在最后笛卡尔写给克莉丝汀的情书中出现了r=a(1-sinθ)的数学坐标方程,解出来是个心形图案,就是著名的“心形线”。这封情书最后被收录到欧洲笛卡尔博物馆。

仓库

https://github.com/XavierJiezou/python-cardioid-matplotlib

其它

love.gif

参考

https://en.wikipedia.org/wiki/Cardioid https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.animation.FuncAnimation.html https://zhuanlan.zhihu.com/p/32380300



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有