NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.

Slides:



Advertisements
Similar presentations
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Advertisements

Conditionals-part11 Barb Ericson Georgia Institute of Technology Nov 2009.
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.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
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.
ManipulatingPictures-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Georgia Institute of Technology Processing Sound Ranges Barb Ericson Georgia Institute of Technology July 2005.
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.
TOPIC 6 MODIFYING PICTURES USING LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and.
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.
CPSC1301 Computer Science 1 Chapter 4 Manipulating Pictures, Arrays, and Loops part 5.
CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.
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.
ManipulatingPictures-part31 Manipulating Pictures, Arrays, and Loops part 3 Barb Ericson Georgia Institute of Technology Nov 2009.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
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
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Barb Ericson Georgia Institute of Technology Dec 2009
Workshop for Programming And Systems Management Teachers
Barb Ericson Georgia Institute of Technology August 2005
Manipulating Pictures, Arrays, and Loops part 2
Manipulating Pictures, Arrays, and Loops part 2
Georgia Institute of Technology
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
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
CSE 8A Lecture 6 Reading for next class:
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
Two-Dimensional Arrays and Nested Loops – part 3
Two-Dimensional Arrays and Nested Loops – part 6
Two-Dimensional Arrays and Nested Loops – part 2
Manipulating Pictures, Arrays, and Loops
Manipulating Pictures, Arrays, and Loops
Two-Dimensional Arrays and Nested Loops – part 6
Georgia Institute of Technology
Processing Sound Ranges part 2
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
Manipulating Pictures, Arrays, and Loops
Processing Sound Ranges part 3
Barb Ericson Georgia Institute of Technology September 2005
Presentation transcript:

NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part12 Learning Goals Understand at a conceptual and practical level –What is a two-dimensional array? –How do you get the pixel for a x and y location? –How do you use nested loops? –How to convert a single for loop to a nested for loop? –How to simplify a problem To make it easier to figure out the algorithm

NestedLoops-part13 What is a two-dimensional array? The pixels in a picture are really stored in a two-dimensional array –Each pixel has a x value (horizontal location) –Each pixel has a y value (vertical location) –pictureObj.getPixel(x,y) returns the pixel at that location x y

NestedLoops-part14 Example Two-Dimensional Arrays Maps –That street is in D-5 Battleship –Try I-5 Hit or miss Chairs at a theater or game –Row C seat 20

NestedLoops-part15 Nested Loop How would you get all the pixels in a picture using their x and y values –From left to right and top to bottom? –x=0 and y=0, x=1 and y=0, x=2 and y=0, … –x=0 and y=1, x=1 and y=1, x=2 and y=1, … –x=0 and y=2, x=1 and y=2, x=2 and y=2, … We need to have one loop inside another –The outer loop counts y from 0 to height - 1 –The inner loop counts x from 0 to width - 1

NestedLoops-part16 Alternative Nested Loop How would you get all the pixels in a picture using their x and y values –From top to bottom and left to right? –x=0 and y=0, x=0 and y=1, x=0 and y=2, … –x=1 and y=0, x=1 and y=1, x=1 and y=2, … –x=2 and y=0, x=2 and y=1, x=2 and y=2, … We need to have one loop inside another –The outer loop counts x to width - 1 –The inner loop counts y from 0 to height - 1

Alice Nested Loop In Alice we used a nested loop to turn the ferris wheel. The outer loop rolled the large double wheel to the right and the inner loop rolled the two smaller wheels to the left. NestedLoops-part17

8 Java Nested Loop Template // loop through the rows for (int y = 0; y < this.getHeight(); y++) { // loop through the columns for (int x = 0; x < this.getWidth(); x++) { // get the current pixel pixelObj = this.getPixel(x,y); // do something to the color // set the new color pixelObj.setColor(colorObj); }

NestedLoops-part19 Alternative Nested Loop Template // loop through the columns (x direction) for (int x = 0; x < this.getWidth(); x++) { // loop through the rows (y direction) for (int y = 0; y < this.getHeight(); y++) { // get the current pixel pixelObj = this.getPixel(x,y); // do something to the color // set the new color pixelObj.setColor(colorObj); }

Example using nested loops public void clearBlueNested() { Pixel pixel = null; // loop through the columns (x direction) for (int x = 0; x < getWidth(); x++) { // loop through the rows (y direction) for (int y = 0; y < getHeight(); y++) { // get pixel at the x and y location pixel = this.getPixel(x,y); // clear the blue pixel.setBlue(0); } NestedLoops-part110

Tracing clearBlueNested pict.clearBlueNested() –Starts execution of the clearBlueNested method on the picture called pict Pixel pixel = null –Set up a variable to refer to the current pixel for (int x = 0; x < getWidth(); x++) –Loop with x starting at 0, stop when x equals the width of the picture and increment by 1 each time. This will loop through the columns. Notice that you don't have to use this.getWidth() the this is implied. NestedLoops-part111

Tracing clearBlueNested - continued for (y = 0; y < getHeight(); y++) Loop with y starting at 0, stop when y equals the height of the picture and increment by 1 each time. This will loop through the rows. pixel = getPixel(x,y); –set the variable pixel to refer to the pixel at the current x and y location. The first time through the loop this is (0,0). The second time it is (0,1). The third time it is (0,2). This keeps on till we process all the pixels in the first column from top to bottom and then we move to the second column. We keep processing pixels till we are done with all of them. pixel.setBlue(0) // set the blue value to 0 NestedLoops-part112

NestedLoops-part113 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-part114 Thinking Through Vertical Mirroring 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 write the algorithm to do this?

NestedLoops-part115 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

NestedLoops-part116 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

NestedLoops-part117 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() x, y); rightPixel.setColor(leftPixel.getColor());

NestedLoops-part118 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++) {

NestedLoops-part119 Mirror Vertical Method - Continued leftPixel = this.getPixel(x, y); rightPixel = this.getPixel(this.getWidth() – 1 – x, y); rightPixel.setColor(leftPixel.getColor()); }

NestedLoops-part120 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 –FileChooser.setMediaPath(“f:/mediasources/"); or –FileChooser.pickMediaPath(); This will write out the directory name to a file and remember it till you change it

NestedLoops-part121 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();

NestedLoops-part122 Mirror Horizontal What about mirroring around a mirror held horizontally in the vertical center of the picture? –Like a reflection in a lake?

NestedLoops-part123 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?

NestedLoops-part124 What is the Horizontal Mirror for this? Try to solve the problem for several small sample problems See if you can come up with the algorithm to solve it –Test it more small samples

NestedLoops-part125 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 y Set the bottom pixel’s color to the top pixel color

NestedLoops-part126 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? How would you mirror on a diagonal line?

NestedLoops-part127 Summary Nested loops can be used to loop through a two-dimensional array –And keep track of the current x and y values 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