-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercise41.py
67 lines (48 loc) · 1.67 KB
/
Exercise41.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
64
65
#Exercise 4.1 - Building building blocks
from TurtleWorld import *
from math import *
def polygon(n,t,length):
"""Uses a turtle (t) to draw a regular n-sided polygon of
given segment length (length)
"""
for i in range(n):
fd(t, length)
lt(t,360/n) #turns to the left at angle 360/n degrees
def circle(t,r):
"""Uses a turtle (t) to draw a circle of radius r
rendered using an approximation of an n-sided regular polygon
where n is 'very large'
"""
n=100
circumference = 2*pi*r
length = circumference/n #Must fit the right amount of equivalent lengths on the "circle"
polygon(n,t,length)
def myArc(t,r,angle): # My solution to constructing an arc. Assuming book's
# solution is better, so will be using that.
n=100
circumference = 2*pi*r
length=circumference/n
for i in range(n*angle/360): #Say if I want it to go 60 degrees, n must be
fd(t,length) #affected by the ratio angle/360)
lt(t,360.0/n)
def arc(t,r,angle): #Book's solution
"""Uses a turtle (t) to draw an arc of would-be radius r. Angle is
specified such that an input of 360 would produce a circle.
"""
arc_length=r*angle*(2*pi/360) #Converts input angle in degrees to radians to calculate arc length
n = int(arc_length/3)+1
step_length = arc_length/n
step_angle = float(angle)/n
for i in range(n):
fd(t,step_length)
lt(t,step_angle)
def tripoly(t,n,length):
for i in range(n):
polygon(3,t,length)
fd(t,length)
lt(t,360.0/n)
world = TurtleWorld()
bob = Turtle()
bob.delay = 0.01
myArc(bob,50,176)
wait_for_user()