画圈极度不一致?

Drawing Circles extremely inconsistent?

对于一个学校项目,我正在使用 Python Turtle 制作一个 "avatar"。我有卷发,所以我写了一些代码来画一个黑色的半圆,每 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)               #

知道这比应该的更复杂。我只是想知道为什么会出现这个问题以及如何解决它。

编辑:https://imgur.com/a/uYe6UAb(证明)

方式 抽签太多,repl.it 似乎不喜欢。 Turtle里其实有一个circle方法帮你画圆(和半圆)!这比用 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()

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

问题可能是您绘制的圆圈对于 repl.it 而言过于详细——尽管您的代码 应该 可以工作,甚至 Python 海龟的自己的 circle() 函数只使用 60 段,而不是 360,来画一个圆。小圈子就更少了。

这里是你的代码的修改,以更少的部分绘制所有的圆,与你每 10 度绘制更小的圆的愿望同步:

import turtle

def drawHair():
    # get turtle to needed position
    t.color("Moccasin")

    for x in range(15):
        t.forward(15)
        t.left(6)

    t.color("Black")

    for x in range(36):  # draw a half-circle
        t.forward(12.5)
        t.left(5)

        if x % 2 == 0:  # every other segment of the outer circle
            for _ in range(72):  # Draw a full, smaller circle
                t.forward(-2)
                t.left(5)

    t.color("Moccasin")  # finish the face outline

    for x in range(15):
        t.forward(15)
        t.left(6)

    t.hideturtle()

t = turtle.Turtle()

drawHair()

turtle.done()

我似乎在 repl.it 上工作。 (虽然 repl.it 确实有很长的停顿。)尽管减少了部分,但圆圈仍然看起来是圆的:

我假设您不允许使用 turtle.circle() 方法,但如果可以,正如@Sweeper 假设的那样,那么这将成为一个更简单的程序:

import turtle

def drawHair():
    # get turtle to needed position
    t.color("Moccasin")
    t.circle(143, 90)
    t.color("Black")

    for x in range(18):  # draw a half-circle
        t.circle(143, 5)
        t.circle(-23)  # draw a full, smaller circle
        t.circle(143, 5)

    t.color("Moccasin")
    t.circle(143, 90)
    t.hideturtle()

t = turtle.Turtle()

drawHair()

turtle.done()

您会看到圆圈比我的第一个示例稍微粗糙一些,但您可以使用 turtle.circle()steps 参数进行调整。