# Picture Lab A6: Mirroring Pictures

Each coding exercise has its own folder. Open its `.java` file and press **Run** to try it. **Run Tests** checks every graded exercise in this lesson at once.

Car designers at General Motors Research Labs only sculpt half of a car out of clay and then use a vertical mirror to reflect that half to see the whole car. What if we want to see what a picture would look like if we placed a mirror on a vertical line in the center of the width of the picture to reflect the left side (Figure 1)?

![Original picture (left) and picture after mirroring (right)](images/picturelabmirror1.png)

*Original picture (left) and picture after mirroring (right)*

How can we write a method to mirror a picture in this way? One way to figure out the algorithm, which is a description of the steps for solving a problem, is to try it on smaller and simpler data. Figure 2 shows the result of mirroring a two-dimensional array of numbers from left to right vertically.

![Two-Dimensional array of numbers (left) and mirrored result (right)](images/picturelabmirror2.png)

*Two-Dimensional array of numbers (left) and mirrored result (right)*

Can you figure out the algorithm for this process? Test your algorithm on different sizes of twodimensional arrays of integers. Will it work for 2D arrays with an odd number of columns? Will it work for 2D arrays with an even number of columns?

One algorithm is to loop through all the rows and half the columns. You need to get a pixel from the left side of the picture and a pixel from the right side of the picture, which is the same distance from the right end as the left pixel is from the left end. Set the color of the right pixel to the color of the left pixel. The column number at the right end is the number of columns, also known as the width, minus one. So assuming there are at least 3 pixels in a row,

- The first left pixel will be at row=0, col=0 and the first right pixel will be at row=0, col=width-1.
- The second left pixel will be at row=0, col=1 and the corresponding right pixel will be at row=0, col=width-1-1.
- The third left pixel will be at row=0, col=2 and its right pixel will be at row=0, col=width-1-2.
- Each time the left pixel is at (current row value, current column value), the corresponding right pixel is at (current row value, width - 1 - (current column value)).

The following method implements this algorithm. Note that, because the method is not looping through all the pixels, it cannot use a nested for-each loop.

```java
public void mirrorVertical()
{
     Pixel[][] pixels = this.getPixels2D();
     Pixel leftPixel = null;
     Pixel rightPixel = null;
     int width = pixels[0].length;
     for (int row = 0; row < pixels.length; row++)
     {
          for (int col = 0; col < width / 2; col++)
          {
               leftPixel = pixels[row][col];
               rightPixel = pixels[row][width - 1 - col];
               rightPixel.setColor(leftPixel.getColor());
          }
     }
}
```

You can see `mirrorVertical` in action by running the exercise in the exercise below.

You can use caterpillar.jpg, puppy.jpg, redmotorcycle.jpg, or beach.jpg in the exercises below which are autograded.

![Mirrorright](images/mirrorright.png)

1. Write the method mirrorVerticalRightToLeft that mirrors a picture around a mirror placed vertically from right to left, so that you get 2 tails for the caterpillar instead of two heads. Hint: you can copy the body of `mirrorVertical` and only change one line in the body of the method to accomplish this.

## Exercise 1

Picture Lab A6 Mirroring: Write a method mirrorVerticalRightToLeft that mirrors a picture around a mirror placed vertically from right to left so that you get two tails for the caterpillar instead of two heads. Hint: you can copy the body of mirrorVertical and only change one line in the body of the method to accomplish this. Change the method call in the main method.

> Open the `01-picture-lab-A6-mirrorVerticalRightToLeft` folder and edit `Picture.java`. Press **Run** to try it; **Run Tests** checks it (4 tests). Its tests check your code, not what it prints: “mirrorVerticalRightToLeft() method”, “mirrorVerticalRightToLeft() sets leftPixel's color”, “mirrorVerticalRightToLeft() uses rightPixel's getColor”, “Checking that mirrorVerticalRightToLeft() contains 2 for loops”. It draws pictures: **Run** saves the drawing as an image in its folder, and **Run Tests** checks the code without the drawing.

## More practice

