-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEX6.44.py
74 lines (58 loc) · 1.32 KB
/
EX6.44.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
66
67
68
69
70
71
72
73
74
# 6.44 (Turtle: plot the square function) Simplify the code for Exercise 5.54 by using the
# functions in Listing 6.14.
import turtle
# Draw a line from (x1, y1) to (x2, y2)
def drawLine(x1, y1, x2, y2):
turtle.penup()
turtle.goto(x1, y1)
turtle.pendown()
turtle.goto(x2, y2)
# Write a text at the specified location (x, y)
def writeText(s, x, y):
turtle.penup() # Pull the pen up
turtle.goto(x, y)
turtle.pendown() # Pull the pen down
turtle.write(s) # Write a string
turtle.speed(0) # Fastest
# Draw a square function
scaleFactor = 0.01
left = -100
right = 100
x = left
turtle.penup()
turtle.goto(x, scaleFactor * x * x)
turtle.pendown()
for x in range(left, right + 1):
turtle.goto(x, scaleFactor * x * x)
# Draw X-axis
drawLine(-160, 0, 160, 0)
# Draw Y-axis
drawLine(0, -80, 0, 80)
# Display X
writeText("Y", 0, 80)
# Display Y
writeText("X", 180, -15)
# Draw arrows
turtle.degrees()
turtle.penup()
turtle.goto(160, 0)
turtle.pendown()
turtle.setheading(150)
turtle.forward(20)
turtle.penup()
turtle.goto(160, 0)
turtle.pendown()
turtle.setheading(-150)
turtle.forward(20)
turtle.penup()
turtle.goto(0, 80)
turtle.pendown()
turtle.setheading(240)
turtle.forward(20)
turtle.penup()
turtle.goto(0, 80)
turtle.pendown()
turtle.setheading(-60)
turtle.forward(20)
turtle.hideturtle()
turtle.done()