-
Notifications
You must be signed in to change notification settings - Fork 3
/
TurtlePractice.py
59 lines (45 loc) · 1.21 KB
/
TurtlePractice.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
# File: Figures.py
# Description: Draws various types of geometric figures
import turtle, math
# draw a line from (x1, y1) to (x2, y2)
def drawLine (ttl, x1, y1, x2, y2):
ttl.penup()
ttl.goto (x1, y1)
ttl.pendown()
ttl.goto (x2, y2)
ttl.penup()
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)
def main():
# put label on top of page
turtle.title ('Geometric 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)
# draw a line
ttl.color ('gold4')
drawLine (ttl, -200, -10, 325, -10)
drawLine (ttl, -200, -15, 325, -15)
# persist drawing
turtle.done()
main()