Skip to content

Instantly share code, notes, and snippets.

@minte9
Last active July 19, 2021 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minte9/ff4cfc6a032e398f265c071a65bccb57 to your computer and use it in GitHub Desktop.
Save minte9/ff4cfc6a032e398f265c071a65bccb57 to your computer and use it in GitHub Desktop.
"""
Write a function called circle that takes a turtle, t, and radius, r,
as parameters and that draws an approximate circle
by calling polygon with an appropriate length and number of sides.
Test your function with a range of values of r.
Hint: figure out the circumference of the circle
and make sure that length * n = circumference.
import turtle
def poligon(t, length, n):
for i in range(n):
t.fd(length)
t.lt(360/n)
bob = turtle.Turtle()
poligon(bob, 20, 10)
turtle.mainloop() """
# SOLUTION
import turtle
import math
def poligon(t, length, n):
for i in range(n):
t.fd(length)
t.lt(360/n)
def circle (t, r):
circumference = 2 * math.pi * r
n = 360 # slow
n = 36 # fast
length = circumference/ n
poligon(t, length, n)
bob = turtle.Turtle()
circle(bob, 50)
circle(bob, 100)
turtle.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment