ELEC 200 Fall 2006 page 1 of 3 Assignment 2 Due at the beginning of class on Friday, September 22, 2006 Instructions Save a diary of your MatLab session. Using your favourite text editor, clean up the diary file to remove unecessary output that makes the session difficult to follow. Be sure to keep all output that is needed to show the correctness, completeness and logical flow of your work. Include your name, your student number, the date and the assignment number at the beginning of the file. Print out a copy of the resulting document and hand it in. The marker may refuse to mark any assignment considered too difficult to follow. If any questions are unclear to you, ask in class, by e-mail or during office hours. Questions 1. a) [16 marks] Copy the following comment block into a new m-file and write the MatLab function, Draw2D(), that conforms to the specification: % % % % % % % % % % % % % % % % % % % % % % % % % -----------------------------------------------------------------------------Draw2D - Draw the indicated line segments between the given points in 2D. Written by <name>. Last modified <date>. Call: Draw2D(Q, d, c) Args: Q - a 2 x N matrix of vertices in columns Q(:,i) is the ith vertex d - a 1 x N vector of drawing commands when i < N, d(i) indicates a line joining adjacent vertices: if d(i) = 1 then a line joins Q(:,i) to Q(:,i+1) if d(i) = 0 then there is no line between Q(:,i) and Q(:,i+1) when i = N, d(i) indicates a line closing the figure: if d(N) = 1 then a line joins Q(:,N) to Q(:,1) if d(N) = 0 then there is no line between Q(:,N) and Q(:,1) c - a ColorSpec, giving the color with which to draw the lines eg, one of 'r','g','b','c','m','y','k' or [R G B], where 0 <= R <= 1, 0 <= G <= 1, 0 <= B <= 1 or a 1 x N vector of single-character ColorSpecs, one for each line or a 3 x N matrix of [R G B] triplets, one for each line Rtns: none -----------------------------------------------------------------------------Your function should check its parameters thoroughly (useful MatLab functions are any(), all(), and ischar() among others) to make sure they are of the right size and type. Your function should correct minor parameter errors when it can, and exit using the error() function when it cannot. The documentation for the MatLab line() function tells you that X,Y pairs can be followed by parameter/value pairs to specify additional properties of the lines. Note that one such possible pair is 'Color', ColorSpec. ELEC 200 Fall 2006 page 2 of 3 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 b) [10 marks] Write a script to test your function by reproducing the figure above. Draw the object and the x and y axes, using your Draw2D() function. Use axis([-10 10 -10 10], 'square') to define the viewing area of your figure. Use grid('on') to turn on the gridlines, which will probably not look like the ones in the figure above. Make the dark lines in the object blue using colorspec 'b', make the light lines magenta using colorspec 'm' and make the axes black using colorspec 'k'. Use one call to Draw2D() for each of the three hexagons, one call for all of the connecting lines, and one call for the axes. Notice that you will sometimes have to specify a colorspec for a line which is not drawn (that is, where d(i) = 0), as a place holder to keep the size of c consistent with Q and d. c) [8 marks] Use get(gcf) to obtain a list of parameters that you can set for the current figure object and use get(gca) to obtain a list of parameters that you can set for the current axes object. Modify your MatLab script to use set(gcf, ...) to turn the background color of your figure from grey to white and use set(gca, ...) to make the gridlines look exactly like the ones in the above figure. For the x-gridlines, use the 'xtick' and 'xcolor' properties, and similarly for y. Use a color of [.8 .8 .8] for the gridlines and [.6 .6 .6] for the axes. Use [.6 0 0] for the dark lines in your object and [1 .8 .8] for the light lines. Now your figure should look exactly like the one above. d) [6 marks] Modify your script to draw the whole object and the axes all at once with a single call to Draw2D(). ELEC 200 Fall 2006 page 3 of 3 2. a) [8 marks] When d(i) = 1, think of a pen starting at point Q(:,i) and drawing a line of color c(:,i) to point Q(:,i+1); this is a draw operation. When d(i) = 0, think of moving the pen from Q(:,i) to Q(:,i+1) without drawing; this is a move operation. Modify your Draw2D() function to combine successive move operations and to combine successive draw operations of the same color so as to minimize the number of calls to the built-in line() function. b) [6 marks] Further modify your function to return a vector of handles to the lines it draws and modify your script from question 1 c) such that the returned handles are displayed in the command window. c) [6 marks] Modify your script from question 1 d) to work with your modified Draw2D() function, and use the returned vector of handles to change the color of all the red lines in the object to [0 0 .7] (dark blue) and all the pink lines to [.7 .7 1] (light blue) while leaving the axes unchanged.