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

Info icon This 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.
Image of page 1

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

View Full Document Right 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?
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
Image of page 3

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

View Full Document Right 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
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
Image of page 5

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

View Full Document Right 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++) {
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()); } } }
Image of page 7

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

View Full Document Right 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:
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?
Image of page 9

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern