Two-Dimensional Arrays and Nested
Loops – part 6
Enlarge
Barb Ericson
Georgia Institute of Technology
August 2005
NestedLoops-Mod7-part6
1
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)
0
1
0
0
1
2
1
4
2
5
3
6
1
2
3
4
5
0
1 1 2 2 3 3
1
1 1 2 2 3 3
2
4 4 5 5 6 6
3
4 4 5 5 6 6
NestedLoops-Mod7-part6
2
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
0
1
0
0
1
2
1
4
2
5
3
6
1
2
3
4
5
0
1 1 2 2 3 3
1
1 1 2 2 3 3
2
4 4 5 5 6 6
3
4 4 5 5 6 6
NestedLoops-Mod7-part6
3
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
NestedLoops-Mod7-part6
4
• 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.
NestedLoops-Mod7-part6
5
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
NestedLoops-Mod7-part6
6
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?
NestedLoops-Mod7-part6
7
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
NestedLoops-Mod7-part6
8
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
NestedLoops-Mod7-part6
9
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
NestedLoops-Mod7-part6
10
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
– }
NestedLoops-Mod7-part6
11
Put these methods
in the same class
as your copy
method.
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.
NestedLoops-Mod7-part6
12
© Copyright 2026 Paperzz