您当前的位置: 首页 >  数学

Xavier Jiezou

暂无认证

  • 1浏览

    0关注

    394博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Python】笛卡尔心形线——数学家的浪漫(花式哄小女友第一天)

Xavier Jiezou 发布时间:2021-07-08 16:39:45 ,浏览量:1

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

简介

pythonmatplotlib库绘制心形线。

演示 心-直角坐标心-极坐标系扁点的心动态绘制 安装
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θ)

源码
  1. 根据参数方程绘制心形线。
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
  1. 根据极坐标方程绘制心形线。
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
  1. 绘制一个扁点的心形线。
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
  1. 动态绘制心形线。
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

关注
打赏
1661408149
查看更多评论
立即登录/注册

微信扫码登录

0.0385s