# in_class.py # # demo done in class of a program # that draws the graph of a function # using Zelle's graphics API # # Jim M, Sep 2011 from graphics import * from math import * def main(): # define some parameters windowWidth = 400 # in pixels windowHeight = 300 xMin = 0.0 xMax = 10.0 yMin = -1.5 yMax = 1.5 xBorder = 1.0 # in x,y coordinates yBorder = 0.2 nSteps = 200 function = "sin(x)" # set up the win window = GraphWin("Function Plot", windowWidth, windowHeight) window.setCoords(xMin - xBorder, yMin - yBorder, xMax + xBorder, yMax + yBorder) plotRectangle = Rectangle(Point(xMin,yMin), Point(xMax,yMax)) plotRectangle.setOutline("grey") plotRectangle.draw(window) # draw the function dx = (xMax - xMin)/nSteps x = xMin for i in range(nSteps): # draw a line from x to (x+dx) (x1, y1) = (x, eval(function)) x = x + dx (x2, y2) = (x, eval(function)) Line(Point(x1, y1), Point(x2, y2)).draw(window) # wait for the user to quit user_input = raw_input("Type to quit. ") window.close() main()