CS 111 - PROGRAM DESIGN I, Spring 2017

Lab 8

In class, we discussed an example (lect0309b.py) that created the mirror image of a picture. This example shows the basic template for most position modification algorithms.

Also recall the code from the example (lect0309b.py) that calculated the modified x and y coordinates based from the original x and y coordinates is as follows:

      // determine the location in the result of the mirrored pixel
       modX = (width-1) - x
       modY = y

The above formulas are very close to the formulas needed for rotating a picture by 90 degrees, 180 degrees, and 270 degrees.

For this lab, you are to write one program that will prompt the user to enter if an integer value from 1 to 3. There is a JES Library Function called requestIntegerInRange(message, min, max) that allows the user to enter in values within a specific range of integers. When the user enters:

  1. - rotate an image 90 degrees in a clockwise direction.
  2. - rotate an image 180 degrees in a clockwise direction.
  3. - rotate an image 270 degrees in the clockwise direction ( which is the same as rotating the image 90 degrees in a counter-clockwise direction).
An example of a rotated image is shown below.
Here is the original image:

Here is the same image rotated 90 degrees clockwise:

Here is the same picture rotated 180 degrees:

caterpillarRot180.jpg

Here is the same picture rotated 270 degrees clockwise:

caterpillarRot270.jpg

Some key issues to think about are

  • What is the size of the new rotated image based on the width and height of the original image? If you get a coordinate out of bounds errors, most likely, you did NOT do the following:
    • for the 90 degree rotation and the 270 degree rotation: the original width becomes the rotated height, and the original height becomes the rotated width
    • for the 180 degree rotation: the rotated width and height remain the same as the original width and height.
  • What are the X and Y coordinates of a pixel in the new rotated images based on the X and Y coordinates of the pixel in the original image?
    • for the 90 degree rotation and the 270 degree rotation: the rotated/modified X coordinate is calcutated from the original Y coordinate, and the rotated/modified Y coordinate is calculated from the original X coordinate.
    • sometimes the calculation will need to "mirror" (or reverse) the original location (see the code above for the "mirror image" algorithm).
We will need to determine the formulas for the clockwise rotation and the counter clockwise rotation.

Consider the following 5x3 "image", which has width of 5 and height of 3:

a b c d e
f g h i j
k l m n o

When rotated clockwise 90 degrees, it will become a picture of width 3 and of height 5:

k f a
l g b
m h c
n i d
o j e
We can (hopefully) notice a pattern between the original X and Y positions and the rotated X and Y positions by inspecting the following summary once it is filled in with the pixel position information.

pixelOriginal XOriginal YRotated XRotated Y
a
b
c
d
e
f
g
h
i
...
m
n
o
When we fill in this table for the rotated 90 degree clockwise result we get:

pixelOriginal XOriginal YRotated XRotated Y
a 0 0 2 0
b 1 0 2 1
c 2 0 2 2
d 3 0 2 3
e 4 0 2 4
f 0 1 1 0
g 1 1 1 1
h 2 1 1 2
i 3 1 1 3
...
m 2 2 0 2
n 3 2 0 3
o 4 2 0 4
We should notice that the Rotated Y values take on the values of the Original X. We should also notice that the Rotated X value take on the "mirrorred location" from the Original Y. Thus the code to modify the positions of the X and Y values in an picture that have been rotated 90 degrees clockwise becomes: (refer back to the original example from lecture to see how this code fits into the overall program)

      # determine the location for the result of picture rotated 90 degrees clockwise
       modX = (height - 1) - y
       modY = x

The same approach will need to be done with the image rotated 180 degrees. You should fill in the table yourself to see the relationship of the original X and Y coordinates compared to the rotated X and Y coordinates. The above image would be as follows once rotated 180 degrees:

o n m l k
j i h g f
e d c b a

The same approach will also need to be done with the image rotated 270 degrees clockwise. The above image would be as follows once rotated:

e j o
d i n
c h m
b g l
a f k

Prompting the User for Which Rotation to Do

To get the user's input, we will use the requestIntegerInRange(message, min, max) function of in JES. This function will display a "pop-up" window that will prompt the user to enter an integer value. If the user does not enter a value in the range from min to max, the prompt will re-ask the user for another value. The function takes a String parameter will be displayed as a message when the user is prompted for the input. It also takes two more parameter which specify the range the number entered by the user must be in. The following code shows this.

 # prompt the user for the number
value = requestIntegerInRange ("Please enter an integer value between 1 and 10.", 1, 10)

# display the value entered by the user
print "The user entered: " , value

Lab Assignment 8

Due: Wednesday, 3/15/2017 by 11:59 pm

Write a python program named NetIDLab8.py that will contain a function named main( ) that will do the following:

  1. Print out your name and your net-id

  2. Allow the user to select a picture from a file stored on the local machine using pickAFile ().
  3. Prompt the user for an integer value of 1, 2 or 3 using getIntNumber ().
  4. If the user enters the value of 1, call a method that will create and return an image that has been rotated 90 degrees clockwise
  5. If the user enters the value of 2, call a method that will create and return an image that has been rotated 180 degrees
  6. If the user enters the value of 3, call a method that will create and return an image that has been rotated 270 degrees clockwise
  7. Display the rotated image
  8. Save the rotated image to a file on the local machine

  9. The code that does the rotation must be written in 3 different methods. One method for each different rotation.These methods are to take the original picture as a parameter and return the rotated picture. I.E. you are to name these three methods:
    1. rotate90 ( )
    2. rotate180 ( )
    3. rotate270 ( )
  10. You must write your programs using good programming style which includes:

    • Good variable names
    • in-line commenting
    • header block commenting for the program and each method written
      Be sure to include the following with the header block comment for the program.
      • your name
      • day and time of your CS 111 lab section (i.e. Monday at 2:00)
      • A description of the project.
    • proper indentation of program statements
    • use of blank lines to separate blocks of code.

      NOTE: The code submitted for this lab must only access each pixel once for the rotation. I.E. You are not allowed to execute the code from rotate90 three times for the rotate270 code.

How assignments should be submitted

You are to submit your program electronically using the link for Lab 8 on the Assignments Page in Blackboard.

Topic revision: r1 - 2017-03-10 - 21:11:20 - Main.troy
 
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
WISEST
Helping Women Faculty Advance
Funded by NSF