nestedloops-part31 nested loops – part 3 barb ericson georgia institute of technology nov 2009

22
NestedLoops-part3 1 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

Upload: mervyn-harper

Post on 24-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 1

Nested Loops – part 3

Barb Ericson

Georgia Institute of Technology

Nov 2009

Page 2: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 2

Learning Goals

• Understand at a conceptual and practical level– What makes a good method?– How to write more general methods?

• By adding parameters to the method

– How to rewrite methods?• To reduce copied code

– How to blend two pictures together• Like blending two sounds

Page 3: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 3

What makes a Good Method?

• A method should do one and only one thing– Accomplish some task– The name should tell you what it does

• A method can call other methods to do some of the work– Procedural decomposition

• We shouldn’t copy code between methods– We should make general methods that are reusable

• A method should be in the class that has the data the method is working on

Page 4: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 4

Where the last two methods general?

• We specified the file to copy from in the method– Meaning we would need to change the

method – or make another method – to copy a different picture

Page 5: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 5

General Copy Algorithm

• Create a method that copies pixels from a passed source picture– Giving a start x and y and end x and y for the

source picture• If the start x and y and end x and y cover the entire

picture then the whole picture will be copied• If the start x and y and end x and y are part of the

picture then cropping will occur

– To the current picture object with a target start x and target start y

• If the start x and y are 0 then it copies to the upper left corner

Page 6: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 6

General Copy Algorithm

• Loop through the x values between xStart and xEnd

• Loop through the y values between yStart and yEnd

• Get the pixel from the source picture for the current x and y values

• Get the pixel from the target picture for the targetStartX + x and targetStartY + y values

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

Page 7: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 7

General Copy Methodpublic void copy(Picture sourcePicture, int startX, int startY, int endX, int endY, int targetStartX, int targetStartY) { Pixel sourcePixel = null; Pixel targetPixel = null;

// loop through the x values for (int x = startX, tx = targetStartX; x < endX; x++, tx++) { // loop through the y values for (int y = startY, ty = targetStartY; y < endY; y++, ty++) {

Page 8: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 8

General Copy Method - Continued // copy the source color to the target color sourcePixel = sourcePicture.getPixel(x,y); targetPixel = this.getPixel(tx,ty); targetPixel.setColor(sourcePixel.getColor()); } } }

Page 9: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 9

Creating a Collage

• We can use the general copy method to make it easier to create an image collage.

Page 10: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 10

Collage Method /** * Method to copy two flowers in a pattern to the * top of the current picture */public void copyFlowersBetter(){ // create the flower pictures Picture flower1Picture = new Picture(FileChooser.getMediaPath("flower1.jpg")); Picture flower2Picture = new Picture(FileChooser.getMediaPath("flower2.jpg")); // copy the first flower picture this.copyPicture(flower1Picture,0);

Page 11: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 11

Collage Method - continued // copy the flower2 picture starting with x = 100 this.copyPicture(flower2Picture,100); // copy the flower1 negated to x = 200 in the canvas flower1Picture.negate() ; this.copyPicture(flower1Picture,200); /* clear the blue in flower 2 picture and * add at x=300 in the canvas */ flower2Picture.clearBlue(); this.copyPicture(flower2Picture,300); // copy the negated flower 1 to x=400 this.copyPicture(flower1Picture,400);}

Page 12: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 12

Challenge

• Rewrite your collage method to use the general copy method

Page 13: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 13

Blend Pictures

• If we want to blend two pictures we need to take 50% of the red from one picture and add it to 50% of the red from the other picture– And do the same for

blue and green

Page 14: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 14

Blend Pictures Algorithm• Create the two pictures to blend• Copy the pixels from the first part of picture1

– First 150 columns from picture1• X loops from 0 and stops when equal to 150• Y loops from 0 and stops when equal to the picture1 height

• Copy the rest of the columns from picture1 blended with the pixels from picture2– Set the color to a new color that is a combination of half of the

red from each pictures, half of the green from each picture and half of the blue from each picture

– X loops from 150 to the width of picture1– Y loops from 0 to the height of picture1

• Copy the rest of the pixels from picture2

Page 15: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 15

Blend Pictures Methodpublic void blendPictures() { // create the sister pictures Picture katiePicture = new Picture(FileChooser.getMediaPath("KatieFancy.jpg")); Picture jennyPicture = new Picture(FileChooser.getMediaPath("JenParty.jpg")); // declare the source and target pixel variables Pixel katiePixel = null; Pixel jennyPixel = null; Pixel targetPixel = null;

/* declare the target x and source x since we will need * the values after the for loop */ int sourceX = 0; int targetX = 0;

Page 16: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 16

blendPictures() continued // copy the first 150 pixels of katie to the canvas for (; sourceX < 150; sourceX++, targetX++) { for (int sourceY=0, targetY=0; sourceY < katiePicture.getHeight(); sourceY++, targetY++) { katiePixel = katiePicture.getPixel (sourceX,sourceY) ; targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(katiePixel.getColor()); } }

Page 17: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 17

blendPictures() continued /* copy 50% of katie and 50% of jenny till * the end of katie’s width */ for (; sourceX < katiePicture.getWidth(); sourceX++, targetX++) { for (int sourceY=0,targetY=0; sourceY < katiePicture.getHeight(); sourceY++, targetY++) { katiePixel = katiePicture.getPixel(sourceX,sourceY); jennyPixel = jennyPicture.getPixel(sourceX - 150,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor( new Color((int) (katiePixel.getRed() * 0.5 + jennyPixel.getRed() * 0.5), (int) (katiePixel.getGreen() * 0.5 + jennyPixel.getGreen() * 0.5), (int) (katiePixel.getBlue() * 0.5 + jennyPixel.getBlue() * 0.5))); } }

Page 18: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 18

blendPictures() continued // copy the rest of Jenny sourceX = sourceX - 150; for (; sourceX < jennyPicture.getWidth(); sourceX++, targetX++) { for (int sourceY = 0, targetY = 0; sourceY < jennyPicture.getHeight(); sourceY++, targetY++) { jennyPixel = jennyPicture.getPixel(sourceX,sourceY); targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(jennyPixel.getColor()); } }}

Page 19: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 19

Trying out blendPictures()

> String fileName = FileChooser.getMediaPath("640x480.jpg");

> Picture picture = new Picture(fileName);

> picture.blendPictures();

> picture.show();

Page 20: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 20

For Loop Syntax

• The general for loop syntax is:for (init area ; continuation test; change area) {

• Each part of this loop is optional– But the semicolons are required

• You can only have 2 semicolons

• We didn't put anything in the init area since we wanted to keep using the values of sourceX and targetX

Page 21: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 21

Challenge

• Write a method to blend two pictures together – And only use 25% of one picture's color

added to 75% of the other picture's color

• Also blend the entire pictures together– Best to use two pictures of the same size

Page 22: NestedLoops-part31 Nested Loops – part 3 Barb Ericson Georgia Institute of Technology Nov 2009

NestedLoops-part3 22

Summary

• To copy part of one picture to another– Change the start and end conditions in the for loop

• A good method should do one and only one thing

• Use parameters to make methods more reusable

• Don’t copy code from one method to another– Create a general method instead– And call the general method from other methods

• You can blend pictures together