2. Write the method `mirrorHorizontal` that mirrors a picture around a mirror placed horizontally at the middle of the height of the picture. Mirror from top to bottom as shown in the pictures below (Figure 3).

![Original picture (left) and mirrored from top to bottom (right)](images/picturelabmirror3.png)

*Original picture (left) and mirrored from top to bottom (right)*

## Exercise 2

Picture Lab A6 Mirroring: Write a method mirrorHorizontal that mirrors a picture around a mirror placed horizontally at the middle of the height of the picture from top to bottom. Change the bottom pixel to be the color of the top pixel.

> Open the `02-picture-lab-A6-mirrorHorizontal` folder and edit `Picture.java`. Press **Run** to try it; **Run Tests** checks it (6 tests). Its tests check your code, not what it prints: “mirrorHorizontal() method”, “mirrorHorizontal() sets height to pixels.length”, “mirrorHorizontal() uses height/2”, “mirrorHorizontal() uses pixels[height - row - 1][col] to get the bottom pixel”, “mirrorHorizontal() uses bottomPixel.setColor to change the bottom pixel”, “Checking that mirrorHorizontal() contains 2 for loops”. It draws pictures: **Run** saves the drawing as an image in its folder, and **Run Tests** checks the code without the drawing.

## More practice

![Horizontalbot2top](images/horizontalbot2top.png)

1. Write the method `mirrorHorizontalBotToTop` that mirrors the picture around a mirror placed horizontally from bottom to top. Hint: you can copy the body of mirrorHorizontal and only change one line to accomplish this.

## Exercise 3

Picture Lab A6 Mirroring: Write the method mirrorHorizontalBotToTop that mirrors the picture around a mirror placed horizontally from bottom to top. Hint: you can copy the body of mirrorHorizontal and only change one line to accomplish this. Change the top pixel to the bottom pixel instead of the other way around.

> Open the `03-picture-lab-A6-mirrorHorizontalBotToTop` folder and edit `Picture.java`. Press **Run** to try it; **Run Tests** checks it (6 tests). Its tests check your code, not what it prints: “mirrorHorizontalBotToTop() method”, “mirrorHorizontalBotToTop() sets height to pixels.length”, “mirrorHorizontalBotToTop() uses height/2”, “mirrorHorizontalBotToTop() uses pixels[height - row - 1][col]”, “mirrorHorizontalBotToTop() uses setColor to change the topPixel variable”, “Checking that mirrorHorizontalBotToTop() contains 2 for loops”. It draws pictures: **Run** saves the drawing as an image in its folder, and **Run Tests** checks the code without the drawing.

## More practice

4. Challenge — Work in groups to figure out the algorithm for the method `mirrorDiagonal` that mirrors just a square part of the picture from bottom left to top right around a mirror placed on the diagonal line (the diagonal line is the one where the row index equals the column index). This will copy the triangular area to the left and below the diagonal line as shown below. This is like folding a square piece of paper from the bottom left to the top right, painting just the bottom left triangle and then (while the paint is still wet) folding the paper up to the top right again. The paint would be copied from the bottom left to the top right as shown in the pictures below (Figure 2).

![Original picture (left) and mirrored around the diagonal line with copying from bottom left to top right (right)](images/picturelabmirror4.png)

*Original picture (left) and mirrored around the diagonal line with copying from bottom left to top right (right)*

## Exercise 4

Picture Lab A6 Mirroring: Write a method method mirrorDiagonal that mirrors just a square part of the picture from bottom left to top right around a mirror placed on the diagonal line (the diagonal line is the one where the row index equals the column index).

> Open the `04-picture-lab-A6-mirrorDiagonal` folder and edit `Picture.java`. Press **Run** to try it; **Run Tests** checks it (3 tests). Its tests check your code, not what it prints: “mirrorDiagonal() method”, “mirrorDiagonal() loops for col < row”, “Checking that mirrorDiagonal() contains 2 for loops”. It draws pictures: **Run** saves the drawing as an image in its folder, and **Run Tests** checks the code without the drawing.
