简体   繁体   中英

Python Turtle: Draw concentric circles using circle() method

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 this way to work:

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)

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:

在此输入图像描述

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

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.

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 this way to work

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

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)

在此输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM