Two-Dimensional Arrays and Nested Loops – part 2

Slides:



Advertisements
Similar presentations
Georgia Institute of Technology Drawing in Java – part 2 Barb Ericson Georgia Institute of Technology September 2005.
Advertisements

Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 6 Barb Ericson Georgia Institute of Technology August 2005.
TOPIC 9 MODIFYING PIXELS IN A MATRIX: COPYING, CROPPING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by.
Chapter 6: Modifying Pixels by Position. Chapter Learning Goals.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
CSC1401 Viewing a picture as a 2D image - 1. Review from the last week We used a getPixels() to get all of the pixels of a picture But this has been somewhat.
02-RangesInPictures1 Barb Ericson Georgia Institute of Technology Oct 2010 Working with ranges in pictures.
Georgia Institute of Technology Processing Sound Ranges Barb Ericson Georgia Institute of Technology July 2005.
UsingSoundRanges-part21 Processing Sound Ranges part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
TOPIC 11 RETURNING VALUES FROM METHODS PICTURE TRANSFORMATIONS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.
NestedLoops-Mod7-part31 Two-Dimensional Arrays and Nested Loops – part 3 Bugs in the garden Originally by Barb Ericson Georgia Institute of Technology.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 5 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-part41 Nested Loops – part 4 Barb Ericson Georgia Institute of Technology Nov 2009.
NestedLoops-Mody7-part51 Two-Dimensional Arrays and Nested Loops – part 5 Rotations Barb Ericson Georgia Institute of Technology May 2007.
Conditionals-part41 Conditionals – part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
Conditionals-Mod8-part41 Conditionals – part 4 Replace background Barb Ericson Georgia Institute of Technology May 2007.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Drawing in Java – part 3 Barb Ericson Georgia Institute of Technology September 2006.
Georgia Institute of Technology Conditionals – part 2 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-part21 Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
Copyright © Curt Hill Further Picture Manipulation Considering position.
Conditionals-Mod8-part21 Conditionals – part 2 Edge Detection Barb Ericson Georgia Institute of Technology May 2007.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005.
NestedLoops-Mod7-part61 Two-Dimensional Arrays and Nested Loops – part 6 Enlarge Barb Ericson Georgia Institute of Technology August 2005.
Georgia Institute of Technology Two-Dimensional Arrays and Nested Loops – part 4 Barb Ericson Georgia Institute of Technology August 2005.
Barb Ericson Georgia Institute of Technology September 2005
Barbara Ericson Georgia Tech Sept 2005
Topic 9 Modifying Pixels in a Matrix: Copying, Cropping
Barb Ericson Georgia Institute of Technology Dec 2009
Manipulating Pictures, Arrays, and Loops part 3
Creating and Modifying Text part 2
Barb Ericson Georgia Institute of Technology August 2005
Workshop for Programming And Systems Management Teachers
Barb Ericson Georgia Institute of Technology August 2005
Manipulating Pictures, Arrays, and Loops part 2
Working with ranges in pictures
Manipulating Pictures, Arrays, and Loops
Barb Ericson Georgia Institute of Technology August 2005
Two-Dimensional Arrays and Nested Loops – part 1
Barb Ericson Georgia Institute of Technology August 2005
Manipulating Pictures, Arrays, and Loops part 5
Two-Dimensional Arrays and Nested Loops – part 2
Two-Dimensional Arrays and Nested Loops – part 5
Two-Dimensional Arrays and Nested Loops – part 1
Georgia Institute of Technology
Arrays versus ArrayList
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
Manipulating Pictures, Arrays, and Loops part 4
Introduction to Processing Digital Sounds part 3
Two-Dimensional Arrays and Nested Loops – part 4
Two-Dimensional Arrays and Nested Loops – part 3
Two-Dimensional Arrays and Nested Loops – part 6
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Two-Dimensional Arrays and Nested Loops – part 6
Georgia Institute of Technology
Manipulating Pictures, Arrays, and Loops
Processing Sound Ranges part 2
Manipulating Pictures, Arrays, and Loops part 6
Processing Sound Ranges
CSC1401 Viewing a picture as a 2D image - 2
Two-Dimensional Arrays and Nested Loops – part 6
Barb Ericson Georgia Institute of Technology May 2006
Processing Sound Ranges part 3
Manipulating Pictures, Arrays, and Loops part 6
Barb Ericson Georgia Institute of Technology September 2005
Presentation transcript:

