PIC - Mirroring Pictures (Lesson)

APCompSci_LessonTopBanner.png

Mirroring Pictures

Introduction

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? How can we reflect one side of an image onto the other side?

Caterpillars mirror image 

How can we reflect one side of an image onto the other side? We will need to develop an algorithm to solve this problem. Using the 2D array below as an example, try to devise an algorithm or process for moving the pixels (in this case numbers) from one side of the array to the other.
Duck saying an algorithm is a step by step plan that can be applied to solve many variations of the same problem. 

Table 

We would want the resulting array to look as follows. Try your algorithm out with this array and with different sized arrays with both odd and even numbers of rows and columns. Tweak and change your algorithm until you get one that will work effectively in all cases.

Table 

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 either 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 knows 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 = 1, 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 (row, col), corresponding right pixel is at (row, width – 1 – col).

Find the mirrorVertical() method in the Picture class. You will note that we do not use a for-each loop since we are only looping over half of the columns.

Change the main method of Picture as follows, compile and run.

public static void main (String{ } args)
{
Picture temple = new Picture("temple.jpg");
temple.explore ( );
temple.mirrorTemple ( ) ;
temple.explore ( ) ;
} You will see a Greek temple that has been restored by mirroring part of the picture.

Examine the mirrorTemple() method. You will notice that this method has changed the starting and ending values for the nested for loops. It also calculates the distance the current column is from the mirrorPoint and then adds that distance to the mirrorPoint to get the column to copy to.

Exploring mirrorTemple() Challenge Activity

Click below to begin the Exploring mirrorTemple() challenge.

Mirroring Picture Lab

Add the following methods to the Picture class. You will need these methods for the final lab project.

1. Write the method mirrorVerticalRightToLeft that mirrors a picture around a mirror placed vertically from right to left. HINT: you can use the mirrorVertical method as a model and change one line in the body of the method.
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.
3. Write the method mirrorHorizontalBottomToTop that mirrors the picture around a mirror placed horizontally from bottom to top. HINT: you can use the mirrorHorizontal method as a model and change one line in the body of the method.
4. Write the method mirrorArms to mirror the arms on the snowman to make a snowman with 4 arms. Use the snowman.jpg picture.
5. Write the method mirrorGull to mirror the seagull to the right so that there are two seagulls on the beach near each other. Use the seagull.jpg picture.


Be sure to test all the new methods to make sure they work as intended.

APCompSci_LessonBottomBanner.pngIMAGES CREATED BY GAVS