Mini-Lab: More Practice with For Loops
for Manipulating Pixels in a Picture

 


Introduction

This mini-lab will give you extra practice using for loops to step through pixels in a picture.



Drawing lines across a picture

  1. Write a function that draws a white horizontal line across the top of a picture. (DO NOT use the addLine function that you used in the Drawing Pictures mini-lab!) Your function should take the picture as a parameter, and set all the pixels in the first row to white.   Test your function with an actual picture, either that you read in with makePicture(pickAFile()) or that you created with makeEmptyPicture.
    Tips: You may recall that JES provides a number of Color keywords, including white, which you can pass to the setColor function.  Also, it might be easier to see your results for some of these exercises against a solid-color background created by makeEmptyPicture than against the variety of colors present in many pictures.  If it is difficult to see the line at the very top row, try drawing the line on row 10.
  2. Write a function similar to the one above, but use a color passed as a parameter rather than the constant white.
    Tip:You might start by copying and pasting your function from above and renaming it. [Quote from Alyce: "Never type if you can cut and paste."]
  3. Write another variation on the same function, this time passing the row number as another parameter.
  4. Now write a function that draws a vertical line on a picture. The function should take a picture, a color and a column number as a parameters.

Filling chunks of a picture

  1. Write a function to fill the entire canvas or picture passed in as a parameter with a specified color.
    Tip:Start by copying and pasting the function above.
  2. Write a function to fill the left half of a canvas or picture with a specified color.  (Or the right half if you prefer.)
  3. Write a function to fill the top half of a canvas or picture with a specified color.  (Or the bottom half if you prefer.)
  4. Write a function to fill one quadrant (such as the upper-left fourth) of a canvas or picture with a specified color.

Drawing grids over a picture

  1. Write a function that colors every other row of a canvas or picture with a specified color.
    Tip: There are two possible ways to write the functions in this section: you could use nested for loops, or you could use for loops that include calls to the functions you wrote in the "Drawing lines..." section above. Feel free to use whichever approach seems more natural to you.
  2. Write a function that colors every other column of a canvas or picture with a specified color.
  3. Write a function that colors every other row and column of a canvas or picture with a specified color.
  4. Write a function that creates a plaid effect across a canvas or picture in a specified color. For this exercise, your lines could be wider than 1 pixel, and/or they could be farther apart than 1 pixel.

If you have time: drawing diagonals

  1. Write a function that draws a diagonal line from the upper-left corner of a canvas or picture down and to the right. To make developing the algorithm easier, assume that the picture is perfectly square. (You can ensure this by using makeEmptyPicture and making the width and height the same, rather than passing in an arbitrary picture.)
  2. Write an efficient function to draw a diagonal line starting from the upper-left corner of the canvas. Do not assume that the picture is necessarily perfectly square.  This means that you must make sure that you stop drawing if you come to either the right edge or bottom edge of the picture.
  3. Write an efficient function to draw a diagonal line from the upper-right corner of a canvas or picture down and to the left. (Or you could start in the lower-left corner and draw up and to the right if you prefer.)  Do not assume that the picture is necessarily perfectly square.
  4. Write a function that draws an X across a picture.  (Hint: you should be able to do this in very few lines of code!)

Submit your results

  1. Submit your file for this mini-lab via . You must have finished exercises 1-5, exercise 6 or exercise 7, exercises 8-11 and the first exercise under 'drawing diagonals' before turning this in. (That's 11 functions!)