python - 画圆极其不一致?

标签 python turtle-graphics

对于一个学校项目,我使用 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()

我基本上使用了圆方程的参数形式。这是结果:

enter image description here

关于python - 画圆极其不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57743649/

相关文章:

python - 如何从两个具有相同型号名称的应用程序中删除对象?

python - 我可以在 Python 中将 io.BytesIO() 流通过管道传输到 subprocess.popen() 吗?

python - 迭代 ndarray 的某些(但不是全部)维度

python - turtle 图形无响应

Python 使用 turtle 按钮

python - 如何编写一个Python程序来读取输入并将其转换为带有 turtle 图形的预定义14段字符?

Python pandas 数据框和 excel : Add cell background color

python - 在 Keras 中使用 imagedatagenerator 添加更多训练数据集

python - 在 Python 中使用随机 x、y 坐标绘制阿基米德螺线

python - Turtle 迷你项目 - Udacity - Python - 围绕另一个形状绘制/移动一个形状