-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCh4.3-Ex5.py
63 lines (53 loc) · 1.37 KB
/
Ch4.3-Ex5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#
# ThinkPython Section 4.3, Exercise 5
#
# J.L.Klay
# 05-Apr-2012
#
# 5. Make a more general version of circle called arc that takes an
# additional parameter angle, which determines what fraction of a
# circle to draw. angle is in units of degrees, so when angle=360,
# arc should draw a complete circle.
#
from TurtleWorld import *
import math
# Function to take a turtle and use it to draw a square
def square(t,len):
for i in range(4):
fd(t,len)
lt(t)
# Function to take a turtle and use it to draw a polygon
def polygon(t,len,n):
for i in range(n):
fd(t,len)
angle = 360.0/n
lt(t,angle)
# Function to take a turtle and use it to draw a circle
def circle(t,radius):
circumference = 2.0*math.pi*radius
n = 50 #pick a number
len = circumference/n;
polygon(t,len,n)
# Function to take a turtle and use it to draw an arc
def arc(t,radius,angle):
circumference = 2.0*math.pi*radius
frac = angle/360.0
arclength = circumference*frac
n = 50 # pick a number
len = arclength/n;
turnang = angle/n
for i in range(n):
fd(t,len)
lt(t,turnang)
world = TurtleWorld()
bob = Turtle()
bob.delay = 0.01
ray = Turtle()
radius = 100.
angle = 60.
arc(bob,radius,angle)
length = 100.
square(ray,length)
# Note: reason it doesn't draw over itself exactly is because of the
# extra left turn at the end of the loop *I think*
wait_for_user()