Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 454
thelma Avatar asked Jul 08 '14 15:07

thelma


Video Answer


2 Answers

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.

like image 199
Noelkd Avatar answered Sep 22 '22 04:09

Noelkd


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.

like image 33
chepner Avatar answered Sep 24 '22 04:09

chepner