Welcome to section for CS 106A! Each week, your section will meet to review the material from the course and to work through some problems. This week we'll work on image-loop problems, using material from the first few lectures. Problems like this will appear on this week's assignment.

Solution code will be available for all section problems later on, so they will also serve as review problems. Each week's section page will list several problems, and your section leader will pick which problems to do and in what form.


Image Range-Loop Problems

You might find nested y,x range loops helpful in solving these. Problems like this are well suited to making a drawing first to think through the algorithm before coding, and especially to get the arithmetic for each coordinate exactly right. These algorithms are a magnet for off-by-one errors.

A good first step, as in lecture, is to sketch out the pixel grid x,y coordinates as in lecture to plan how your algorithm will access the pixels.

An image grid

Implement the following functions, each of which takes an image's filename and returns a SimpleImage. You might find the SimpleImage documentation useful to consult.

  1. double_left(filename): Takes the left half of the image, and copies it on top of the right half.

    double_left_output
  2. double_left_up(filename): Takes the left half of the image, and copies it on the right half as before, except it is flipped upside-down as it is copied.

    double_left_up_output
  3. copies_2(filename): Create a new image twice as wide as the original, and place two copies of the original image, in it, side by side.

    copies_2_output
  4. squeeze_width(filename, n): A funhouse mirror effect governed by the int parameter n. Create a new image with the same height as the original, but with a width that is n times smaller than the original's. Copy the original image such that it is squeezed horizontally.

    squeeze_width_output

    For example, if n = 4, the pixels in the output image with an x coordinate of 0 would be copies of the input pixels with an x coordinate of 0 * 4 = 0, while pixels in the output image with an x coordinate of 1 would be copies of the input pixels with an x coordinate of 1 * 4 = 4, and so on.

Test your solutions here.