Use Python turtle to make circles wtihout the circle function

Quinn picture Quinn · Oct 27, 2014 · Viewed 11.6k times · Source

I have this assignment for school:

Build a Snowman without turtle circle function

The snowman should be on a blue background, and should be drawn filled with white.

The outline of the snowman should be in black.

The snowman’s body should be made of 3 filled circles.

The outline of each circle should be 3 pixels wide.

The bottom circle should have a radius of 100 pixels.

The middle circle should have a radius of 70 pixels.

The top circle should have a radius of 40 pixels.

Each circle should be centered above the one below it (except the bottom circle, which can be located anywhere).

There should be no gap between the circles.

Give the snowman a mouth, eyes, and a nose (a hat is optional).

Make sure to include two stick-arms and at least two fingers on each hand.

So far I created this, but I can't seem to get the circles right before I move on. Also, don't know how to color in circles or make dots for eyes. Help me please, first time coding.

import turtle                               # allows us to use turtle library
wn = turtle.Screen()                        # allows us to create a graphics window
wn.bgcolor("blue")                          # sets gtaphics windows background color to blue
import math                                 # allows us to use math functions
quinn = turtle.Turtle()                     # sets up turtle quinn
quinn.setpos(0,0)
quinn.pensize(3)
quinn.up()

# drawing first circle middle
quinn.forward(70)
quinn.down()
quinn.left(90)

# calculation of cicumference of a circle
a = (math.pi*140.00/360)

#itineration for first circle
for i in range (1,361,1):
    quinn.left(a)
    quinn.forward (1)

# drawing second circle bottom
quinn.up()
quinn.home()
quinn.right(90)
quinn.forward(70)
quinn.left(90)
quinn.down()

b = (math.pi*200.00/360)

for i in range (1,361,1):
    quinn.right(b)
    quinn.forward(1)

# drawing third circle head top

quinn.up ()
quinn.goto(0,70)
quinn.right(90)
quinn.down()

c =(math.pi*80/360)

for i in range (1,361,1):
    quinn.left(c)
    quinn.forward(1)

wn.exitonclick()

Answer

gmas80 picture gmas80 · Oct 27, 2014

The following is an example function to draw a circle filled in blue:

def draw_circle(radius):    
    turtle.up()
    turtle.goto(0,radius) # go to (0, radius)
    turtle.begin_fill() # start fill
    turtle.down() # pen down
    turtle.color('blue')
    times_y_crossed = 0
    x_sign = 1.0
    while times_y_crossed <= 1:
        turtle.forward(2*math.pi*radius/360.0) # move by 1/360
        turtle.right(1.0)
        x_sign_new = math.copysign(1, turtle.xcor())        
        if(x_sign_new != x_sign):
            times_y_crossed += 1
        x_sign = x_sign_new
    turtle.up() # pen up
    turtle.end_fill() # end fill.
    return

Then you can modify the above function adding parameters for position (x,y) of the circle center:

def draw_circle(radius, x, y):    
    turtle.up()
    turtle.goto(x,y+radius) # go to (x, y + radius)
    turtle.begin_fill() # start fill
    turtle.down() # pen down
    turtle.color('blue')
    times_y_crossed = 0
    x_sign = 1.0
    while times_y_crossed <= 1:
        turtle.forward(2*math.pi*radius/360.0) # move by 1/360
        turtle.right(1.0)
        x_sign_new = math.copysign(1, turtle.xcor())        
        if(x_sign_new != x_sign):
            times_y_crossed += 1
        x_sign = x_sign_new
    turtle.up() # pen up
    turtle.end_fill() # end fill.
    return

You can easily add dots as, for instance,:

turtle.goto(-20,10)
turtle.color('red')
turtle.dot(20)
turtle.goto(40,10)
turtle.dot(20)

Putting together:

import turtle
import math

def draw_circle(radius, x, y):    
    turtle.up()
    turtle.goto(x,y+radius) # go to (0, radius)
    turtle.begin_fill() # start fill
    turtle.down() # pen down
    turtle.color('blue')
    times_y_crossed = 0
    x_sign = 1.0
    while times_y_crossed <= 1:
        turtle.forward(2*math.pi*radius/360.0) # move by 1/360
        turtle.right(1.0)
        x_sign_new = math.copysign(1, turtle.xcor())        
        if(x_sign_new != x_sign):
            times_y_crossed += 1
        x_sign = x_sign_new
    turtle.up() # pen up
    turtle.end_fill() # end fill.
    return


draw_circle(100, 10, 10)
turtle.goto(-20,10)
turtle.color('red')
turtle.dot(20)
turtle.goto(40,10)
turtle.dot(20)
turtle.pen(shown=False)
turtle.done()

You should attempt to do by yourself the remaining part of the assignment.. ;)