如何使用Turtle绘制虚线螺旋图



我的代码一直工作到某个时刻,然后它在一个地方螺旋上升。尝试调试它,但没有帮助。

电流输出:在此处输入图像描述预期输出:类似这样的东西而不是破折号。。。我在用圆点在此处输入图像描述

from turtle import Turtle, done
from math import pi, copysign
chinky = Turtle()
colours = ["DeepPink2", "DarkTurquoise", "chartreuse3", "CornflowerBlue"]
j = 0

def draw_spirograph(size_of_gap, radius):
global j
for i in range(int(360 / size_of_gap)):
chinky.color(colours[j])
if j < 3:
j += 1
else:
j = 0
draw_dotted_circle(radius, 5)
chinky.setheading(chinky.heading() + size_of_gap)

def draw_dotted_circle(radius, dot_size):
chinky.penup()
chinky.sety(radius)
diameter = 2 * radius
circumference = pi * diameter
dot_extent = 360 * dot_size / circumference  # diameter to angle
times_y_crossed = 0
x_sign = 1
while times_y_crossed < 2:
chinky.dot(dot_size)  # draw the dot
chinky.right(dot_extent)
chinky.forward(dot_size)
chinky.right(dot_extent)  # draw the gap
chinky.forward(dot_size)
x_sign_new = copysign(1, chinky.xcor())
if x_sign_new != x_sign:
times_y_crossed += 1
x_sign = x_sign_new
chinky.pendown()

chinky.speed("fastest")
draw_spirograph(5, 100)
done()

您可以通过创建一个自定义圆函数来使用分段来完成此操作,该函数将使用交替的颜色绘制分段的圆。以一定角度移动乌龟可以让你在不需要做任何三角运算的情况下绘制线段。

import turtle as tg
def segCircle(x,y,radius,step):
colors = ["DeepPink2", "DarkTurquoise", "chartreuse3", "CornflowerBlue"]
c      = 0                   # color index
prev   = None                # start of segment
for a in range(0,360,step):  # run through segment points
tg.penup()
tg.goto(x,y)             # center of circle
tg.setheading(a)         # segment point angle
tg.forward(radius)       # move to perimeter
if prev:
tg.pendown()         # draw line from previous segment point
tg.color(colors[0])
colors.append(colors.pop(0)) # rotate colors
tg.goto(*prev)
prev = None
else:
prev = tg.pos()     # skip every other point

绘制肺活量图:

wn = tg.Screen() # setup for "instantaneous drawing"
wn.tracer(0) 
angle  = 10
radius = 100
for a in range(0,360,angle):       # offset circles around center
tg.penup() 
tg.goto(0,0)                   # move to origin
tg.setheading(a)               # set offsetted angle
tg.forward(radius)             # go to center of circle
segCircle(*tg.pos(),radius,angle//2) # draw it

wn.update()    # render drawing in one paint
wn.mainloop() 

最新更新