效果:

代码讲解:
首先导入模块。
from turtle import *
from math import *
获取桃心边上点的坐标。
def getXY(a,t):
x=a*(15*(sin(t)**3))
y=a*(15*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t))
return x,y
绘制桃心形函数
def draw():
pensize(3)
pencolor("red")
speed(0)
tracer(5)
pu()
a=8
t=0
while t<=360:
x,y=getXY(a,t)
goto(x,y)
pd()
dot(10)
t+=1
pu()
执行函数,最后保持窗口显示
draw()
done()
最终代码
from turtle import *
from math import *
def getXY(a,t):
x=a*(15*(sin(t)**3))
y=a*(15*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t))
return x,y
def draw():
pensize(3)
pencolor("red")
speed(0)
tracer(5)
pu()
a=8
t=0
while t<=360:
x,y=getXY(a,t)
goto(x,y)
pd()
dot(10)
t+=1
pu()
draw()
done()
喜欢的话就点赞关注吧!订阅Turtle画图专栏,查看更多往期文章吧!
这段代码演示了如何利用Python的Turtle模块绘制一个精美的桃心图案。通过数学函数计算每个点的坐标,然后连接这些点形成轮廓。代码中定义了获取桃心边上的点的坐标函数,并在循环中绘制点,最终形成完整的心形。颜色为红色,线条宽度为3,速度设置为最快。喜欢Python图形绘制的读者不容错过。
1569

被折叠的 条评论
为什么被折叠?



