Python Turtle:使用 circle() 方法绘制同心圆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24636271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:55:00  来源:igfitidea点击:

Python Turtle: Draw concentric circles using circle() method

pythonturtle-graphics

提问by thelma

I was showing a grandson patterns drawn with Python's Turtle module, and he asked to see concentric circles. I thought it would be faster to use the turtle's circle()to draw them than to write my own code for generating a circle. Ha! I am stuck. I see that the circle produced begins its circumference at the turtle's current location and its direction of drawing depends on turtle's current direction of motion, but I can't figure out what I need to do to get concentric circles. I am not at this point interested in an efficient way of producing concentric circles: I want to see what I have to do to get thisway to work:

我正在展示一个用 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)

采纳答案by Noelkd

You can do it like this:

你可以这样做:

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

Which creates the picture below:

这创建了下面的图片:

enter image description here

在此处输入图片说明

Basically it moves the turtle down one radius lenght to keep the center point for all the circles in the same spot.

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

回答by chepner

From the documentation:

从文档:

The center is radius units left of the turtle.

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

So wherever the turtle is when you start to draw a circle, the center of that circle is some distance to the right. After each circle, just move left or right some number of pixels and draw another circle whose radius is adjusted for the distance the turtle moved. For example, if you draw a circle with a radius of 50 pixels, then move right 10 pixels, you would draw another circle with a radius of 40, and the two circles should be concentric.

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

回答by Nidhi Shah

So now i am giving you the exact code that can draw concentric circles.

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

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()

回答by cdlane

I am not at this point interested in an efficient way of producing concentric circles: I want to see what I have to do to get thisway to work

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

To address the OP's question, the change to their original code to make it work is trivial:

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

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

becomes:

变成:

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

The complete code with the above fix and some style changes:

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

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)

enter image description here

在此处输入图片说明