1. SimpleGrapher01.py
– Graphs any function on domain [-10.0, 10.0]
#
# Name:
# File: SimpleGrapher01.py
# Date: February 20, 2008
#
# Desc: Graphs any
function on interval [-10.0, 10.0]
#
from graphics import *
# function
to be graphed
def func(x):
return 0.5*x**2 - 10.0
def main():
#
parameters for graphics window
windowTitle = "Simple Grapher"
screenWidth = 401
screenHeight = 401
#
# compute
range of function to be graphed
# range
computed as [minY, maxY]
#
x =
-10.0
maxY = func(x)
minY = func(x)
while (x <= 10.0):
x = x + 0.5
f = func(x)
if (f > maxY):
maxY = f
if (f < minY):
minY = f
#
# Open a
graphics window with
# LL
coordinates (-10.0, minY-1) and
# UR
coordinates (+10,0, maxY+1)
#
win
= GraphWin(windowTitle, screenWidth, screenHeight)
win.setCoords(-10.0,
minY-1, 10.0, maxY+1)
#
draw axes
Line(Point(-10.0, 0.0), Point(10.0, 0.0)).draw(win)
Line(Point(0.0, minY), Point(0.0, maxY)).draw(win)
#
graph function using points
#
x = -10
#
while (x <= 10.0):
# Point(x, func(x)).draw(win)
# x = x + 0.05
# graph
function using polyline
x =
-10.0
p1
= Point(x, func(x))
while (x <= 10.0):
x = x + 0.5
p2 = Point(x, func(x))
Line(p1, p2).draw(win)
p1 = Point(x, func(x))
# pause – then close window on <Enter>
raw_input("Hit
<Enter> to Quit: ")
win.close()
main()
2. Lab07a.py – Calculating n choose k
using a factorial function
# Name:
# File: Lab07a.py
# Date: February 21,
2008
#
# Desc: Calculating n choose k using a function call for n!
#
def
nFact(n):
#
# computes n factorial
#
factorial = 1
for i in range(1, n+1):
factorial = factorial * i
return factorial
def
main():
# Program Title
print
print "Calculating n choose k"
print
# Get input from user
n, k = input("Enter
n and k separated by a comma: ")
# compute C(n,k) = n!/(k! * (n-k)!)
c = nFact(n)/(nFact(k)*nFact(n-k))
# Display results
print
print n, "choose", k,
"=", c
print
main()
3. Lab07b.py – A Simple Sort using a function
that returns multiple values
#
# Name:
# File: Lab07b.py
# Date: February 21, 2008
#
# Desc: Sorts three
numbers in ascending order
#
def
order(a, b):
#
# returns a, b where a < b
#
if (a < b):
return a, b
else:
return b, a
def
main():
# display program title
print
print "Simple Sort"
# use an ask-before iterating loop
again = 'y'
while (again == 'y'):
print
# get 3
numbers
a, b, c = input("Enter
three numbers separated by commas: ")
# order them from low to high
a, b = order(a,b)
b, c = order(b,c)
a, b = order(a,b)
# display sorted list
print
print "The sorted list is: ", a, b, c
print
again = raw_input("Again? (y/n):
")
main()
4. Lab07c.py – A Simple Sort using a “void”-type
function
#
# Name:
# File: Lab07c.py
# Date: February 21, 2008
#
# Desc: Sorts three
numbers
#
def
order(list, i, j):
#
# void function which exchanges ith and jth components of list
# if they are out of order
#
if (list[i] >
list[j]):
list[i],
list[j] = list[j], list[i]
def
main():
# display title
print
print "Simple Sort v.2"
# ask-before iterating loop
again = 'y'
while (again == 'y'):
print
# read values into list
list = [0,0,0]
list[0],
list[1], list[2] = input("Enter three numbers separated by commas: ")
# sort list
order(list, 0,
1)
order(list, 0,
2)
order(list, 1,
2)
# display ordered list
print
print "The sorted list is: ", list[0], list[1], list[2]
print
again = raw_input("Again? (y/n):
")
main()
5. Lab07d.py – A modification of the Triangle
program
#
# Name:
# File: Lab07d.py
# Date: February 14,
2008
#
# Desc: Mouse Click Triangle Program with Area Calculation
#
from
graphics import *
import
math
def
length(p1, p2):
#
# Returns length of line segment defined
by two points
#
x1, y1 = p1.getX(),
p1.getY()
x2, y2 = p2.getX(),
p2.getY()
return math.sqrt((x1 - x2)**2 + (y1
- y2)**2)
def
areaOfTriangle(p1, p2,p3):
#
# Returns area of triangle defined by
three points (vertices)
# using Heron's formula
#
a = length(p1, p2)
b = length(p2, p3)
c = length(p1, p3)
s = (a + b + c)/2.0
return math.sqrt(s*(s-a)*(s-b)*(s-c))
def
main():
windowTitle = "Click
Me!"
screenWidth = 401
screenHeight =
401
#
# Open a graphics window object
#
win = GraphWin(windowTitle, screenWidth,
screenHeight)
win.setCoords(0.0, 0.0, 10.0, 10.0)
# draw grid
for i in range(11):
Line(Point(0,i),Point(10,i)).draw(win)
Line(Point(i,0),Point(i, 10)).draw(win)
# using mouse clicks get three points
message = Text(Point(5,
0.5), "Click on three points")
message.draw(win)
p1 = win.getMouse()
p1.draw(win)
p2 = win.getMouse()
p2.draw(win)
p3 = win.getMouse()
p3.draw(win)
# Use Polygon object to draw triangle
triangle = Polygon(p1,
p2, p3)
triangle.setFill("peachpuff")
triangle.setOutline("cyan")
triangle.draw(win)
# compute and display area of triangle
area = areaOfTriangle(p1, p2, p3)
message.setText("Area = %0.2f" % area)
# wait for another click to end
win.getMouse();
win.close()
main()
6. MousePolyLine.py – clicking the mouse
draws a polyline
#
# Name:
# File: MousePolyline.py
# Date: February 21,
2008
#
# Desc: A mouse polyline program
#
from
graphics import *
def
inside(p1, p2, p3):
#
# Returns True if point p2 is between
points p1 and p3
#
return (p1.getX()
<= p2.getX()) and (p2.getX() <= p3.getX()) and \
(p1.getY()
<= p2.getY()) and (p2.getY() <= p3.getY())
def
drawButton(win,
LL, UR, str):
#
# Draws a button between LL and UR points with
str as text
#
Rectangle(LL,
UR).draw(win)
midX = (LL.getX()
+ UR.getX())/2
midY = (LL.getY()
+ UR.getY())/2
Text(Point(midX, midY), str).draw(win)
def
main():
windowTitle = "Mouse Polyline"
screenWidth = 401
screenHeight =
401
#
# Open a graphics window object and
re-orients so
# y axis runs bottom to top
#
win = GraphWin(windowTitle, screenWidth,
screenHeight)
win.setCoords(0, 0, screenWidth, screenHeight)
win.setBackground("white")
# create a true button
LL =
Point(350, 10) #
define Lower Left
UR = Point(390,
25) # and
Upper Right coordinates of button
drawButton(win, LL, UR, "Quit")
# await first mouse click
p1 = win.getMouse()
done = False
while (not done):
p2 = win.getMouse()
if inside(LL,
p2, UR): #
check if button clicked
done = True
else:
Line(p1,
p2).draw(win) #
otherwise draw line
p1 = p2
raw_input("Hit <Enter> to quit ")
win.close()
main()