如何用Python画出玫瑰曲线和蝴蝶曲线
1.绘制玫瑰曲线。
化为代码语言就是这样:
g为度数0 p=a*sin(n*g) x=a*sin(n*g)*cosg y=a*sin(n*g)*sing
然后这个n是要注意的,
其中,参数a控制叶子的长度;参数n控制叶子的数量,并影响曲线闭合周期。 当n为奇数时,玫瑰曲线的叶子数为n,闭合周期为π,即参数θ的取值范围为0~π。 当n为偶数时,玫瑰曲线的叶子数为2n,闭合周期为2π,即参数θ的取值范围为0~2π。
这次我们直接做偶数,奇数你们看我代码自己改改范围就会了,很简单的
import math import turtle as t # 初始化 n=6 #数量 a=100 #叶长 g=0 #度数 x=0 #坐标 y=0 t.setup(500,500) #窗口长和宽 while g<math.pi*2: x=a*math.sin(n*g)*math.cos(g) y=a*math.sin(n*g)*math.sin(g) t.goto(x,y) g=g+0.01 if g>math.pi*2: g=math.pi*2 x = a * math.sin(n * g) * math.cos(g) y = a * math.sin(n * g) * math.sin(g) t.goto(x, y) #回到原点 t.done()
接下来是蝴蝶曲线
绘制蝴蝶曲线,其坐标方程,参数方程如下。
具体代码就是这样的,比较多
t为度数0 p=e^cost-2cos4t+(sin(t/12))^5 x=a*sint*[e^cost-2cos4t+(sin(t/12))^5] y=a*cost*[e^cost-2cos4t+(sin(t/12))^5]
然后就是这个范围问题,上面图片已经说了,就是0~24π
代码如下:
import turtle as tt import math as m a=60 #叶长 t=0 #度数 x=0 #坐标 y=0 tt.setup(500,500) #窗口的长宽 while t<24*m.pi: x = a * m.sin(t) * (m.pow(m.e, m.cos(t)) - 2 * m.cos(4 * t) + m.pow(m.sin(t / 12), 5)) y = a * m.cos(t) * (m.pow(m.e, m.cos(t)) - 2 * m.cos(4 * t) + m.pow(m.sin(t / 12), 5)) tt.goto(x,y) t =t + 0.1 if t>24*m.pi: t=24*m.pi x = a * m.sin(t) * (m.pow(m.e, m.cos(t)) - 2 * m.cos(4 * t) + m.pow(m.sin(t / 12), 5)) y = a * m.cos(t) * (m.pow(m.e, m.cos(t)) - 2 * m.cos(4 * t) + m.pow(m.sin(t / 12), 5)) tt.goto(x,y) tt.done()
因为公式太长了,所以我都隔开了一点,方便你们理解,实在看不下去的话,其实看玫瑰曲线就好了,学会那个基本就学会这个了,就是套公式,获取坐标,然后就直接画过去,然后继续循环。
你们理解了吗,代码还是很好理解吧0.0
好久没更新咯,改天看看能不能更新一下数据结构的常规题目
下一篇:
Python菜鸡笔记--控制流程