前面已经画过圆了,那么画弧线就会容易很多。知道弧的度数之后,实际上只需要画相应部分的圆就可以得到想要的弧。
那么下面我们就来定义一个可以画弧线的函数,并且用它来画个美元符号。
import turtle
import math
# 定义一个画多线段的函数,这个函数有五个参数
# t是turtle对象,n是线段数,length为每条线段的长度
# angle每次旋转的角度,direction旋转的方向
def polyline(t, n, length, angle, direction):
# 循环n次画出n条线段
for i in range(n):
# 画边
t.fd(length)
# 判断旋转的方向
if direction == "left":
t.lt(angle)
elif direction == "right":
t.rt(angle)
def arc(t, r, angle, direction):
# 根据半径计算圆的周长
circumference = 2 * math.pi * r
# 根据弧度计算弧长
arc_len = angle / 360 * circumference
# 这里设定线段长度为3,计算线段数,int强制转换成整数
n = int(arc_len / 3) + 1
# 得到整数的线段数之后,重新计算线段的长度,
# 得到的length这时可能是小数了
length = arc_len / n
# 计算每一次旋转的角度
step_angle = angle / n
# 调用前面定义的画多线段的函数来画弧线
polyline(t, n, length, step_angle, direction)
# 定义一个回家的函数,让箭头回到原点和原始的方向
def home(t):
t.pu()
t.home()
t.pd()
# 定义一个画垂直线的函数,这个函数有三个参数
# t为turtle对象,offset为距离原点的偏移量,length控制画线的长度
def vline(t, offset, length):
home(t)
t.pu()
t.fd(offset)
t.pd()
t.setheading(90)
t.fd(length)
t.bk(2 * length)
bob = turtle.Turtle()
# 设置笔的颜色为绿色
bob.pencolor('green')
# 设置笔的粗细为3
bob.pensize('3')
# 将箭头的方向设置为正西
bob.setheading(180)
# 画一条弧度为250°,半径为110,顺时针的弧
arc(bob, 110, 250, 'right')
# 回到原点
home(bob)
# 再画一条弧度为250°,半径为110,顺时针的弧
arc(bob, 110, 250, 'right')
# 原点往左20,画一条垂直线
vline(bob, -20, 250)
# 原点往右20,画一条垂直线
vline(bob, 20, 250)
turtle.mainloop()
来看看“印钞”的过程
联系客服