最近看完了《隐秘的角落》,除了被玩坏的爬上梗之外。 还有一个贯穿始终的小故事——关于笛卡尔的心形线的故事。两个版本的故事,一个是美好的童话,一个是残忍的真相。
暂且不谈你是愿意相信童话还是愿意相信真相,我们先看下心形线的函数的参数方程。
x=a*(2cos(t)-cos(2t)) y=a*(2sin(t)-sin(2t))
知道了方程,那么我们要实现一半红色一半蓝色的效果也就好说了,a是常数,我们随意赋个值就好,代码中我赋的值为80。接着确定t的取值范围就可以绘图了,分别为:
-π<t<0 0<t<π
先上效果图如下:
具体实现代码如下:
import numpy as np import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False #定义参数方程 def X(a,t): return a*(2*np.sin(t)-np.sin(2*t)) def Y(a,t): return a * (2*np.cos(t) - np.cos(2 * t)) #绘制左半边 t1=[i for i in np.arange(-np.pi,0,0.01)] x1=[X(80,i) for i in t1] y1=[Y(80,i) for i in t1] plt.title("爬山吗?") plt.plot(x1,y1,color='r') plt.text(-150,-50,'fairy',color='r') #绘制右半边 t2=[i for i in np.arange(0,np.pi,0.01)] x2=[X(80,i) for i in t2] y2=[Y(80,i) for i in t2] plt.plot(x2,y2,color='b') plt.text(150,-50,'truth',color='b') plt.show()好了,现在你可以相信
更多精彩内容可以康康这个公众号↓