对于一个学校项目,我使用 Python Turtle 制作“头像”。我有一头卷发,所以我编写了一些代码来绘制一个黑色的半圆,每 10 度左右它就会停止,并绘制一个更小的相同颜色的圆,然后继续。
代码有效吗?它对前 3 个较小的圆圈执行了预期的操作,但在第 4 个较小的圆圈上似乎是随机的。我什至将绘制半圆的度数设置为 10000,但它只完成了第 4 个较小的圆的 3/4。
import turtle
t = turtle.Turtle() #defining Turtle
def drawHair():
##debug, getting turtle to needed pos.
t.color("Moccasin")
for x in range (90):
t.forward(2.5)
t.left(1)
t.setheading(90)
##
t.color("Black")
cTime = 0 #"Timer" to determine a smaller "Curl"
for x in range (180): #SUPPOSED to draw a half-circle
t.forward(2.5) #
t.left(1) #
cTime = cTime + 1 ##For every "Degree" in the main half-circle,
##add 1 to the "Timer"
print("circle = " + str(cTime)) #debug
if cTime == 10: #If "Timer has reached it's limit"
cTime = 0 #Reset timer
for x in range (360): #Draw a full, smaller circle
t.forward(-0.4) #
t.left(1) #
我知道这比应有的更复杂。我只是想知道为什么会出现这个问题以及如何解决它。
编辑:/image/z9kF6.jpg (证明)
最佳答案
你进行了太多的抽奖,这似乎是 repl.it 不喜欢的。实际上有一个circle
Turtle
中的方法为您绘制圆(和半圆)!这比使用 for 循环绘制要快得多。
使用这个和一些数学,我想出了这个代码:
import turtle
from math import cos, sin, pi
t = turtle.Turtle() #defining Turtle
def drawHair():
##debug, getting turtle to needed pos.
t.color("Moccasin")
t.radians()
t.setheading(-pi / 2)
t.circle(140, extent=pi) # bottom semi circle
t.color("Black")
t.circle(140, extent=pi) # top semi circle
for x in range(19):
t.penup()
t.goto(cos(x*pi/18)*180+140, sin(x*pi/18)*180) # position for each curl
t.setheading(x*pi/18 + pi/2)
t.pendown()
t.circle(20)
drawHair()
我基本上使用了圆方程的参数形式。这是结果:
关于python - 画圆极其不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57743649/