cs026_7_Part3

cs026_7_Part3 - Topic 6 Modifying Pixels in a Matrix Part...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Topic 6 Modifying Pixels in a Matrix Part 3 (Rotation, Scaling) Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared by B. Ericson.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Rotating Pictures • Sometimes we may want to rotate a picture to the right or to the left • Which way has the image of the red motorcycle been rotated here?
Background image of page 2
3 Left Rotation • To rotate an image 90 degrees to the left, we still copy all the pixels, but they go to different locations in the target • (0, 0 ) goes to ( 0 ,2) (1, 0 ) goes to ( 0 ,1) (2, 0 ) goes to ( 0 ,0) (0, 1 ) goes to ( 1 ,2) (1, 1 ) goes to ( 1 ,1) (2, 1 ) goes to ( 1 ,0) • What happens to the row values? • They go to the columns : target x = source y (2,1) (1,1) (0,1) (2,0) (1,0) (0,0) (0,1) here (0,0) here (1,1) here (1,0) here (2,1) here (2,0) here 0 1 2 0 1 0 0 1 1 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Left Rotation • What happens to the column values? ( 0 ,0) goes to (0, 2 ) ( 1 ,0) goes to (0, 1) ( 2 ,0) goes to (0, 0) ( 0 ,1) goes to (1, 2 ) ( 1 ,1) goes to (1, 1 ) ( 2 ,1) goes to (1, 0 ) target y = source width –1 – source x (2,1) (1,1) (0,1) (2,0) (1,0) (0,0) (0,1) here (0,0) here (1,1) here (1,0) here (2,1) here (2,0) here 0 1 2 0 1 0 0 1 1 2
Background image of page 4
5 Left Rotation Method • We will now write a method to copy a picture (the source picture) to the current picture (the target picture) while rotating it to the left 90 degrees • Note that the source picture itself will not be changed • The method header will be public void copyLeftRotation(Picture sourcePicture) • Algorithm: • Loop through the source x • Loop through the source y • Get the source pixel at the x and y values • Get the target pixel whose x = the source y y = (the source picture width – 1 – source x) • Copy the color from the source pixel to the target pixel
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Left Rotation Method public void copyLeftRotation(Picture sourcePicture) { Pixel sourcePixel = null; Pixel targetPixel = null; int targetX, targetY = 0; // loop through the columns for (int sourceX = 0; sourceX < sourcePicture.getWidth(); sourceX++) {
Background image of page 6
7 Left Rotation Method (continued) // loop through the rows for (int sourceY = 0; sourceY < sourcePicture.getHeight(); sourceY++) { // set the target pixel color to the source pixel color sourcePixel = sourcePicture.getPixel(sourceX,sourceY); targetX = sourceY; targetY = sourcePicture.getWidth() – 1 – sourceX; targetPixel = this.getPixel(targetX,targetY); targetPixel.setColor(sourcePixel.getColor()); } } }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Result of Left Rotation • Here is the image in redMotorcycle.jpg, and the result of rotating it to the left 90 degrees:
Background image of page 8
9 Right Rotation • To rotate an image 90 degrees to the right: ( 0 ,0) goes to (1, 0 ) ( 1, 0) goes to (1, 1 ) ( 2 ,0) goes to (1, 2 ) ( 0 ,1) goes to (0, 0 ) ( 1 ,1) goes to (0, 1 ) ( 2, 1) goes to (0, 2 ) • What happens to the column values? • They go to the
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/20/2009 for the course COMP SCI 026 taught by Professor Unknown during the Spring '07 term at UWO.

Page1 / 33

cs026_7_Part3 - Topic 6 Modifying Pixels in a Matrix Part...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online