+ All Categories
Home > Documents > Nested Loops – part 2

Nested Loops – part 2

Date post: 05-Jan-2016
Category:
Upload: holt
View: 36 times
Download: 2 times
Share this document with a friend
Description:
Nested Loops – part 2. Barb Ericson Georgia Institute of Technology Nov 2009. Learning Goals. Understand at a conceptual and practical level How to mirror part of a picture How to calculate the number of times a nested loop will execute How to copy pixels from one picture to another - PowerPoint PPT Presentation
27
NestedLoops-part2 1 Nested Loops – part 2 Barb Ericson Georgia Institute of Technology Nov 2009
Transcript
Page 1: Nested Loops – part 2

NestedLoops-part2 1

Nested Loops – part 2

Barb Ericson

Georgia Institute of Technology

Nov 2009

Page 2: Nested Loops – part 2

Georgia Institute of Technology

Learning Goals

• Understand at a conceptual and practical level– How to mirror part of a picture– How to calculate the number of times a

nested loop will execute– How to copy pixels from one picture to

another– How to declare, initialize and change several

variables in a for loop– How to copy part of a picture to another

picture

Page 3: Nested Loops – part 2

Mirror part of a picture

• Use the explorer to figure out the part to mirror

NestedLoops-part2 3

Page 4: Nested Loops – part 2

Mirror temple method/**

* Method to mirror part of the temple picture around a

* vertical line at a mirror point

*/

public void mirrorTemple()

{

int mirrorPoint = 276;

Pixel leftPixel = null;

Pixel rightPixel = null;

// loop through the rows

for (int y = 27; y < 97; y++)

{

// loop from 13 to just before the mirror point

for (int x = 13; x < mirrorPoint; x++)

NestedLoops-part2 4

Page 5: Nested Loops – part 2

Mirror temple - continued

{ leftPixel = getPixel(x, y);

rightPixel = getPixel(mirrorPoint +

(mirrorPoint - x), y);

right Pixel .setColor(leftPixel .getColor());

}

}

}

NestedLoops-part2 5

Page 6: Nested Loops – part 2

Testing the mirrorTemple method

> String fileName =

"C:/intro-prog-java/mediasources/temple.jpg";

> Picture picture = new Picture(fileName);

> picture.explore();

> picture.mirrorTemple();

> picture.explore();

NestedLoops-part2 6

Page 7: Nested Loops – part 2

How many pixels were changed?

• We could put print statements in the code – to see the x and y values, but that would take a long

time

• We can also just keep a count – And print the value of the count after the loop ends

• We can calculate the number of times a loop will run using end – start + 1 = number of times– numOuter = 96 – 27 + 1 = 70– numInner = 275 – 13 + 1 = 263– Total for a nested loop is numInner * numOuter

• 70 * 263 = 18,410

NestedLoops-part2 7

Page 8: Nested Loops – part 2

Georgia Institute of Technology

Copying Pixels to a New Picture

• Need to track the source picture x and y – And the target picture

x and y

• We can use a blank picture – As the target picture

• Several blank pictures are available– 640x480.jpg– 7inX95in.jpg

1 2

3 4

1 2

3 4

Page 9: Nested Loops – part 2

Georgia Institute of Technology

Copy Picture Algorithm

• Copy a picture to the 7 by 9.5 inch blank picture– Create the target picture object– Invoke the method on the target picture

• Create the source picture object• Loop through the source picture pixels

– Get the source and target pixels– Set the color of the target pixel to the color of

the source pixel

Page 10: Nested Loops – part 2

Georgia Institute of Technology

Copy Algorithm to Code

• Loop through the source pixels

// loop through the columns for (int sourceX = 0, targetX = 0; sourceX < sourcePicture.getWidth(); sourceX++, targetX++) { // loop through the rows for (int sourceY = 0, targetY = 0; sourceY < sourcePicture.getHeight(); sourceY++, targetY++) {

Page 11: Nested Loops – part 2

Georgia Institute of Technology

Copy Algorithm to Code – Cont

• Get the source and target pixels

sourcePixel = sourcePicture.getPixel(sourceX,sourceY);

targetPixel = this.getPixel(targetX,targetY);

• Set the color of the target pixel to the color of the source pixel

targetPixel.setColor(sourcePixel.getColor());

Page 12: Nested Loops – part 2

Georgia Institute of Technology

Copy Methodpublic void copyKatie()