Two-Dimensional Arrays and Nested Loops – part 2 Barb Ericson Georgia Institute of Technology August 2005 Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Understand at a conceptual and practical level How to create a simpler version of a problem 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 Georgia Institute of Technology

Georgia Institute of Technology 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 Georgia Institute of Technology

Thinking Through Vertical Mirroring 1 2 3 4 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? 1 2 3 4 5 1 2 1 2 3 5 4 Georgia Institute of Technology

What is the Vertical Mirror for this? 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 1 2 1 2 3 4 5 6 7 8 9 1 2 1 2 1 2 Georgia Institute of Technology

Mirror Vertical Algorithm Loop through all the rows (y starts at 0, increments by 1, and is less than the picture height) Loop with x starting at 0 and x less than the midpoint (mirror point) value Get the left pixel at x and y Get the right pixel at width – 1 - x Set the color for the right pixel to be the color of the left pixel 1 2 3 4 5 1 2 3 5 4 Georgia Institute of Technology

Mirror Vertical Algorithm to Code 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 (starting at 1) for (int x = 0; x < midpoint; x++) { Set right pixel color to left pixel color Pixel leftPixel = this.getPixel(x, y); Pixel rightPixel = this.getPixel(this.getWidth() - 1 - x, y); rightPixel.setColor(leftPixel.getColor()); We don’t need to calculate the midpoint every time through the loop so calculate it once before the loop. Georgia Institute of Technology

Mirror Vertical Method public void mirrorVertical() { int mirrorPoint = this.getWidth() / 2; Pixel leftPixel = null; Pixel rightPixel = null; // loop through the rows for (int y = 0; y < this.getHeight(); y++) // loop from 0 to just before the mirror point for (int x = 0; x < mirrorPoint; x++) Georgia Institute of Technology

Mirror Vertical Method - Continued leftPixel = this.getPixel(x, y); rightPixel = this.getPixel(this.getWidth() – 1 – x, y); rightPixel.setColor(leftPixel.getColor()); } Georgia Institute of Technology

Using Picture.getMediaPath(fileName) Gets the full path name for the file with the passed short name redMotorcycle.jpg Defaults to using the directory c:\intro-prog-java\mediasources\ Set it to a directory using Picture.setMediaPath(“c:/book/media/"); This will write out the directory name to a file and remember it till you change it The method getMediaPath is a class method in the Picture class. Georgia Institute of Technology

Trying Mirror Vertical Create the picture Picture p1 = new Picture( FileChooser.getMediaPath(“caterpillar.jpg”); Invoke the method on the picture p1.mirrorVertical(); Show the picture p1.show(); Georgia Institute of Technology

Georgia Institute of Technology Mirror Horizontal What about mirroring around a mirror held horizontally in the vertical center of the picture? Like a reflection in a lake? Georgia Institute of Technology

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? 1 2 1 2 3 4 5 6 7 8 9 1 2 1 2 1 2 3 4 5 6 1 2 Georgia Institute of Technology

What is the Horizontal Mirror for this? 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 1 2 3 4 5 8 9 1 2 Georgia Institute of Technology

Mirror Horizontal Algorithm Get the vertical midpoint Picture height / 2 Loop through all the x values Loop from y=0 to y < vertical midpoint Get the top pixel At x and y Get the bottom pixel Height - 1 - y Set the bottom pixel’s color to the top pixel color 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 Georgia Institute of Technology

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? Georgia Institute of Technology

Georgia Institute of Technology Summary Create several small versions of a problem And solve those before you try to code a programming solution Try to determine the general algorithm from the concrete small versions Translate the algorithm into code You can mirror by changing how you loop and how you get the source and target pixels Georgia Institute of Technology