160 likes | 308 Views
Creative Commons Attribution Non-Commercial Share Alike License. http://creativecommons.org/licenses/by-nc-sa/3.0/ Original Developer: Beth Simon, 2009 bsimon@cs.ucsd.edu. CSE8A Lecture 11. Read next class: read pg 152-162 Finish PSA3 and do an interview!
E N D
Creative Commons AttributionNon-Commercial Share Alike License • http://creativecommons.org/licenses/by-nc-sa/3.0/ • Original Developer: Beth Simon, 2009bsimon@cs.ucsd.edu
CSE8A Lecture 11 • Read next class: read pg 152-162 • Finish PSA3 and do an interview! • Check out the cool stuff on the wiki and post! • ANNOUNCEMENT: • Society of Hispanic Professional Engineers Meeting • TONIGHT: 7-8pm! • EBU1 Room 4140 (Second Floor) • FREE PIZZA! • http://shpe.ucsd.edu
Discussion in Groups • We only do this because it’s been shown to improve learning. • Discussion is critical: prepare and JUST DO IT. • Also, you only get clicker points if your group votes the same thing… • Starting now (since some people didn’t understand) • We work HARD to give you the best learning experience • We expect your honest effort as well
Discussion Group: Rotating Leaders • Each member of the group should be ONE icon. • Each day in class, we’ll have one person “leading” your discussion group. • We’ll announce it at the beginning of class • If you have only 4 people, one icon will be “wild” and anyone can lead discussion that day
By the end of today’s class you should be able to… • LG22: Read and code nested loops to perform mirroring (and related) transformations using nested loops. • LG23: Select an appropriate constructor method for “making a new Picture” • LG24: Employ the technique of drawing examples to explore various cases with 2-D array manipulation. • LG25: Read, trace, and write code that performs a transformation over a restricted set of pixels (like a “box” of pixels).
Match the scenario to the constructor call (we’ll vote for each scenario) Scenario Call Picture p=new Picture(); Picture p=new Picture(“filename.jpg”); Picture p=new Picture(other); Picture p=new Picture(aNum,bNum); • Create a picture from a specific file • Create a picture that is a copy of another picture • Create a picture of width X and height Y • Create a picture of the same width and height as another picture
Code to Mirror Around Vertical Axis:From Left to Right int mirrorPt = getWidth()/2; Pixel leftP, rightP; for (int y = 0; y < getHeight); y++) { for (int x = 0; x < mirrorPt; x++) { leftP = getPixel(x,y); rightP = getPixel(getWidth()-1-x,y); rightP.setColor(leftP.getColor()); } • Which of the following is the BEST answer: A) y increases faster than x B) x increases faster than y C) x and y increase together, in step D) x increases for a while, then y increases once, then x restarts and increases again E) Y increases for a while, then x increases once, then y restarts and increases again
Mirroring Around Vertical Axis:Left to Right • What are the parameter values we use to index leftPixel and rightPixel for the first three iterations of the loop? (assume picture has a height = 50 and width = 100) int mirrorPt = getWidth()/2; Pixel leftP, rightP; for (int y = 0; y < getHeight); y++) { for (int x = 0; x < mirrorPt; x++) { leftP = getPixel(x,y); rightP = getPixel(getWidth()-1-x,y); rightP.setColor(leftP.getColor()); } }
How do you figure these kinds of questions out? • Answer: Draw a diagram • imagine “beginning” and “answer” • Draw arrows to show how to get from beginning to answer • Then fill in numbers in order, write loops to create those numbers
Mirroring Even pixels versus Odd pixels int mirrorPt = getWidth()/2; ... for (int x = 0; x < mirrorPt; x++)
What is the order of topP and bottomP coords for mirroring around horizontal axis? • Give first 4 iterations Does it matter bottom into top Or top into bottom? NO What would assignment statements be?
Review: What does this code do? ISOMORPHIC don’t show results • Hint, you probably need to trace the getPixel index values. int magic = getWidth()/2; Pixel foo, bar; for(int y = 0; y < getHeight(); y++) { int countingDown = getWidth()-1; for(int x = 0; x < magic; x++) { foo = getPixel(x,y); bar = getPixel(countingDown,y); bar.setColor(foo.getColor()); countingDown--; } } Copies top half into bottom half not mirrored. Copies left half into right half not mirrored. Mirrors around vertical axis, left into right Mirrors around horizontal axis, top into bottom Some other bizarre transformation
Practice for the quiz: • Modify the code to do… (choose the various options) int magic = getWidth()/2; Pixel foo, bar; for(int y = 0; y < getHeight(); y++) { int countingDown = getWidth()-1; for(int x = 0; x < magic; x++) { foo = getPixel(x,y); bar = getPixel(countingDown,y); bar.setColor(foo.getColor()); countingDown--; } } Copies top half into bottom half not mirrored. Copies left half into right half not mirrored. Mirrors around vertical axis, left into right Mirrors around horizontal axis, top into bottom Some other bizarre transformation