How to draw Circle using Turtle in Python 3

16,793

Solution 1

If you really need to define a polygon.

from turtle import *
import math

apple = Turtle()

def polygon(t, n, length):
    for i in range(n):
        left(360/n)
        forward(length)

def draw_circle(t, r):
    circumference = 2 * math.pi * r
    n = 50
    length = circumference / n
    polygon(t, n, length)
    exitonclick()

draw_circle(apple, 30)

Solution 2

use the circle method

import turtle
import math

apple=turtle.Turtle()

def draw_circle(t, r):
    turtle.circle(r)

draw_circle(apple, 15)

turtle.exitonclick()

Solution 3

here is a function for polygon:

def drawPolygon (ttl, x, y, num_side, radius):
  sideLen = 2 * radius * math.sin (math.pi / num_side)
  angle = 360 / num_side
  ttl.penup()
  ttl.goto (x, y)
  ttl.pendown()
  for iter in range (num_side):
    ttl.forward (sideLen)
    ttl.left (angle)

Here is how you use it:

def main():
  # put label on top of page
  turtle.title ('Figures')

  # setup screen size
  turtle.setup (800, 800, 0, 0)

  # create a turtle object
  ttl = turtle.Turtle()

  # draw equilateral triangle
  ttl.color ('blue')
  drawPolygon (ttl, -200, 0, 3, 50)

  # draw square
  ttl.color ('red')
  drawPolygon (ttl, -50, 0, 4, 50)

  # draw pentagon
  ttl.color ('forest green')
  drawPolygon (ttl, 100, 0, 5, 50)

  # draw octagon
  ttl.color ('DarkOrchid4')
  drawPolygon (ttl, 250, 0, 8, 50)

  # persist drawing
  turtle.done()

main()

Dont Forget to add import turtle, math

Share:
16,793
Alyssa Kelley
Author by

Alyssa Kelley

Updated on December 03, 2022

Comments

  • Alyssa Kelley
    Alyssa Kelley over 1 year

    This is the code that I have already, but it is saying I need to define 'polygon' which I know I need to, but not exactly sure how and different ways that I have been trying just keeps giving me errors.

    import turtle
    import math
    
    apple=turtle.Turtle()
    
    def draw_circle(t, r):
        circumference = 2 * math.pi * r
        n = 50
        length = circumference / n
        polygon(t, n, length)
    
    draw_circle(apple, 15)
    
    turtle.exitonclick()
    
    • AAM111
      AAM111 over 6 years
      Shouldn’t polygon be turtle.polygon?
    • Alyssa Kelley
      Alyssa Kelley over 6 years
      Unfortunately I tried that too and I got the error "AttributeError: module 'turtle' has no attribute 'polygon'"
    • combinatorist
      combinatorist over 6 years
      It seems like you're working off a tutorial. Could you include the link?
    • combinatorist
      combinatorist over 6 years
      When you say you need to "define polygon" are you expecting to import someone else's function to create polygons or trying to write your own from scratch?
    • Alyssa Kelley
      Alyssa Kelley over 6 years
      I'm not working off of any tutorial like showing the steps if that is what you mean. I am in a CS 122 college course and so I am just guessing my way through this haha but I am using Anaconda to write the code and using Jupyter Notebook and it is showing me my errors, if that is what you were asking as well.
    • Alyssa Kelley
      Alyssa Kelley over 6 years
      I just thought I had to include that polygon in order to incorporate my t, n, and length all together, but if that, then I can take it out. I ran uphill's version (comment below) and it ran just fine, but I just wanted to add more customization to it if possible.
  • Alyssa Kelley
    Alyssa Kelley over 6 years
    Thanks! A bit more complex than my level right now, I am veeeeery intro level right now haha
  • BlooB
    BlooB over 6 years
    No problem :-). If you need help with any part let me know.
  • AAM111
    AAM111 over 6 years
    What is the parameter t used for?
  • uphill
    uphill over 6 years
    Yeah true, you don't need it