### CSC 240: Computer Graphics

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.

1. Do 2D translations commute, i.e., is it always true that T1T2 = T2T1, where T1 and T2 are translation transformations?
2. Do 2D rotations commute, i.e., is it always true that R1R2 = R2R1, where R1 and R2 are rotation transformations?
3. 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?
4. 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?
You can turn this part in on paper to Ford 355 (under my door if I'm not there), or as a document on Moodle. If you're interested in learning LaTeX, this might be a good opportunity to practice - you can start from the transformations.tex document on Piazza.

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.

1. 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]]. `
2. 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).
3. 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.

4. 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.
5. ROTATE: Implement the "rotate" method.
` def rotate(self, theta): `
where "theta" is the angle of rotation (counterclockwise).
6. 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".
7. Demonstrate these three transformation methods in a creative image. Can you create the illusion that an object is moving (i.e. animated)?
COLLABORATION:

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)