## CSE 131 Module 2: Choice & Iteration

### Manipulating an Image Raster Using Iteration

Directions: In the imagetransforms package, modify the provided Transforms Java program to implement the methods as described below. Your methods will use iteration (either while loops or for loops) to operate on the pixels of a picture.

#### Notes

• To run this extension, right (control) click on Main and choose Run as... then Java Application.

This extension uses the same base code as the filter extension.

• The Picture class is a Sedgewick class whose API is documented on page 720 of our text. The coordinates are expressed using the standard in computer science graphics: the coordinate (x,y) denotes the pixel that is
• x pixels to the right of the leftmost edge of the picture
• y pixels down from the top edge of the picture
Thus, (0,0) is the top left corner of the picture.
• Pixel addressing follows the common Java convention: if there are w horizontal pixels, then 0 is the first (leftmost) and w-1 is the last (rightmost).
• Almost all of the methods are written in terms of two Picture parameters:
• source: the picture to be used as input to your transformation
• target: the picture area to be used as output from your transformation
There is just one exception: the gradient method produces its output without needing a source. Its only parameter is target.

• Each Picture has a width and a height, and these can be obtained for a Picture p as follows:
```  int width  = p.width();
int height = p.height();
```
• To find the Color of a pixel at location x,y, you use
```  Color c = source.get(x,y);
```
• To set the Color of a pixel at location x,y to the color c, you use
```  target.set(x,y,c);
```

#### Instructions

Each of the methods described below is found in the Transforms class.

1. The provided method flipHoriz(Picture source, Picture target) flips the image horizontally.

Look at the code given to you for this example carefully. It is broken into simple steps and the comments help explain why the pixel indexing works.

2. Complete the method flipVert(Picture source, Picture target) that flips the image vertically.
3. Complete the method flipHorizLeftHalf(Picture source, Picture target) that flips the left half of the image horizontally.
The left half of the target image should be same as the source, but the right half of the target image should be the mirror of the left half of the source.

4. Complete the method flipVertBotHalf(Picture source, Picture target) that flips the bottom half of the image vertically.
5. Complete the method gradient(Picture target) that takes a single Picture as a parameter. Your code should create a color gradient by computing the following for each pixel:
• The amount of red in each pixel increases gradually from 0 at the left edge of the image, to 255 at the right edge of the image.
• The amount of green in each pixel increases gradually from 0 at the top edge of the image, to 255 at the bottom edge of the image.
• The amount of blue in every pixel should be 128.
Thus, each pixel will have a different color depending on its position. For example, the pixel at the top left will have red=0, green=0, and blue=128. The pixel about 1/4 of the way down on the right edge will have red=255, green=64, and blue=128.
Develop an expression for the amount of red and green in each pixel, given the x and y position of that pixel and the width and height of the image:
```   int amountRed   = .....
int amountGreen = ....
```
Then set the pixel at (x,y) to a color based on those computations:
```    target.set(x, y, new Color(amountRed, amountGreen, 128));
```

Note There are other methods in the Transform class, but they are the subject of other extensions. Once you have completed the above work, demo and you are done with this extension.
When you done with this extension, you must be cleared by the TA to receive credit.
• Fill in the form below with the relevant information
• Have a TA check your work
• The TA should check your work and then fill in his or her name
• Click OK while the TA watches
• If you request propagation, it does not happen immediately, but should be posted in the next day or so

 Last name WUSTL Key Propagate? (or your numeric ID) Do not propagate e.g. Smith j.smith 1 Copy from 1 to all others 2 Copy from 2 to all others

### Loop Invariants

Loop invariants were discussed in the CSE131R seminar. You are welcome to try this extension even if you are not participating in the seminar. However, you must agree to the following:
• You will meet with the instructor as indicated in the steps below.
• Those meetings will take place in office hours during the semester, or during times otherwise arranged with the instructor.
• Your work on this extension must be complete before the last day of class.
Consider the following code:
```int ans = 0;
int i   = a;

while (__________________________ ) {  // fill in the loop predicate
i   =   i - b
ans = ans + 1
}
// ans is now a/b
```
The code is supposed to compute ans as the int result of dividing a by b.
1. Develop a loop invariant for the above code that can be used to prove that ans = a/b using integer division.
You should show your loop invariant to the instructor before proceeding, to be sure that you have a proper loop invariant
2. Write a mathematical proof of the loop's correctness. Every step must proceed from what came before it, with no gaps in logic. Any flaw will cause your submission to be rejected, but you can continue revising your proof until the end of the semester when extensions are finally due.
3. When the loop is over, what is the strongest statement you can make about i?
Check this with the instructor too.
To be cleared for credit, show your final work to the instructor. Be sure your written work clearly bears your name, your WUSTL key, and your student ID.

You will be asked to explain steps of your proof, so you better understand what you write if you want to get credit for it.

When you done with this extension, you must be cleared by the TA to receive credit.