{

String sourceFile =

FileChooser.getMediaPath("KatieFancy.jpg");

Picture sourcePicture = new Picture(sourceFile);

Pixel sourcePixel = null;

Pixel targetPixel = null;

// loop through the columns

for (int sourceX = 0, targetX = 0;

sourceX < sourcePicture.getWidth();

sourceX++, targetX++)

{

Page 13: Nested Loops – part 2

Georgia Institute of Technology

Copy Method - Continued // loop through the rows for (int sourceY = 0, targetY = 0; sourceY < sourcePicture.getHeight(); sourceY++, targetY++) {

// set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } }

Page 14: Nested Loops – part 2

Georgia Institute of Technology

Trying the copyKatie Method

• Create a picture object using the 7inX95in.jpg file in the mediasources directory– Picture p1 = new

Picture(FileChooser.getMediaPath(“7inX95in.jpg”));

• Show the picture– p1.show();

• Invoke the method on this picture object – p1.copyKatie();

• Repaint the picture– p1.repaint();

Page 15: Nested Loops – part 2

Georgia Institute of Technology

Result of copyKatie Method

Page 16: Nested Loops – part 2

Georgia Institute of Technology

Copying Pixels to a New Picture

• What if we want to copy the target to a different location in the source– Than 0,0– Say startX and startY

• What is an algorithm that will do this?

1 2

3 4

1 2

3 4

0

1

0

1

0 1 2 3

0

1

2

3

Page 17: Nested Loops – part 2

Georgia Institute of Technology

Copy to Position Exercise

• Write a method copyRobot to copy – robot.jpg– To location

• 100, 100 in 7inx95in.jpg

• Test withString file =

FileChooser.getMediaPath(“7inx95in.jpg”);

Picture p = new Picture(file);

p.copyRobot();

p.show();

Page 18: Nested Loops – part 2

Cropping

• We can copy just part of a picture to a new picture– Just change the start and end source x and y

values to the desired values– Use pictureObj.explore() to find the x and y

values– What are the x and y values to get the face of

the girl in KatieFancy.jpg?

Page 19: Nested Loops – part 2

Copy Face Methodpublic void copyKatiesFace() { String sourceFile = FileChooser.getMediaPath("KatieFancy.jpg"); Picture sourcePicture = new Picture(sourceFile); Pixel sourcePixel = null; Pixel targetPixel = null;

// loop through the columns for (int sourceX = 70, targetX = 100; sourceX < 135; sourceX++, targetX++) { // loop through the rows for (int sourceY = 3, targetY = 100; sourceY < 80; sourceY++, targetY++) {

Page 20: Nested Loops – part 2

Copy Face Method - Continued // set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } }

Page 21: Nested Loops – part 2

Testing Copy Katie’s Face

• Create a picture object – Picture p1 = new

Picture(FileChooser.getMediaPath(

“7inX95in.jpg”));

• Show the picture– p1.show();

• Invoke the method – p1.copyKatiesFace();

• Repaint the picture– p1.repaint();

Page 22: Nested Loops – part 2

Creating a Collage

• You can create a collage by copying several pictures to a blank picture– You can use the

general copy method on flower1.jpg and flower2.jpg

Page 23: Nested Loops – part 2

Create Collage Method/** * Method to copy flower pictures to create a * collage. * All the flower pictures will be lined up near the * top of the current picture */ public void copyFlowersTop() { // create the flower pictures Picture flower1Picture = new

Picture(FileChooser.getMediaPath( "flower1.jpg")); Picture flower2Picture = new

Picture(FileChooser.getMediaPath( "flower2.jpg"));

// declare the source and target pixel variables Pixel sourcePixel = null; Pixel targetPixel = null;

// copy the first flower picture to the top left // corner for (int sourceX = 0, targetX = 0; sourceX < flower1Picture.getWidth(); sourceX++, targetX++) { for (int sourceY = 0, targetY = 0; sourceY < flower1Picture.getHeight(); sourceY++, targetY++) { sourcePixel =

flower1Picture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } }

Page 24: Nested Loops – part 2

Create Collage Method - cont// copy the flower2 picture starting with x = // 100 for (int sourceX = 0, targetX = 100; sourceX < flower2Picture.getWidth(); sourceX++, targetX++) { for (int sourceY = 0, targetY = 0; sourceY < flower2Picture.getHeight(); sourceY++, targetY++) { sourcePixel =

flower2Picture.getPixel(sourceX, sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } }

// copy the flower1 negated to x = 200 flower1Picture.negate(); for (int sourceX = 0, targetX = 200; sourceX < flower1Picture.getWidth(); sourceX++, targetX++) { for (int sourceY = 0, targetY = 0; sourceY < flower1Picture.getHeight(); sourceY++, targetY++) { sourcePixel =

flower1Picture.getPixel(sourceX, sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } }

Page 25: Nested Loops – part 2

Create Collage Method - cont// clear the blue in flower 2 picture and add at // x=300flower2Picture.clearBlue();for (int sourceX = 0, targetX = 300; sourceX < flower2Picture.getWidth(); sourceX++, targetX++){ for (int sourceY = 0, targetY = 0; sourceY < flower2Picture.getHeight(); sourceY++, targetY++) { sourcePixel =

flower2Picture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); }}

// copy the negated flower 1 to x=400for (int sourceX = 0, targetX = 400; sourceX < flower1Picture.getWidth(); sourceX++, targetX++){ for (int sourceY = 0, targetY = 0; sourceY < flower1Picture.getHeight(); sourceY++, targetY++) { sourcePixel =

flower1Picture.getPixel(sourceX, sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } }}

Page 26: Nested Loops – part 2

Challenge

• Create your own collage– Copy at least two different pictures to the

collage – Do at least 3 different picture manipulations to

the pictures• Reduce red• Negate• Clear blue

– Mirror the collage

Page 27: Nested Loops – part 2

Georgia Institute of Technology

Summary• To mirror part of a picture

– Set the starting and ending values in the nested loop• To copy pixels from one picture to another

– Keep track of the sourceX, sourceY and targetX and targetY

• You can declare, initialize, and change more than one variable in a for loop

for (int sourceX = 0, targetX = 0; sourceX < sourcePicture.getWidth();

sourceX++, targetX++)

• You can copy just part of a picture to another picture


Recommended