Today's lab will focus on the python's mathematics library and the book's graphics library.
Included with the textbook is a simple graphics library. To use it, place the file graphics.py to the directory where you saved your python files. There is a useful cheat sheet of the graphics functions.
Here is the first program (inspired from motionscript.com):
#Idea from: http://www.motionscript.com/mastering-expressions/simulation-basics-1.html from math import * from graphics import * from time import * def goodSine(): veloc = .5 #horizontal velocity (pixels per second) amp = 50 #sine wave amplitude (pixels) freq = .01 #oscillations per second #Set up a graphics window: win = GraphWin("Good Sine Waves",400,200) win.setCoords(0.0, -100.0, 200.0, 100.0) #Draw a line for the x-axis: p1 = Point(0,0) p2 = Point(200,0) xAxis = Line(p1,p2) xAxis.draw(win) #Draw a ball that follows a sine wave for time in range(1000): x = time*veloc y = amp*sin(freq*time*2*pi) ball = Circle(Point(x,y),2) ball.draw(win) sleep(0.1) #Needed so that animation runs slowly enough to be seen win.getMouse() win.close() goodSine()Save this file to your USB or MyDocuments folder (along with graphics.py) and run the program. Let's go through what each part does:
from math import * from graphics import * from time import *The import statement asks python to include the functions from a given module, or collection of functions. These three import statements ask for the functions from the math, graphics, and time modules. The math and time modules are built-in to python; the graphics module was written by Zelle, the textbook's author.
veloc = .5 #horizontal velocity (pixels per second) amp = 50 #sine wave amplitude (pixels) freq = .01 #oscillations per secondThese three variables control the speed, height, and bounce of the ball. Try changing them and see what happens to the animation. Your goal is to have the ball touch the ceiling every time.
#Set up a graphics window: win = GraphWin("Good Sine Waves",400,200) win.setCoords(0.0, -100.0, 200.0, 100.0)These lines set up the graphics window. This window will have the title, "Good Sine Waves" and width 400 pixels and height 200 pixel. A pixel is just one tiny light bulb in your computer's screen. So, 400 pixels is 400 tiny lights in the screen. You can assume that computers have screens that are at least 800 by 600 pixels. In the graphics module, there is a function for changing the coordinates of a graphics window. We did so here to make the math calculating the height of the bouncing ball easier.
#Draw a line for the x-axis: p1 = Point(0,0) p2 = Point(200,0) xAxis = Line(p1,p2) xAxis.draw(win)The graphics module has several standard shapes (see list of graphics objects). We created two points, called p1 and p2, that will be the endpoints of a line called xAxis. For our line to be seen, we need to draw it to our window (which we called win). Since it is possible to have multiple windows in a given program, we need to include the name of our window as the input to the draw function.
#Draw a ball that follows a sine wave for time in range(1000): x = time*veloc y = amp*sin(freq*time*2*pi) ball = Circle(Point(x,y),2) ball.draw(win) sleep(0.1) #Needed so that animation runs slowly enough to be seenThis loop repeats the indented statements 1000 times. First time through the variable time is 0; the next time through it is 1; and it keeps going until time is 999. Each repetition of the loop sets the variable x to a multiple of time and sets the variable y to the sine of a multiple of time. It then makes a circle centered at (x,y) and draws that circle (called ball) to the window. Lastly, we have a call to the sleep function so that the animation runs slowly enough so we can see it.
win.getMouse() win.close()We add these lines at the end of our graphics programs to keep the window open. The first command is waiting for a mouse click. After the mouse is clicked in the window, the next statement is executed, win.close() which closes the window.
goodSine()This is a call (or invocation) of the function goodSine() that we just wrote. It is not necessary to have in your file, but if you do, you do not have to type goodSine() in the Python shell to run your function. Instead, it will be called when you use the F5 key.
To make the ball appear to bounce, we need for it to stay above the line representing the x-axis. Another way of saying that is its y-coordinate should always be positive. A simple way to do that is to take the absolute value of the expression for the y variable:
y = abs(amp*sin(freq*time*2*pi))Try running your program to see the bouncing ball!
If you finish early, you may work on the programming problems.