【问题标题】:Python Turtle: Draw concentric circles using circle() methodPython Turtle:使用 circle() 方法绘制同心圆
【发布时间】:2023-06-05 18:26:01
【问题描述】:

我正在展示一个用 Python 的 Turtle 模块绘制的孙子图案, 他要求看同心圆。 我认为使用海龟的circle() 来绘制它们会更快 而不是编写我自己的代码来生成一个圆圈。哈!我被困住了。 我看到产生的圆圈从乌龟的圆周开始 当前位置及其绘制方向取决于海龟当前的位置 运动的方向,但我不知道我需要做什么才能得到 同心圆。 在这一点上,我对有效的生产方式不感兴趣 同心圆:我想看看我必须做什么才能得到 这个工作方式:

def turtle_pos(art,posxy,lift):
    if lift:
        art.penup()
        art.setposition(posxy)
        art.pendown()

def drawit(tshape,tcolor,pen_color,pen_thick,scolor,radius,mv):
    window=turtle.Screen() #Request a screen
    window.bgcolor(scolor) #Set its color

    #...code that defines the turtle trl

    for j in range(1,11):
        turtle_pos(trl,[trl.xcor()+mv,trl.ycor()-mv],1)
        trl.circle(j*radius)

drawit("turtle","purple","green",4,"black",20,30)

【问题讨论】:

  • 你能发布你当前的代码吗?
  • def turtle_pos(art,posxy,lift): if lift: art.penup() art.setposition(posxy) art.pendown() def drawit(ts​​hape,tcolor,pen_color,pen_thick,scolor, radius,mv): window=turtle.Screen() #请求一个屏幕 window.bgcolor(scolor) #设置它的颜色.xcor()+mv,trl.ycor()-mv],1) trl.circle(j*radius) drawit("turtle","purple","green",4,"black",20,30) -- 抱歉,不知道如何格式化。

标签: python turtle-graphics


【解决方案1】:

我目前对有效的生产方式不感兴趣 同心圆:我想看看我必须做些什么才能让这个方式 工作

为了解决 OP 的问题,对其原始代码进行更改以使其正常工作是微不足道的:

turtle_pos(trl, [trl.xcor() + mv, trl.ycor() - mv], 1)
trl.circle(j * radius)

变成:

turtle_pos(trl, [trl.xcor(), trl.ycor() - mv], 1)
trl.circle(j * mv + radius)

带有上述修复和一些样式更改的完整代码:

import turtle

def turtle_pos(art, posxy, lift):
    if lift:
        art.penup()
        art.setposition(posxy)
        art.pendown()

def drawit(tshape, tcolor, pen_color, pen_thick, scolor, radius, mv):
    window = turtle.Screen()  # Request a screen
    window.bgcolor(scolor)  # Set its color

    #...code that defines the turtle trl
    trl = turtle.Turtle(tshape)
    trl.pencolor(pen_color)
    trl.fillcolor(tcolor)  # not filling but makes body of turtle this color
    trl.width(pen_thick)

    for j in range(10):
        turtle_pos(trl, (trl.xcor(), trl.ycor() - mv), True)
        trl.circle(j * mv + radius)

    window.mainloop()

drawit("turtle", "purple", "green", 4, "black", 20, 30)

【讨论】:

    【解决方案2】:

    所以现在我给你一个可以绘制同心圆的确切代码。

    import turtle
    t=turtle.Turtle()
    for i in range(5):
      t.circle(i*10)
      t.penup()
      t.setposition(0,-(i*10))
      t.pendown()
    turtle.done()
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      import turtle
      
      turtle.penup()
      for i in range(1, 500, 50):
          turtle.right(90)    # Face South
          turtle.forward(i)   # Move one radius
          turtle.right(270)   # Back to start heading
          turtle.pendown()    # Put the pen back down
          turtle.circle(i)    # Draw a circle
          turtle.penup()      # Pen up while we go home
          turtle.home()       # Head back to the start pos
      

      创建下面的图片:

      基本上,它会将海龟向下移动一个半径长度,以将所有圆的中心点保持在同一位置。

      【讨论】:

        【解决方案4】:

        来自文档:

        中心是海龟左边的半径单位。

        所以当你开始画一个圆时,无论乌龟在哪里,那个圆的中心在右边有一段距离。在每个圆圈之后,只需向左或向右移动一些像素并绘制另一个圆圈,其半径已根据海龟移动的距离进行了调整。例如,如果你画一个半径为 50 像素的圆,然后向右移动 10 像素,你会再画一个半径为 40 的圆,这两个圆应该是同心的。

        【讨论】:

        • 在当前的 Python 乌龟下,这个答案似乎不再有效。引用的文档仍然相同,但它是虚假的,因为中心相对于海龟的位置取决于海龟的航向。使用默认的初始标题,引用是完全错误的。切换到“logo”模式,它只对初始标题有效。但是您的具体示例目前是错误的,因为您必须以相反的方式移动以使圆同心。而且它不适用于任何其他标题。