TASK: Draw an 8x8 checkerboard as 64 squares filled with red or black.
COMMENT: The MATLAB "fill (xlist,ylist,color)" command can outline a shape and fill it with a color. Black is 'k' and red is 'r'.
Imagine the checkerboard as being drawn on graph paper:
8 o--o--o--o--o--o--o--o--o |b |r |b |r |b |r |b |r | 7 o--o--o--o--o--o--o--o--o |r |b |r |b |r |b |r |b | 6 o--o--o--o--o--o--o--o--o |b |r |b |r |b |r |b |r | Y: 5 o--o--o--o--o--o--o--o--o |r |b |r |b |r |b |r |b | 4 o--o--o--o--o--o--o--o--o |b |r |b |r |b |r |b |r | 3 o--o--o--o--o--o--o--o--o |r |b |r |b |r |b |r |b | 2 o--o--o--o--o--o--o--o--o |b |r |b |r |b |r |b |r | 1 o--o--o--o--o--o--o--o--o |r |b |r |b |r |b |r |b | 0 o--o--o--o--o--o--o--o--o 0 1 2 3 4 5 6 7 8 <-- X -->
Imagine drawing just the bottom row, which we can think of as row I = 1, columns J = 1 to 8:
I = 1, J = 1 xlist = [ 0, 1, 1, 0 ]; ylist = [ 0, 0, 1, 1 ]; fill ( xlist, ylist, 'r' ); I = 1, J = 2 xlist = [ 1, 2, 2, 1 ]; ylist = [ 0, 0, 1, 1 ]; fill ( xlist, ylist, 'b' ); I = 1, J = 3 xlist = [ 2, 3, 3, 2 ]; ylist = [ 0, 0, 1, 1 ]; fill ( xlist, ylist, 'r' ); I = 1, J = 4 xlist = [ 3, 4, 4, 3 ]; ylist = [ 0, 0, 1, 1 ]; fill ( xlist, ylist, 'b' ); and so on up to I=1, J=8.
Now imagine drawing these 8 squares using a FOR loop on J:
I = 1 for j = 1 : 8 xlist = [ ?, ?, ?, ? ]; <- What pattern did we see? ylist = [ 0, 0, 1, 1 ]; if ( ? ) fill ( xlist, ylist, 'r' ); else fill ( xlist, ylist, 'k' ); end end
Now imagine modifying this loop to draw row 2:
I = 2 for j = 1 : 8 xlist = [ ?, ?, ?, ? ]; <- Pattern the same as previous loop ylist = [ ?, ?, ?, ? ]; <- This has to change! if ( ? ) <- Do you have the right test here? fill ( xlist, ylist, 'r' ); else fill ( xlist, ylist, 'k' ); end end
If you can draw one square, and then draw row 1, and then see how to draw row 2, you should be able to draw the whole checkerboard with a double loop:
for i = 1 : 8 for j = 1 : 8 xlist = [ ?, ?, ?, ? ]; ylist = [ ?, ?, ?, ? ]; if ( ? ) fill ( xlist, ylist, 'r' ); else fill ( xlist, ylist, 'k' ); end end end
INSTRUCTIONS:
Type "clf" so you are sure to have a clear graphics window. Type "hold on" so all your squares show up together. Set up your double loop: for i = 1 : 8 for j = 1 : 8 xlist = [ ?, ?, ?, ? ]; ylist = [ ?, ?, ?, ? ]; if ( ? ) fill ( xlist, ylist, 'r' ); else fill ( xlist, ylist, 'k' ); end end end Then give a "hold off" command. Your plot may look squashed. Force MATLAB to use the same scale in X and Y using the command: axis ( equal ) MATLAB likes to show the X and Y axis. For this plot, we don't want them, so add the command: axis off Your last command should save a copy of your plot: print ( '-djpeg', 'hw036.jpg' );
CHECK:(Note that the lower left square should be RED, not black.)
SUBMIT: I only need your script file "hw036.m". I do NOT need your plot file. Your script file should begin with:
% hw036.m % YOUR NAME % This script (describe what it does)