TWiki
>
CS111 Web
>
WebLeftBar
>
NotesS18
>
CodeS18
>
AssignmentsS18
>
Lab8s18
(2018-03-15, Main.troy)
(raw view)
E
dit
A
ttach
---++ CS 111 - PROGRAM DESIGN I, Spring 2018 ---+++ <a name="Lab_8"></a> <a name="Lab_8"></a> Lab 8 In class, we discussed an [[https://www.cs.uic.edu/bin/view/CS111/CodeS18][example (lect0308d.py)]] that rotated a picture by 90 degrees. This example shows the basic template for most position modification algorithms. Also recall the code from the [[https://www.cs.uic.edu/bin/view/CS111/CodeS18][example (lect0308d.py)]] that calculated the modified x and y coordinates based from the original x and y coordinates is as follows: <blockquote> <pre> // determine the location in the result of the rotated pixel</pre> <pre> xNew = (height-1) - ypos</pre> <pre> yNew = xpos</pre> </blockquote> 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 <b>requestIntegerInRange</b>(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. 1 - rotate an image 180 degrees in a clockwise direction. 1 - 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. <br />Here is the original image: <br /><img alt="" src="http://www.cs.uic.edu/~i101/labs/caterpillar.jpg" /> Here is the same image rotated 90 degrees clockwise: <br /><img alt="" src="http://www.cs.uic.edu/~i101/labs/caterpillarRot90.jpg" /> Here is the same picture rotated 180 degrees: <img alt="caterpillarRot180.jpg" src="http://www.cs.uic.edu/pub/CS111/Lab10f13/caterpillarRot180.jpg" /> Here is the same picture rotated 270 degrees clockwise: <img alt="caterpillarRot270.jpg" src="http://www.cs.uic.edu/pub/CS111/Lab10f13/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. We will need to determine the formulas for the clockwise rotation (the rotate90) and the counter clockwise rotation (the rotate270). Consider the following 5x3 "image", which has width of 5 and height of 3: <table border="1" cellpadding="0" cellspacing="0" id="table1"> <tbody> <tr> <td bgcolor="#ffffff" valign="top">a</td> <td bgcolor="#ffffff" valign="top">b</td> <td bgcolor="#ffffff" valign="top">c</td> <td bgcolor="#ffffff" valign="top">d</td> <td bgcolor="#ffffff" valign="top">e</td> </tr> <tr> <td bgcolor="#edf4f9" valign="top">f</td> <td bgcolor="#edf4f9" valign="top">g</td> <td bgcolor="#edf4f9" valign="top">h</td> <td bgcolor="#edf4f9" valign="top">i</td> <td bgcolor="#edf4f9" valign="top">j</td> </tr> <tr> <td bgcolor="#ffffff" valign="top">k</td> <td bgcolor="#ffffff" valign="top">l</td> <td bgcolor="#ffffff" valign="top">m</td> <td bgcolor="#ffffff" valign="top">n</td> <td bgcolor="#ffffff" valign="top">o</td> </tr> </tbody> </table> When rotated clockwise 90 degrees, it will become a picture of width 3 and of height 5: <table border="1" cellpadding="0" cellspacing="0" id="table2"> <tbody> <tr> <td bgcolor="#ffffff" valign="top">k</td> <td bgcolor="#ffffff" valign="top">f</td> <td bgcolor="#ffffff" valign="top">a</td> </tr> <tr> <td bgcolor="#edf4f9" valign="top">l</td> <td bgcolor="#edf4f9" valign="top">g</td> <td bgcolor="#edf4f9" valign="top">b</td> </tr> <tr> <td bgcolor="#ffffff" valign="top">m</td> <td bgcolor="#ffffff" valign="top">h</td> <td bgcolor="#ffffff" valign="top">c</td> </tr> <tr> <td bgcolor="#edf4f9" valign="top">n</td> <td bgcolor="#edf4f9" valign="top">i</td> <td bgcolor="#edf4f9" valign="top">d</td> </tr> <tr> <td bgcolor="#ffffff" valign="top">o</td> <td bgcolor="#ffffff" valign="top">j</td> <td bgcolor="#ffffff" valign="top">e</td> </tr> </tbody> </table> 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. <table border="1" cellpadding="0" cellspacing="0" id="table3"> <tbody> <tr><th valign="top">pixel</th><th valign="top">Original X</th><th valign="top">Original Y</th><th valign="top">Rotated X</th><th valign="top">Rotated Y</th></tr> <tr> <td align="center" valign="middle">a</td> <td align="center" valign="middle">0</td> <td align="center" valign="middle">0</td> <td bgcolor="#ffffff" valign="top"> </td> <td bgcolor="#ffffff" valign="top"> </td> </tr> <tr> <td align="center" valign="middle">b</td> <td align="center" valign="middle">1</td> <td align="center" valign="middle">0</td> <td bgcolor="#edf4f9" valign="top"> </td> <td bgcolor="#edf4f9" valign="top"> </td> </tr> <tr> <td align="center" valign="middle">c</td> <td align="center" valign="middle">2</td> <td align="center" valign="middle">0</td> <td bgcolor="#ffffff" valign="top"> </td> <td bgcolor="#ffffff" valign="top"> </td> </tr> <tr> <td align="center" valign="middle">d</td> <td align="center" valign="middle">3</td> <td align="center" valign="middle">0</td> <td bgcolor="#edf4f9" valign="top"> </td> <td bgcolor="#edf4f9" valign="top"> </td> </tr> <tr> <td align="center" valign="middle">e</td> <td align="center" valign="middle">4</td> <td align="center" valign="middle">0</td> <td bgcolor="#ffffff" valign="top"> </td> <td bgcolor="#ffffff" valign="top"> </td> </tr> <tr> <td align="center" valign="middle">f</td> <td align="center" valign="middle">0</td> <td align="center" valign="middle">1</td> <td bgcolor="#edf4f9" valign="top"> </td> <td bgcolor="#edf4f9" valign="top"> </td> </tr> <tr> <td align="center" valign="middle">g</td> <td align="center" valign="middle">1</td> <td align="center" valign="middle">1</td> <td bgcolor="#ffffff" valign="top"> </td> <td bgcolor="#ffffff" valign="top"> </td> </tr> <tr> <td align="center" valign="middle">h</td> <td align="center" valign="middle">2</td> <td align="center" valign="middle">1</td> <td bgcolor="#edf4f9" valign="top"> </td> <td bgcolor="#edf4f9" valign="top"> </td> </tr> <tr> <td align="center" valign="middle">i</td> <td align="center" valign="middle">3</td> <td align="center" valign="middle">1</td> <td bgcolor="#ffffff" valign="top"> </td> <td bgcolor="#ffffff" valign="top"> </td> </tr> <tr> <td align="center" valign="middle">...</td> <td align="center" valign="middle">...</td> <td align="center" valign="middle">...</td> <td bgcolor="#edf4f9" valign="top"> </td> <td bgcolor="#edf4f9" valign="top"> </td> </tr> <tr> <td align="center" valign="middle">m</td> <td align="center" valign="middle">2</td> <td align="center" valign="middle">2</td> <td> </td> <td> </td> </tr> <tr> <td align="center" valign="middle">n</td> <td align="center" valign="middle">3</td> <td align="center" valign="middle">2</td> <td> </td> <td> </td> </tr> <tr> <td align="center" valign="middle">o</td> <td align="center" valign="middle">4</td> <td align="center" valign="middle">2</td> <td bgcolor="#ffffff" valign="top"> </td> <td bgcolor="#ffffff" valign="top"> </td> </tr> </tbody> </table> When we fill in this table for the rotated 90 degree clockwise result we get: <table border="1" cellpadding="0" cellspacing="0" id="table3"> <tbody> <tr><th align="center" valign="top">pixel</th><th align="center" valign="top">Original X</th><th align="center" valign="top">Original Y</th><th align="center" valign="top">Rotated X</th><th align="center" valign="top">Rotated Y</th></tr> <tr> <td align="center" valign="top">a</td> <td align="center" valign="top">0</td> <td align="center" valign="top">0</td> <td align="center" valign="top">2</td> <td align="center" valign="top">0</td> </tr> <tr> <td align="center" valign="top">b</td> <td align="center" valign="top">1</td> <td align="center" valign="top">0</td> <td align="center" valign="top">2</td> <td align="center" valign="top">1</td> </tr> <tr> <td align="center" valign="top">c</td> <td align="center" valign="top">2</td> <td align="center" valign="top">0</td> <td align="center" valign="top">2</td> <td align="center" valign="top">2</td> </tr> <tr> <td align="center" valign="top">d</td> <td align="center" valign="top">3</td> <td align="center" valign="top">0</td> <td align="center" valign="top">2</td> <td align="center" valign="top">3</td> </tr> <tr> <td align="center" valign="top">e</td> <td align="center" valign="top">4</td> <td align="center" valign="top">0</td> <td align="center" valign="top">2</td> <td align="center" valign="top">4</td> </tr> <tr> <td align="center" valign="top">f</td> <td align="center" valign="top">0</td> <td align="center" valign="top">1</td> <td align="center" valign="top">1</td> <td align="center" valign="top">0</td> </tr> <tr> <td align="center" valign="top">g</td> <td align="center" valign="top">1</td> <td align="center" valign="top">1</td> <td align="center" valign="top">1</td> <td align="center" valign="top">1</td> </tr> <tr> <td align="center" valign="top">h</td> <td align="center" valign="top">2</td> <td align="center" valign="top">1</td> <td align="center" valign="top">1</td> <td align="center" valign="top">2</td> </tr> <tr> <td align="center" valign="top">i</td> <td align="center" valign="top">3</td> <td align="center" valign="top">1</td> <td align="center" valign="top">1</td> <td align="center" valign="top">3</td> </tr> <tr> <td align="center" valign="top">...</td> <td align="center" valign="top"> </td> <td align="center" valign="top"> </td> <td align="center" valign="top"> </td> <td align="center" valign="top"> </td> </tr> <tr> <td align="center">m</td> <td align="center">2</td> <td align="center">2</td> <td align="center">0</td> <td align="center">2</td> </tr> <tr> <td align="center">n</td> <td align="center">3</td> <td align="center">2</td> <td align="center">0</td> <td align="center">3</td> </tr> <tr> <td align="center" valign="top">o</td> <td align="center" valign="top">4</td> <td align="center" valign="top">2</td> <td align="center" valign="top">0</td> <td align="center" valign="top">4</td> </tr> </tbody> </table> 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) <blockquote> <pre> # determine the location for the result of picture rotated 90 degrees clockwise</pre> <pre> modX = (height - 1) - y</pre> <pre> modY = x</pre> </blockquote> 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: <table border="1" cellpadding="0" cellspacing="0" id="table4"> <tbody> <tr> <td bgcolor="#ffffff" valign="top">o</td> <td bgcolor="#ffffff" valign="top">n</td> <td bgcolor="#ffffff" valign="top">m</td> <td bgcolor="#ffffff" valign="top">l</td> <td bgcolor="#ffffff" valign="top">k</td> </tr> <tr> <td bgcolor="#edf4f9" valign="top">j</td> <td bgcolor="#edf4f9" valign="top">i</td> <td bgcolor="#edf4f9" valign="top">h</td> <td bgcolor="#edf4f9" valign="top">g</td> <td bgcolor="#edf4f9" valign="top">f</td> </tr> <tr> <td bgcolor="#ffffff" valign="top">e</td> <td bgcolor="#ffffff" valign="top">d</td> <td bgcolor="#ffffff" valign="top">c</td> <td bgcolor="#ffffff" valign="top">b</td> <td bgcolor="#ffffff" valign="top">a</td> </tr> </tbody> </table> 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: <table border="1" cellpadding="0" cellspacing="0" id="table5"> <tbody> <tr> <td bgcolor="#ffffff" valign="top">e</td> <td bgcolor="#ffffff" valign="top">j</td> <td bgcolor="#ffffff" valign="top">o</td> </tr> <tr> <td bgcolor="#edf4f9" valign="top">d</td> <td bgcolor="#edf4f9" valign="top">i</td> <td bgcolor="#edf4f9" valign="top">n</td> </tr> <tr> <td bgcolor="#ffffff" valign="top">c</td> <td bgcolor="#ffffff" valign="top">h</td> <td bgcolor="#ffffff" valign="top">m</td> </tr> <tr> <td bgcolor="#edf4f9" valign="top">b</td> <td bgcolor="#edf4f9" valign="top">g</td> <td bgcolor="#edf4f9" valign="top">l</td> </tr> <tr> <td bgcolor="#ffffff" valign="top">a</td> <td bgcolor="#ffffff" valign="top">f</td> <td bgcolor="#ffffff" valign="top">k</td> </tr> </tbody> </table> ---+++ <a name="Prompting_the_User_for_Which_Rot"></a> Prompting the User for Which Rotation to Do To get the user's input, we will use the <b>requestIntegerInRange</b>(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. <table border="1" cellpadding="0" cellspacing="0" id="table1" rules="all"> <tbody> <tr> <td bgcolor="#ffffff" valign="top"> <pre> # prompt the user for the number<br /> value = requestIntegerInRange ("Please enter an integer value between 1 and 10.", 1, 10)<br /> <br /> # display the value entered by the user<br /> print "The user entered: " , value</pre> </td> </tr> </tbody> </table> ---+++ <a name="Lab_Assignment_8"></a> Lab Assignment 8 Due: Wednesday, 3/15/2017 by 11:59 pm Write a python program named <a href="https://www.cs.uic.edu/bin/edit/CS111/NetIDLab8?topicparent=CS111.Lab8f16;nowysiwyg=0" rel="nofollow" title="NetIDLab8 (this topic does not yet exist; you can create it)">NetIDLab8</a>.py that will contain a function named main( ) that will do the following: 1 <p>Print out your name and your net-id</p> 1 Allow the user to select a picture from a file stored on the local machine using pickAFile (). 1 Prompt the user for an integer value of 1, 2 or 3 using the JES library function of requestIntegerInRange (). 1 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 1 If the user enters the value of 2, call a method that will create and return an image that has been rotated 180 degrees 1 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 1 Display the rotated image 1 Save the rotated image to a file on the local machine <br /> <p> </p> 1 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 ( ) 1 rotate180 ( ) 1 rotate270 ( ) 1 You must write your programs using good programming style which includes: <p> </p> * Good variable names * in-line commenting * header block commenting for the program and each method written<br />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. <p> </p> *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. <p> </p> ---+++ <a name="How_assignments_should_be_submit"></a> How assignments should be submitted You are to submit your program electronically using the link for Lab 8 on the Assignments Page in Blackboard.
E
dit
|
A
ttach
|
P
rint version
|
H
istory
: r2
<
r1
|
B
acklinks
|
V
iew topic
|
Ra
w
edit
|
M
ore topic actions
Topic revision: r2 - 2018-03-15 - 01:09:20 - Main.troy
CS111
Web Page Spr 18
Syllabus Spr 18
Lecture Notes Spr 18
Sample Code Spr 18
Assignments Spr 18
[edit this menu
]
Log In
CS 111 Main Page
Create New Topic
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
ABOUT US
Our Department
Recent News
Contact Us
ACADEMICS
Prospective Students
Undergraduate
CS Minor
Graduate
Courses
RESEARCH
Overview
By Faculty
Labs
PEOPLE
Faculty
Adjuncts
Staff
Students
Alumni
Copyright 2016 The Board of Trustees
of the University of Illinois.
webmaster@cs.uic.edu
WISEST
Helping Women Faculty Advance
Funded by NSF