170 likes | 214 Views
Learn how to mirror images vertically and horizontally using algorithms and code. Understand the concept behind mirroring pictures and practice creating mirror effects programmatically.
E N D
Two-Dimensional Arrays and Nested Loops – mirroring Barb Ericson Georgia Institute of Technology NestedLoops-Mod7-mirrors
You’re going to have to think for this one! • Goals • How to create an algorithm by solving several concrete examples • And figuring out what is common in them • How to translate the algorithm into code • How to mirror a picture vertically and horizontally NestedLoops-Mod7-mirrors
Vertical Mirroring • What if we want to pretend to place a mirror in the middle of the picture • We would see the left side of the picture mirrored on the right side NestedLoops-Mod7-mirrors
Thinking Through Vertical Mirroring 0 1 2 3 4 original • If we just think of a number at each x and y location instead of a color • The mirroring would look like this: • Can you find the algorithm to do this? 0 1 2 mirrored NestedLoops-Mod7-mirrors
What are the Vertical Mirror for these? Odd number of columns Even number of columns • Try the solve the problem for small samples • If you can’t solve it on a small sample • You can’t write a program to solve it 0 1 2 0 1 2 0 1 2 0 1 2 NestedLoops-Mod7-mirrors
Which pixel goes where? • The far left goes to the far right • The first is copied to the last • Column width-1 is set to column 0 • Move inward until you get to the middle • Column width-2 is set to column 1 • The loop for each row starts at 0 and goes to the middle NestedLoops-Mod7-mirrors
Is every row the same? • Yes • To mirror the whole picture how many rows should you go through? • All of them. • From y=0 to y= height-1 NestedLoops-Mod7-mirrors
Mirror Vertical Algorithm to Code • A method inside the Picture class • public void mirrorVertical( ) { • } • We are going to need the midpoint int midpoint = this.getWidth() / 2; • Loop through the rows (y values) for (int y = 0; y < this.getHeight(); y++) { • Loop through x values up to the midpoint for (int x = 0; x < midpoint; x++) { // you write the code to get the left and right pixels and change the color of the right pixel… see the next slide for hints } NestedLoops-Mod7-mirrors
Mirror Vertical Algorithm • Loop through all the rows (y starts at 0, while it is less than the picture height, increments by 1) • Loop with x starting at 0 and x less than the midpoint (mirror point) • Get the left pixel at x and y Pixel leftPixel = this.getPixel(x,y); • Get the right pixel at column width – 1 - x also in row y • Set the color for the right pixel to be the color of the left pixel NestedLoops-Mod7-mirrors
Trying Mirror Vertical • Create a picture • Picture p1 = new Picture(“caterpillar.jpg”); • Invoke the method on the picture • p1.mirrorVertical(); • Show the picture • p1.show(); NestedLoops-Mod7-mirrors
Mirror Horizontal • What about mirroring around a mirror held horizontally in the vertical center of the picture? • Like a reflection in a lake? NestedLoops-Mod7-mirrors
Thinking Through Mirror Horizontal • Again think of a number at each x and y location • Instead of a color • And try it with a small sample • How can we write a nested for loop to do this? original 0 1 2 0 1 2 0 1 2 0 1 mirrored 2 NestedLoops-Mod7-mirrors
What is the Horizontal Mirror for this? 0 1 2 3 4 • Try to solve the problem for several small samples problems • See if you can come up with the algorithm to solve it • Test it more small samples 0 1 2 NestedLoops-Mod7-mirrors
Mirror Horizontal Algorithm Only go half way down • This is a method of the Picture class. • public void mirrorHorizontal(){ • } • Get the Picture height • int height = this.getHeight(); • Get the vertical midpoint • Picture height / 2 • Loop through all the x values (columns) • Loop from y=0 to y < vertical midpoint • Get the top pixel (source) • At x and y • Get the bottom pixel (destination) • Height - 1 - y • Set the bottom pixel’s color to the top pixel color NestedLoops-Mod7-mirrors
Mirror Horizontal Exercise • Write the method to mirror the top half of the picture to the bottom half. • This is a motorcycle redMotorcycle.jpg • How about mirroring bottom to top? NestedLoops-Mod7-mirrors
Exercise • Add mirrorVertical and mirrorHorizontal to your Picture class. • Test with the rubber duckie. public class TestMirrors{ public static void main(){ Picture duck1 = new Picture(“duckie.gif”); Picture duck2 = new Picture(“duckie.gif”); duck1.mirrorVertical(); duck1.explore(); duck2.mirrorHorizontal(); duck2.explore(); } } NestedLoops-Mod7-mirrors
Challenge • Create a 4 way mirror creating a picture 4 times the size of the original Picture fourWayFlip = new Picture(source.getWidth()*2, source.getHeight()*2); Hint: copy the original picture to position (0,0) in the new picture. Flip the picture and then copy it into the new picture starting at the midway point. NestedLoops-Mod7-mirrors