Homework 3: Transformation Matrices

* Due: Wednesday, Oct. 7, 11:59pm on Moodle *

The goal of this homework is to practice using transformation matrices, both "on paper" and through code.

** Part I: Working with Transformations ** (10 pts)

Answer the following Yes/No questions. If Yes, provide a brief justification (doesn't need to be a proof, but some kind of picture, matrix calculation, or explanation). If No, provide a counterexample. You can multiply the matrtrices by hand or using software like Matlab or Mathematica.

- Do 2D translations commute, i.e., is it always true that T
_{1}T_{2}= T_{2}T_{1}, where T_{1}and T_{2}are translation transformations? - Do 2D rotations commute, i.e., is it always true that R
_{1}R_{2}= R_{2}R_{1}, where R_{1}and R_{2}are rotation transformations? - Do 2D translations commute with rotations, i.e., is it always true that TR = RT, where T is a translation transformation and R is a rotation transformation?
- Do 2D translations commute with scalings, i.e., is it always true that TS = ST, where T is a translation transformation and S is a scaling transformation?

Credit: Joe O'Rourke

** Part II: Implementing Transformations ** (10 pts)

For this part, you will build on Homework 2 and implement the transformations rotate, translate, and scale for your RegularPolygon class. First copy your Homework 2 into a new file and name it hw3.py.

- Add a method (before the Point, Line, and RegularPolygon classes) to multiply matrices:
def matrix_multiply(m1, m2):

where m1 and m2 are 2D arrays. For example:matrix_multiply([[2,4,3],[1,-5,0]], [[3,2],[-1,2],[4,3]])

should return[[14, 21], [8, -8]].

- Download hw3_poly_methods.py and copy over these methods into your RegularPolyon class (so they should all be indented). We're going to use these methods to make it easier to transform polygons. The first method "draw_from_vertices" will use a new attribute ("self.vertices") to draw the polygon, instead of a list of all the points. The other draw method made it easier to implement "fill", but this method will make it easier to implement transformations.
First, in the RegularPolygon constructor, add the line

self.vertices = []

and then append each of the vertices to this list (you've probably already defined them in a for loop). - SCALE: Implement the "scale" method:
def scale(self, ax, ay):

where "ax" is the scaling for the x-coordinate and "ay" is the scaling for the y-coordinate. For each of these transformation matrices, first define the matrix in a 2D array. Second, create a matrix of the polygon vertices ("create_matrix"). Third, mutliply the transformation matrix and the matrix of vertices. Finally, use "update_vertices" with the resulting matrix. - ROTATE: Implement the "rotate" method.
def rotate(self, theta):

where "theta" is the angle of rotation (counterclockwise). - TRANSLATE: Implement the "translate" method.
def translate(self, tx, ty):

where the x-coordinate is translated by "tx" and the y-coordinate is translated by "ty". - Demonstrate these three transformation methods in a creative image. Can you create the illusion that an object is moving (i.e. animated)?

The second method "create_matrix" will create a 2D array (matrix) of the vertices of the polygon, so they can be easily passed in to your "matrix_multiply" method. The third method "update_vertices" will take in a 2D array (matrix) and update (overwrite) the polyons vertices.

For this assignment, you are welcome to work together, but everyone should turn in their own math/code that they have written and understood. Please cite anyone you worked with at the top of your assignment.

**EXTRA CREDIT OPTIONS:**

- Implement reflection for your RegularPolygon class. (+1)
- We now have two ways of drawing polygons, using a list of all their points (which was helpful for "fill"), or using a list of their vertices (which was helpful for transformations). Can you update your code so that it's possible to use "fill" in conjunction with transformations? Refactor the RegularPolygon code to make it easy to move the shape, fill it, move it again, fill it, etc. (+2)