forked from swaaz/basicprograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlot_function
More file actions
69 lines (60 loc) · 1.5 KB
/
Copy pathPlot_function
File metadata and controls
69 lines (60 loc) · 1.5 KB
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
import turtle # import the turtle module to draw the graph with
# set up the screen
wn = turtle.Screen()
# set up the turtle
graph = turtle.Turtle()
graph.hideturtle() # hide the turtle so the graph looks nice
graph.speed(0) # speed up the turtle
# draw the axes
# x axis
graph.forward(250)
graph.penup()
graph.goto(250,5)
graph.write('x',font=('Cambria',12,'italic'))
graph.goto(0,0)
graph.pendown()
graph.backward(250)
graph.forward(250)
# y axis
graph.left(90)
graph.forward(250)
graph.penup()
graph.goto(5,250)
graph.write('y',font=('Cambria',12,'italic'))
graph.goto(0,0)
graph.pendown()
graph.backward(250)
graph.forward(250)
def f(x):
'''f(x) -> float
generic function to be graphed'''
return x**2
def decrease_to_fit(zoom):
'''decrease_to_fit(zoom) -> tup
decreases x to fit the graph'''
minX = -250/zoom
maxX = 250/zoom
# fit for negative x
while abs(f(minX)) > 250/zoom:
minX += 0.5/zoom
# fit for positive x
while f(maxX) > 250/zoom:
maxX -= 0.5/zoom
# return tuple of min and max
return (minX,maxX)
def graph_function(t,zoom=10):
'''graph_function(t,zoom) -> none
draws the graph of a function using turtle t
and is zoomed in to zoom'''
t.pensize(2)
# go to the start without drawing a ling
x,maxX = decrease_to_fit(zoom)
t.penup()
t.goto(x*zoom,f(x)*zoom)
t.pendown()
# draw the graph
while x <= maxX:
x += 0.01
t.goto(x*zoom,f(x)*zoom)
graph_function(graph,100)
wn.mainloop()