Skip to content

Instantly share code, notes, and snippets.

@m-ammar
Created April 2, 2018 19:42
Show Gist options
  • Save m-ammar/4117629ebb4e7fa200ec5d91e808afa2 to your computer and use it in GitHub Desktop.
Save m-ammar/4117629ebb4e7fa200ec5d91e808afa2 to your computer and use it in GitHub Desktop.
Exercise 4-3. Write an appropriately general set of functions that can draw shapes as in https://www.safaribooksonline.com/library/view/think-python-2nd/9781491939406/assets/tnkp_0402.png
from __future__ import print_function, division
import math
import turtle
def draw_pie(t, n, r):
draw_poly_pie(t, n, r)
t.pu()
t.fd(r*2 + 10)
t.pd()
def draw_poly_pie(t, n, r):
interior_angle = 360/n
for i in range(n):
draw_triangle(t, r, interior_angle/2)
t.lt(180 + interior_angle/2)
def draw_triangle(t, r, angle):
# Base of the isoscelese triangle
y = 2 * r * math.sin(angle * math.pi / 180)
t.rt(angle)
t.fd(r)
t.lt(90 + angle)
t.fd(y)
t.lt(90 + angle)
t.fd(r)
bob = turtle.Turtle()
bob.pu()
bob.bk(130)
bob.pd()
# draw polypies with various number of sides
size = 40
draw_pie(bob, 5, size)
draw_pie(bob, 6, size)
draw_pie(bob, 7, size)
draw_pie(bob, 8, size)
bob.hideturtle()
turtle.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment