PROJECT 4: Some fun with computer graphics

MAS3114, Spring 1999


For this project you may work individually or in pairs (no groups larger than two). Two students working together need only turn in one printout with both names on it. You should not share answers with anybody other than your "teammate". Make sure you are familiar with the student honor code.

  • Handing in the project.

    Once again you should staple the pages of your output together, and put your name(s) at the top of the first page. Follow all instructions, answer all questions. Edit your output, either on the computer or by hand, so that a reader can easily tell what problem you are working on or what question you are answering. Display your answers in such a way that they are easily distinguished from the rest of the output (e.g. by putting boxes around your final answers). Label which plots go with which problems.

    Important reminder! As soon as you enter MATLAB, remember to start saving your session, either by using the diary command as in Project 1, or by whatever other means you found works for you.

  • Getting started

    In this project you are going to experiment with some computer graphics using the material in section 2.8. Before starting this project, you should re-read the beginning of Section 2.8 through the bottom of p. 156. Also re-read Example 3 on pp. 77-78.

  • Problem 1

    Write a MATLAB program to produce the matrix of the linear transformation from R2 to R2 that rotates vectors counterclockwise through a given angle. (The formula for the matrix is given in Example 3 on pp. 77-78.) This matrix is a function of the angle (measured in radians), so your program will compute a function of one variable that outputs a 2 x 2 matrix. MATLAB knows the functions "sin" and "cos", and the number "pi" (lower-case).

    Once you've stored your program in a .m-file, you can test it by seeing if it gives the correct matrices for some angles whose sine and cosine you know. For example, if your .m-file is "rotate.m", typing "rotate(pi/3)" should give you a 2x2 matrix whose rows are [0.5000, -0.8660] and [.8660, 0.5000]. You will also be able to test it graphically in problems below.

    When you are satisfied that your program is correct, again use the "type" command to include the file contents in your output, but first make sure you begin and end the file with good visual separators.

  • Problem 2

    Write down a 2 x 9 matrix whose columns are the coordinates of the points in Figure 1, but ordered in such a way that you could draw the figure by moving your pencil from point 1 to point 2, then to point 3, then to point 4, etc ., without lifting your pencil from the paper, as if you were drawing a "connect the dots" picture. (For example you could label your points 1 and 2 as in the book, but your third point would be what the book labels as point 5.) Thus the first 8 columns of your matrix will be some permutation of the columns of the matrix D at the bottom of p. 155. Your 9th column should be the same as your first, because if you were drawing the N you'd have to connect point 8 with point 1. Put this matrix in a .m-file (these can be used just to define variables as well as functions). For example, you could call your file "regular_N.m", and in which you would need only the single line
    regular_N=[ ... ]
    
    where "..." stands for the entries of your 2x9 matrix. Then, once you start up MATLAB, any time you type "regular_N" MATLAB will interpret that to be the matrix stored in your .m-file. Make sure you don't name this file with a common variable name like "A.m", because as long as this file exists, MATLAB will think that the variable A stands for the matrix stored in A.m.

    Now start up MATLAB. At the prompt, type "regular_N" (or whatever you named your matrix). You will be using this matrix a lot below, so during this MATLAB session give it a second name, B, by next typing "B=regular_N".

    MATLAB can plot a set of points in the xy plane using the "plot" command. To learn how this works, type "help plot" and read what MATLAB says. Then to plot the N in figure 1, you can do the following:

  • Problem 3

    To do a new plot without erasing your old plot on the computer screen, click on the "file" menu in the plot window and select "new" and then "figure".

    Input the two-by-two matrices A and S defined on p. 156. To see the effect of multiplication by A, plot the second row of A*B against the first row, just as you plotted the second row of B against the first row to view the "regular N". After you enter the plot command, again type

     axis([-10 10 -10 10])
    
    to rescale your axes so that you can meaningfully compare this plot to the previous one. Below, enter this "axis" command every time you plot something new. (It's your option whether also to use the "grid" command to superimpose the coordinate grid on your plot as well.) Print your plot.

    Next see the effect that multiplying by S has on your slanted N by bringing up a new plot window and plotting the rows of S*A*B against each other. Print your plot. How does this plot differ from the previous plot?

  • Problem 4

    Choose an angle other than pi/2 strictly between 0 and pi. To rotate your picture through this angle, use your program from Problem 1 to multiply B by the appropriate rotation matrix. (For example, if function you programmed in Problem 1 is called "rotate", and the angle you choose is pi/6, you could type
    rotB=rotate(pi/6)*B
    
    and then plot the second row of rotB against the first row.) Print your plot.

  • Problem 5

    Input the matrices
    reflx=[1 0; 0 -1]
    
    and
    refly=[-1 0; 0 1]
    
    Appropriately multiplying matrices, plot the effect of the linear transformations corresponding to reflx and refly on your slanted N (the result of S*A*B). Print your plots. Describe in words what these linear transformations did.

  • Problem 6

    Input the matrix
    reflxy=[0 1; 1 0]
    
    Let rotB be the matrix you constructed in problem 4. Bring up a new plot window and re-plot the corresponding rotated N (using the same "axis" command as before), but don't print the plot yet. Type
    hold
    
    To see the effect that the linear transformation has on the rotated N, multiply reflxy*rotB. This time do not bring up a new plot window. Enter the commands to plot the second row of reflxy*rotB against the first. The "hold" command that you entered will cause the plot to be superimposed on the previous plot. Print out the double-plot, and describe in words the effect that multiplying by reflxy had on your rotated N.

    Another way to do such a double-plot without using the "hold" command is to enter

    plot(x1,y1,'.-',x2,y2,'.-')
    
    where x1,y1 are the first pair of x and y vectors you want to plot against each other and x2,y2 are the second pair. However, the two plots then come up simultaneously, so it's harder to tell which plot is which.

  • Problem 7

    Using your rotation-matrix program, define R to be the matrix corresponding to a counterclockwise rotation through the angle pi/2. Bring up a new plot window, and set the axes as before. Using the "hold" command again, on the same diagram plot the effects on your original N of (a) the linear transformation "multiply by SR" and the effect of (b) the linear transformation "multiply by RS". Print out the double-plot. What fact about composing two linear transformations in different orders (equivalently, multiplying two matrices in different orders) is illustrated by the fact that the transformed N's in your double-plot look different from each other?