Download presentation
Presentation is loading. Please wait.
Published byPrimrose Cole Modified over 8 years ago
1
NestedLoops-Mod7-part61 Two-Dimensional Arrays and Nested Loops – part 6 Enlarge Barb Ericson Georgia Institute of Technology August 2005
2
NestedLoops-Mod7-part62 Thinking Through Scaling Up Copy each pixel in the source multiple times to the target –Source (0,0) Target (0,0) –Source (0,0) Target(1,0) –Source (1,0) Target(2,0) –Source (1,0) Target(3,0) –Source (2,0) Target(4,0) –Source (2,0) Target(5,0) –Source (0,0) Target(0,1) –Source (0,0) Target(1,1) 112233 112233 445566 445566 123 456 0 0 1 12 0 0 1 1 2 2 3 3 45
3
NestedLoops-Mod7-part63 Idea If you move through the source picture half as fast as the target picture, you’ll get 2 pixels in the target for each 1 in the source 112233 112233 445566 445566 123 456 0 0 1 12 0 0 1 1 2 2 3 3 45
4
NestedLoops-Mod7-part64 Scaling Up Algorithm Create the source picture Create a new target picture double the height and width of the source –Loop with source x starting at 0 and target x starting at 0 as long as source x< source width Increment the source x by 0.5 each time through the loop, increment the target x by 1 Loop with source y starting at 0 and target y starting at 0 as long as < source height –Increment the source y by 0.5 each time through the loop, increment the target y by 1 »Copy the color from the source to target pixel
5
NestedLoops-Mod7-part65 Can you get a pixel at position (0.5,1)? –No –You can only get pixels at whole number positions Can you increment the index number by 0.5? –int index = 0; –index = index + 0.5; –ERROR! Loss of precision, the equation on the right is now a double, it won’t fit into the variable on the left.
6
NestedLoops-Mod7-part66 How to solve the ½ pixel problem Can you do this? int index = 0; index = (int)index + 0.5; –NO –That just casts index into an integer, which it already is, then adds 0.5 so now the answer on the right is still a double
7
NestedLoops-Mod7-part67 Solving the ½ pixel problem How about: int index = 0; index = (int)(index + 0.5) –Nice idea, but that adds index+0.5 and then chops the.5 right off again. How about: double counter = 0.0; index = (int)(counter + 0.5); –OK, that works, but how to use it in a loop?
8
NestedLoops-Mod7-part68 Here’s a loop that uses a double for(double d=0; d<2; d+=0.5){ System.out.println(d); } What prints? –0.0 –0.5 –1.0 –1.5
9
NestedLoops-Mod7-part69 Here’s a loop that uses 2 doubles for(double d=0, i=0; d<2; d+=0.5, i++){ System.out.println(d+” “+i); } What prints? –0.0 0.0 –0.5 1.0 –1.0 2.0 –1.5 3.0
10
NestedLoops-Mod7-part610 Looping by halves –The counters are doubles: countSx, countSy, countTx, countTy –Loop with countSx starting at 0 and countTx starting at 0 as long as countSx < source width Increment the countSx by 0.5 each time through the loop, increment the countTx by 1 Loop with countSy starting at 0 and countTy starting at 0 as long as countSy < source height –Increment the counters y by 0.5 each time through the loop, increment the countTy by 1 »sourcex = (int) countSx ; targetx = (int) countTx; »sourcey = (int) countSy ; targety = (int) countTy; »Copy the color from the source to target pixel
11
NestedLoops-Mod7-part611 Scaling Up Exercise Write a method copyBigger to scale up the picture flower1.jpg when you copy it. You need 2 methods in your copy/duplicate class: public static void testBigger(){ –// makes the 2 pictures, the source and target –// calls copyBigger –// displays the target picture } public static void copyBigger (Picture smallPic, Picture copied){ –// makes the copy –}
12
NestedLoops-Mod7-part612 Summary You can scale a picture up –By copying the source pixels more than one time You can increment the source index by 0.5 and then cast it to integer Look at your code to shrink pictures for help with the syntax and coding. Put these methods in the same class as your copy method.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.