Due Date
This lab is due at the start of class, 12 Sep.
Submissions
Save your python file to a name in the following format: <alpha>_<lastname>_Lab3.py
e.g. 179999_Johnson_Lab3.py
Email the file to your instructor (blenk@usna.edu) with "SI460 Lab3" as the subject line.
The purposes of this lab are
- an introduction to programming graphics transforms, and
- to become familiar with Python's class and functions definitions.
The source code shown at the bottom of the page includes two Python classes and some test code. The classes create a 2D vector and a 2x2 matrix. The classes contain methods to rotate and scale the vector.
There are additional methods for creating an Identity matrix, calculating the Determinant, returning the transpose of a matrix, and checking whether a matrix is orthogonal.
Normally, this file would be imported by another program in order to use the classes and functions. The '__main__' method at the bottom of the script only gets executed if the file is run as a program. It includes test code to make sure that the matrix computations are correct.
Begin the lab by copying the code example below into your lab file and making sure that they run. You must use Python v3.3 for this Lab.
You must extend this program in the following ways:
- Add class Matrix3x3
Must include the same methods as Matrix2x2:
- __init__() {Initializes the object at creation-time}
- __str__() {Returns a string value for print(Matrix3x3)}
- det() {Returns the Determinant of the matrix}
- transpose() {Returns the Transpose of the matrix}
- isOrthogonal() {Returns True or False indicating whether the matrix is orthogonal}
- Add class Vector3d
Must include the same methods as Vector2:
- __init__() {Initializes the object at creation-time}
- __str__() {Returns a string value for print(Vector3d)}
- __mul__() {Returns a new Vector3d that is transformed by a matrix}
- Add function getIdentity3x3()
Must return a Matrix3x3 object that is initialized as the Identity matrix.
See getIdentity2x2() for an example.
- Add new 3D test cases to the '__main__' block at the bottom of
the file.
Include at least one test for each function that you added. See the existing test code for examples.
Grading Criteria
| Max: | Earned: | |
|---|---|---|
| File is named correctly (e.g. 179999_Smith_Lab3.py) | 5 | |
| Classes & functions named correctly | 5 | |
Python script
| 30 | |
Vector3d
| 25 | |
Matrix3x3
| 25 | |
getIdentity3x3()
| 10 | |
| Total: | 100 |
Collaboration Restrictions:
- CP-6 (See course policy for specifics)
- The entire class can be treated as your "assigned group members". You may ask any student for help or advice on this Lab, but you may not work on the same model. Each student must hand in their own, unique program.
- Only the student submitting the Lab may directly affect the program in any way. (e.g. Another student can tell you how to create a function definition, but cannot do it for you.)
- The use of existing Python libraries, other than the ones included on this page, is strictly forbidden. You may not copy Python code, in whole or in part, from the Internet or any other source.
- You must use the Python code sample below to get started.
Submitting your Lab:
Save your Python file (*.py) with a filename as described above. Email the file to your instructor.
Sample code:
Copy and paste this into you Lab file to get started:
# Compares two values to see if they are equal within an acceptable error
def equals(a, b):
DELTA = .001
if(abs(a-b)<DELTA):
return True
return False
# Returns True if a^2 + b^2 == 1
def isOrthonormalVector2(a, b):
return equals(a*a+b*b, 1)
class Matrix2x2:
def __init__(self, a,b,c,d):
self.val = [a,b,c,d]
def __str__(self):
s = "|%7.3f, %7.3f|\n" % (self.val[0], self.val[1])
s += "|%7.3f, %7.3f|" % (self.val[2], self.val[3])
return s
def det(self):
return self.val[0]*self.val[3] - self.val[1]*self.val[2]
def transpose(self):
m = Matrix2x2(self.val[0], self.val[2],
self.val[1], self.val[3])
return m
def isOrthogonal(self):
# (1) Check determinant == -1 or 1
if(equals(abs(self.det()), 1)):
# (2) Check for orthonormal vectors in matrix rows
if(isOrthonormalVector2(self.val[0], self.val[1])):
if(isOrthonormalVector2(self.val[2], self.val[3])):
return True
return False
def getIdentity2x2():
return Matrix2x2(1,0,0,1)
class Vector2d:
def __init__(self, _x, _y):
self.x = _x
self.y = _y
def __str__(self):
return ("(%7.3f, %7.3f)" % (self.x, self.y))
# m must be a Matrix2x2 object
def __mul__(self, m):
v = Vector2d(0,0)
v.x = self.x*m.val[0] + self.y*m.val[1]
v.y = self.x*m.val[2] + self.y*m.val[3]
return v
def printBreak():
print ("-------------------")
# The '__main__' block only runs if the script runs as a program.
# It is ignored if the script is imported as a library.
if (__name__=="__main__"):
v = Vector2d(3,4)
print("Vector v: ")
print (v)
mIdentity = getIdentity2x2()
printBreak()
print ("Identity matrix:")
print (mIdentity)
printBreak()
print ("v transformed by Identity:")
print (v*mIdentity)
printBreak()
print ("Determinant of Identity:")
print (mIdentity.det())
mScale = Matrix2x2(2,0,
0,3)
printBreak()
print ("mScale matrix:")
print (mScale)
printBreak()
print ("v transformed by mScale:")
print (v*mScale)
printBreak()
print ("mScale Determinant:")
print (mScale.det())
printBreak()
print ("Is mScale Orthogonal?")
print (mScale.isOrthogonal())
mRotate = Matrix2x2(.866, -.5,
.5, .866)
printBreak()
print ("mRotate matrix")
print (mRotate)
printBreak()
print ("v transformed by mRotate:")
print (v*mRotate)
printBreak()
print ("mRotate Determinant:")
print (mRotate.det())
printBreak()
print ("Transpose of mRotate:")
print (mRotate.transpose())
printBreak()
print ("Is mRotate Orthogonal?")
print (mRotate.isOrthogonal())