Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96166
License: OTHER
1
"""This module contains a code example related to
2
3
Think Python, 2nd Edition
4
by Allen Downey
5
http://thinkpython2.com
6
7
Copyright 2015 Allen Downey
8
9
License: http://creativecommons.org/licenses/by/4.0/
10
"""
11
12
from __future__ import print_function, division
13
14
import turtle
15
16
from Point1 import Point, Rectangle
17
from Circle import Circle
18
19
import polygon
20
21
def draw_circle(t, circle):
22
"""Draws a circle.
23
24
t: Turtle
25
circle: Circle
26
"""
27
t.pu()
28
t.goto(circle.center.x, circle.center.y)
29
t.fd(circle.radius)
30
t.lt(90)
31
t.pd()
32
polygon.circle(t, circle.radius)
33
34
35
def draw_rect(t, rect):
36
"""Draws a rectangle.
37
38
t: Turtle
39
rect: Rectangle
40
"""
41
t.pu()
42
t.goto(rect.corner.x, rect.corner.y)
43
t.setheading(0)
44
t.pd()
45
46
for length in rect.width, rect.height, rect.width, rect.height:
47
t.fd(length)
48
t.rt(90)
49
50
51
if __name__ == '__main__':
52
bob = turtle.Turtle()
53
54
# draw the axes
55
length = 400
56
bob.fd(length)
57
bob.bk(length)
58
bob.lt(90)
59
bob.fd(length)
60
bob.bk(length)
61
62
# draw a rectangle
63
box = Rectangle()
64
box.width = 100.0
65
box.height = 200.0
66
box.corner = Point()
67
box.corner.x = 50.0
68
box.corner.y = 50.0
69
70
draw_rect(bob, box)
71
72
# draw a circle
73
circle = Circle
74
circle.center = Point()
75
circle.center.x = 150.0
76
circle.center.y = 100.0
77
circle.radius = 75.0
78
79
draw_circle(bob, circle)
80
81
# wait for the user to close the window
82
turtle.mainloop()
83
84