Python绘制玫瑰曲线(附带源码)
在数学中有一些美丽的曲线图形,如螺旋线、摆线、双纽线、蔓叶线、心脏线、渐开线、玫瑰曲线、蝴蝶曲线……这些形状各异、简繁有别的数学曲线图形为看似枯燥的数学公式披上了精彩纷呈的美丽衣裳。
在数学曲线的百花园中,玫瑰曲线算得上个中翘楚,它的数学方程简单,曲线变化众多,根据参数的变化能展现出姿态万千的优美形状。
玫瑰曲线可用极坐标方程表示为:
也可以用参数方程表示为:
式中,参数 a 控制叶子的长度;参数 n 控制叶子的数量,并影响曲线闭合周期。当 n 为奇数时,玫瑰曲线的叶子数为 n,闭合周期为 π,即参数 θ 的取值范围为 0~π,才能使玫瑰曲线闭合为完整图形。当 n 为偶数时,玫瑰曲线的叶子数为 2n,闭合周期为 2π,即参数 θ 的取值范围为 0~2π。
假如要利用玫瑰曲线的参数方程绘制三叶玫瑰曲线,则参数 n 的值可以设定为 3,参数 a 的值可以设定叶子的长度(如 100)。因为参数 n=3 是奇数,所以三叶玫瑰曲线的闭合周期为 π。
绘制玫瑰曲线的编程思路:在一个循环结构中,让参数 θ 从 0 变化到 π,再利用玫瑰曲线的参数方程求出点坐标 x 和 y 的值,并通过绘图库绘制一系列连续的点,最终绘制出一个完整的玫瑰曲线图形。
【实例 1】使用 turtle 模块(海龟绘图)绘制图案。

图 3 玫瑰曲线
可修改代码中的 n 值,绘制不同代玫瑰曲线。
【实例 2】利用海龟绘制盛开的玫瑰花。

图 4 盛开的玫瑰花
在数学曲线的百花园中,玫瑰曲线算得上个中翘楚,它的数学方程简单,曲线变化众多,根据参数的变化能展现出姿态万千的优美形状。
玫瑰曲线可用极坐标方程表示为:

也可以用参数方程表示为:

式中,参数 a 控制叶子的长度;参数 n 控制叶子的数量,并影响曲线闭合周期。当 n 为奇数时,玫瑰曲线的叶子数为 n,闭合周期为 π,即参数 θ 的取值范围为 0~π,才能使玫瑰曲线闭合为完整图形。当 n 为偶数时,玫瑰曲线的叶子数为 2n,闭合周期为 2π,即参数 θ 的取值范围为 0~2π。
算法分析
在数学世界中,像玫瑰曲线这样美丽的曲线图形实际上是由简单的函数关系生成的。利用曲线函数的参数方程,可以在平面直角坐标系中方便地绘制出它们的图形。假如要利用玫瑰曲线的参数方程绘制三叶玫瑰曲线,则参数 n 的值可以设定为 3,参数 a 的值可以设定叶子的长度(如 100)。因为参数 n=3 是奇数,所以三叶玫瑰曲线的闭合周期为 π。
绘制玫瑰曲线的编程思路:在一个循环结构中,让参数 θ 从 0 变化到 π,再利用玫瑰曲线的参数方程求出点坐标 x 和 y 的值,并通过绘图库绘制一系列连续的点,最终绘制出一个完整的玫瑰曲线图形。
经典应用
根据上述算法分析中给出的编程思路,编程绘制玫瑰曲线的图形。下面通过两个实例演示利用 Python 绘制玫瑰曲线。【实例 1】使用 turtle 模块(海龟绘图)绘制图案。
from turtle import *
from math import *
def rose(a,n):
t = 0
while t <= cycle:
x = cos(t) * a * (sin(n * t))
y = sin(t) * a * (sin(n * t))
goto(x,y)
dot(10)
pd()
t + = 1
pu()
speed(0)
tracer(100)
pencolor("blue")
pensize(5)
pu()
cycle = 360
a = 150
n = 2
rose(a,n)
hideturtle()
done()
运行程序,当 n=2 时,得到效果如下图(a)所示;当 n=3 时,得到效果如下图(b)所示;当 n=8 时,得到效果如下图(c)所示。
图 3 玫瑰曲线
可修改代码中的 n 值,绘制不同代玫瑰曲线。
【实例 2】利用海龟绘制盛开的玫瑰花。
import turtle as t
t.setup(800,800)
t.hideturtle()
t.speed(11)
t.penup()
t.goto(50,-450)
t.pensize(5)
t.pencolor("black")
t.seth(140)
t.pendown()
t.speed(10)
t.circle(-300,60)
t.fd(100)
#1ye
t.seth(10)
t.fd(50)
t.fillcolor("green")
t.begin_fill()
t.right(40)
t.circle(120,80)
t.left(100)
t.circle(120,80)
t.end_fill()
t.seth(10)
t.fd(90)
t.speed(11)
t.penup()
t.fd(-140)
t.seth(80)
#2ye
t.pendown()
t.speed(10)
t.fd(70)
t.seth(160)
t.fd(50)
t.fillcolor("green")
t.begin_fill()
t.right(40)
t.circle(120,80)
t.left(100)
t.circle(120,80)
t.end_fill()
t.seth(160)
t.fd(90)
t.speed(11)
t.penup()
t.fd(-140)
t.seth(80)
t.pendown()
t.speed(10)
#
t.fd(100)
#1ban
t.seth(-20)
t.fillcolor("crimson")
t.begin_fill()
t.circle(100,100)
t.circle(-110,70)
t.seth(179)
t.circle(223,76)
t.end_fill()
#2ban
t.speed(11)
t.fillcolor("red")
t.begin_fill()
t.left(180)
t.circle(-223,60)
t.seth(70)
t.speed(10)
t.circle(-213,15) #55
t.left(70) #125
t.circle(200,70)
t.seth(-80)
t.circle(-170,40)
t.circle(124,94)
t.end_fill()
t.speed(11)
t.penup()
t.right(180)
t.circle(-124,94)
t.circle(170,40)
t.pendown()
t.speed(10)
t.seth(-60)
t.circle(175,70)
t.seth(235)
t.circle(300,12)
t.right(180)
t.circle(-300,12)
t.seth(125)
t.circle(150,60)
t.seth(70)
t.fd(-20)
t.fd(20)
t.seth(-45)
t.circle(150,40)
t.seth(66)
t.fd(-18.5)
t.fd(18.5)
t.seth(140)
t.circle(150,27)
t.seth(60)
t.fd(-8)
t.speed(11)
t.penup()
t.left(20.8)
t.fd(-250.5)
#3ban
t.pendown()
t.speed(10)
t.fillcolor("crimson")
t.begin_fill()
t.seth(160)
t.circle(-140,85)
t.circle(100,70)
t.right(165)
t.circle(-200,32)
t.speed(11)
t.seth(-105)
t.circle(-170,14.5)
t.circle(123,94)
t.end_fill()
运行程序,效果如下图所示:
图 4 盛开的玫瑰花
ICP备案:
公安联网备案: