Difference between revisions of "CSC111 Homework 4 2011"
(→Requirements) |
(→Warnings) |
||
(32 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] 12:42, 4 October 2011 (EDT) | --[[User:Thiebaut|D. Thiebaut]] 12:42, 4 October 2011 (EDT) | ||
---- | ---- | ||
− | < | + | |
− | <font | + | <br /> |
− | <br | + | __TOC__ |
− | </ | + | <br /> |
+ | <bluebox> | ||
+ | This assignment is due on the evening of Thursday Oct. 12th, at midnight (you get 2 extra days because of the break). You can work on it individually or in pair-programming mode. Make sure you write your name(s) and account number(s) in the header of your programs. | ||
+ | </bluebox> | ||
+ | <br /> | ||
+ | |||
+ | =Reference= | ||
+ | |||
+ | There is a very nice 4-page PDF written by the author of the book, on the graphics library. Make sure you read it before (or while) you work on this assignment: | ||
+ | <br /> | ||
+ | * [http://mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf graphics.pdf] | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | =<font color="red">'''Warnings'''</font>= | ||
+ | <br /> | ||
+ | (section added 10/12 after several reported strange errors generated by Idle...) | ||
+ | <br /> | ||
+ | * Make sure that when you write python program you don't give them the name of already existing modules, otherwise you will get some strange errors when running your program. | ||
+ | * For example, here are names you '''shouldn't use''' for your programs: | ||
+ | ** random.py | ||
+ | ** graphics.py (''because that's the name of the graphics module we use for doing graphics'') | ||
+ | ** sys.py | ||
+ | ** os.py | ||
+ | |||
=Mondrian= | =Mondrian= | ||
[[Image:Mondrian1.jpg|right|200px]] | [[Image:Mondrian1.jpg|right|200px]] | ||
Line 14: | Line 38: | ||
This assignment is inspired by this style and asks you to write a graphics program that will display 1000 random rectangles filled with random colors. | This assignment is inspired by this style and asks you to write a graphics program that will display 1000 random rectangles filled with random colors. | ||
− | == | + | ==Downloading the graphics module== |
+ | |||
+ | * Download the graphics module from this [http://mcsp.wartburg.edu/zelle/python/ this page]. | ||
+ | * Copy it to the same folder (directory) where you will be developing your graphics program. | ||
+ | * For this assignment, it will be easier to use Idle on the Mac or Windows. | ||
+ | |||
+ | ==Exploring the Python '''random''' module== | ||
+ | |||
===Picking an item at random in a list=== | ===Picking an item at random in a list=== | ||
* The random module is a standard module in Python. It is part of the language and can be used to generate randomness. Actually, pseudo-randomness, because all randomness generated by programs cannot be purely random, since it is always based on some mathematical algorithm. | * The random module is a standard module in Python. It is part of the language and can be used to generate randomness. Actually, pseudo-randomness, because all randomness generated by programs cannot be purely random, since it is always based on some mathematical algorithm. | ||
Line 65: | Line 96: | ||
===Generating random circles=== | ===Generating random circles=== | ||
− | * Let's generate 5 circles on a line (like a music notes on a score), and pick their placement at random. | + | * Let's generate 5 circles on a line (like a music notes on a score), and pick their placement at random. |
+ | * Type in the code below in a new program... | ||
<br /> | <br /> | ||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
+ | from graphics import * | ||
+ | import random | ||
+ | |||
def main2(): | def main2(): | ||
# define the width and height of our window (so that we can easily | # define the width and height of our window (so that we can easily | ||
Line 86: | Line 121: | ||
# draw 5 circles placed at random x positions on the line | # draw 5 circles placed at random x positions on the line | ||
+ | radius = 20 | ||
for i in range( 5 ): | for i in range( 5 ): | ||
− | + | # generate a random x position | |
x = random.randrange( 0+radius, width-radius ) | x = random.randrange( 0+radius, width-radius ) | ||
+ | # put a circle at that x, and mid height... | ||
c = Circle( Point( x, height/2 ), radius ) | c = Circle( Point( x, height/2 ), radius ) | ||
+ | # make the circle black | ||
c.setFill( "black" ) | c.setFill( "black" ) | ||
c.draw( win ) | c.draw( win ) | ||
Line 95: | Line 133: | ||
win.getMouse() | win.getMouse() | ||
win.close() | win.close() | ||
+ | |||
+ | main2() | ||
</source> | </source> | ||
Line 116: | Line 156: | ||
<br /> | <br /> | ||
− | * | + | * Below is the code to do that. Type it in and test it out! Play with it. Modify some of the numbers/colors/thicknesses to understand how the rectangle works. |
<br /> | <br /> | ||
Line 144: | Line 184: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | |||
==Your Assignment== | ==Your Assignment== | ||
Line 149: | Line 190: | ||
* Note that some shapes do not look like rectangles. It is because every time a rectangle is drawn on the window, it will overlap other rectangles and the visible part that will be left may not be exactly rectangular. | * Note that some shapes do not look like rectangles. It is because every time a rectangle is drawn on the window, it will overlap other rectangles and the visible part that will be left may not be exactly rectangular. | ||
+ | |||
===Requirements=== | ===Requirements=== | ||
+ | * Include your 111a-xx account in the title of the graphics window. This way, when I print a copy of your graphics, I will know who the printout belongs to! | ||
+ | ** For example: | ||
+ | |||
+ | win = GraphWin( "Hw4 - 111a-bz", width, height ) | ||
+ | |||
+ | |||
* Your program should wait for the user to click the mouse for it to stop. | * Your program should wait for the user to click the mouse for it to stop. | ||
* Each rectangle should have a fairly wide black border (I used 5 pixels in the example above) | * Each rectangle should have a fairly wide black border (I used 5 pixels in the example above) | ||
− | + | * Your rectangles should have width and heights that are multiples of 20 (to give them a nicer look). | |
+ | * You do not have to use the palette I have used here. Feel free to use as many colors as you want. | ||
<br /> | <br /> | ||
<center> | <center> | ||
Line 167: | Line 216: | ||
rsubmit hw4 hw4a.py | rsubmit hw4 hw4a.py | ||
+ | |||
+ | * You may also use the submit form [http://maven.smith.edu/~111a/submit4.htm here]. | ||
+ | |||
+ | =Music!= | ||
+ | <br /> | ||
+ | <center> | ||
+ | [[Image:MusicalNotes.png|300px]]<br /> | ||
+ | [[Image:pianoKeys.jpg|300px]] | ||
+ | |||
+ | </center> | ||
+ | <br /> | ||
+ | * Your assignment is to generate a graphics program with Python that is ''inspired'' by the image above. | ||
+ | * Your program should draw 7 notes on a score, at the location of your choosing (but they cannot be all on the same line). They can represent the scale or not. It can be the beginning of your favorite tune. | ||
+ | * You do not have to generate a fancy elipse for your note, just a regular circle with a nice width of, say, 3 or 5, should be fine. | ||
+ | * The notes can be filled in white or black. You decide. | ||
+ | * Your program should also represent the keys of a piano underneath the score. It is not required for the piano keys to be related to the notes in the score. You should, however, show white keys and black keys. At least 7 keys are required. For information, the picture above shows 12 keys. | ||
+ | * Your notes may have a ''stem'' attached to the ''head'', but it is not required. In other words, you can use ''half'' or ''whole'' notes. See [http://en.wikipedia.org/wiki/Note_value wikipedia] for more information. | ||
+ | * Make sure you include your account number in the name of your graphics window, please! | ||
+ | |||
+ | ==Submission== | ||
+ | * call your program hw4b.py and submit it as follows: | ||
+ | |||
+ | rsubmit hw4 hw4b.py | ||
+ | |||
+ | =Optional and Extra Credit= | ||
+ | |||
+ | * If you are in search of a definitely more challenging problem, this is for you! | ||
+ | * Same program as for the previous problem, but you must use a loop to generate the notes, and a loop to generate the keys | ||
+ | * You should use '''only one''' loop to generate both the black and the white keys! | ||
+ | |||
+ | * ''Hints'' | ||
+ | ** the MacDonals assignment should be helpful for this problem... | ||
+ | ** Another hint: if I wanted to print just the names of the colors of the 12 keys above, I could do it several different ways: | ||
+ | <br /> | ||
+ | <source lang="python"> | ||
+ | |||
+ | colors = [ "white", "black", "white", "black", "white", "black", "white", | ||
+ | "white", "black", "white", "black", "white" ] | ||
+ | |||
+ | for color in colors: | ||
+ | print( color ) | ||
+ | |||
+ | for i in range( len( colors ) ): | ||
+ | color = colors[ i ] | ||
+ | print( color ) | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | |||
+ | ==Submission== | ||
+ | |||
+ | * Call your program hw4x.py and submit it as follows: | ||
+ | |||
+ | rsubmit hw4 hw4x.py | ||
+ | |||
+ | =A Note on Grading= | ||
+ | |||
+ | * Each problem (1 and 2) will be graded separately, with a grade between A and E. | ||
+ | * Up to a point will be removed for missing or incomplete documentation. Look at the posted solutions for Homework 3 to see examples of good documentation. | ||
+ | * A program that crashes when it runs will be worth at most C. | ||
+ | * Up to 2/3 extra points will be given to imaginative designs (both for the graphics, and for the code). | ||
+ | * The two letter grades will be translated into their numeric equivalent, then averaged. A is 4, A- is 3.7, B+ is 3.3, etc... The closest letter grade to this average will be used as the homework grade. | ||
+ | * The extra credit, if done right, can bring the whole grade to a maximum of A+ |
Latest revision as of 09:53, 12 October 2011
--D. Thiebaut 12:42, 4 October 2011 (EDT)
This assignment is due on the evening of Thursday Oct. 12th, at midnight (you get 2 extra days because of the break). You can work on it individually or in pair-programming mode. Make sure you write your name(s) and account number(s) in the header of your programs.
Reference
There is a very nice 4-page PDF written by the author of the book, on the graphics library. Make sure you read it before (or while) you work on this assignment:
Warnings
(section added 10/12 after several reported strange errors generated by Idle...)
- Make sure that when you write python program you don't give them the name of already existing modules, otherwise you will get some strange errors when running your program.
- For example, here are names you shouldn't use for your programs:
- random.py
- graphics.py (because that's the name of the graphics module we use for doing graphics)
- sys.py
- os.py
Mondrian
Piet Mondrian is famous for his very geometric paintings with simple colors with mostly white, red, yellow and blue rectangles between black lines. This style has been applied to many different articles, as shown below.
This assignment is inspired by this style and asks you to write a graphics program that will display 1000 random rectangles filled with random colors.
Downloading the graphics module
- Download the graphics module from this this page.
- Copy it to the same folder (directory) where you will be developing your graphics program.
- For this assignment, it will be easier to use Idle on the Mac or Windows.
Exploring the Python random module
Picking an item at random in a list
- The random module is a standard module in Python. It is part of the language and can be used to generate randomness. Actually, pseudo-randomness, because all randomness generated by programs cannot be purely random, since it is always based on some mathematical algorithm.
- Visit the official python.org documentation on the random module, and in particular scroll down to the Examples and Recipes section. It shows many different ways to get random information.
- For example. If I wanted to pick a random color out of a set of colors, here's a way we could do it:
import random
def main():
colors = ["blue", "red" ]
for i in range( 10 ):
print( random.choice( colors ) )
main()
- I have used a for-loop just to test the choice() function several times, and verify that it picks a random color every time.
- By the way, a list of the color names that are recognized by the graphics module is available here.
- Add a few more colors to your list and run your program again. See that it picks them at random?
Generating a random number
- The Examples and Recipes section of the documentation of the random module also shows that you can also pick a random number in a range. For example, if you want an even random number between 0 and 100, included, you can do this:
random.randrange(0, 101, 2) # Even integer from 0 to 100
- Try it by typing in the program below and running it a few times:
import random
def main():
# generate 10 random even numbers between 0 and 100:
for i in range( 10 ):
x = random.randrange( 0, 101, 2 )
print( "x = ", x )
main()
Generating random circles
- Let's generate 5 circles on a line (like a music notes on a score), and pick their placement at random.
- Type in the code below in a new program...
from graphics import *
import random
def main2():
# define the width and height of our window (so that we can easily
# change the geometry if needed)
width = 800
height = 600
# open a graphics window
win = GraphWin( "Circles", width, height )
# draw a line
line =Line( Point( 10, height/2 ), Point( width-10, height/2 ) )
line.setFill( "black" )
line.setWidth( 5 )
line.draw( win )
# draw 5 circles placed at random x positions on the line
radius = 20
for i in range( 5 ):
# generate a random x position
x = random.randrange( 0+radius, width-radius )
# put a circle at that x, and mid height...
c = Circle( Point( x, height/2 ), radius )
# make the circle black
c.setFill( "black" )
c.draw( win )
win.getMouse()
win.close()
main2()
- Here's an example of the output of the program:
- Two of the circles overlap, but that's fine for our purpose.
Generating rectangles
- Let's draw a red rectangle that is 30 pixels wide, and 20 pixels high, and such that its top left corner is at Coordinates (30, 20) and the bottom right at (60, 40). Remember that in graphics mode, the top left corner is at (0, 0).
- Below is the code to do that. Type it in and test it out! Play with it. Modify some of the numbers/colors/thicknesses to understand how the rectangle works.
from graphics import *
import random
def main():
width = 200
height = 100
win = GraphWin( "RedRed", width, height )
# create 2 points for the top-left and bottom-right corners
topLeft = Point( 30, 20 )
botRight= Point( 60, 40 )
rect = Rectangle( topLeft, botRight )
rect.setFill( "red" )
rect.setWidth( 5 ) # the width of the line
rect.draw( win )
# wait for user to click the mouse on the window...
win.getMouse()
win.close()
main()
Your Assignment
- Generate a Mondrian-type image with 1000 random rectangles, located at random locations, of random widths and random heights, with a limited color palette. The image below shows an example of what your program could generate.
- Note that some shapes do not look like rectangles. It is because every time a rectangle is drawn on the window, it will overlap other rectangles and the visible part that will be left may not be exactly rectangular.
Requirements
- Include your 111a-xx account in the title of the graphics window. This way, when I print a copy of your graphics, I will know who the printout belongs to!
- For example:
win = GraphWin( "Hw4 - 111a-bz", width, height )
- Your program should wait for the user to click the mouse for it to stop.
- Each rectangle should have a fairly wide black border (I used 5 pixels in the example above)
- Your rectangles should have width and heights that are multiples of 20 (to give them a nicer look).
- You do not have to use the palette I have used here. Feel free to use as many colors as you want.
Submission
- Call your program hw4a.py and submit it as follows:
rsubmit hw4 hw4a.py
- You may also use the submit form here.
Music!
- Your assignment is to generate a graphics program with Python that is inspired by the image above.
- Your program should draw 7 notes on a score, at the location of your choosing (but they cannot be all on the same line). They can represent the scale or not. It can be the beginning of your favorite tune.
- You do not have to generate a fancy elipse for your note, just a regular circle with a nice width of, say, 3 or 5, should be fine.
- The notes can be filled in white or black. You decide.
- Your program should also represent the keys of a piano underneath the score. It is not required for the piano keys to be related to the notes in the score. You should, however, show white keys and black keys. At least 7 keys are required. For information, the picture above shows 12 keys.
- Your notes may have a stem attached to the head, but it is not required. In other words, you can use half or whole notes. See wikipedia for more information.
- Make sure you include your account number in the name of your graphics window, please!
Submission
- call your program hw4b.py and submit it as follows:
rsubmit hw4 hw4b.py
Optional and Extra Credit
- If you are in search of a definitely more challenging problem, this is for you!
- Same program as for the previous problem, but you must use a loop to generate the notes, and a loop to generate the keys
- You should use only one loop to generate both the black and the white keys!
- Hints
- the MacDonals assignment should be helpful for this problem...
- Another hint: if I wanted to print just the names of the colors of the 12 keys above, I could do it several different ways:
colors = [ "white", "black", "white", "black", "white", "black", "white",
"white", "black", "white", "black", "white" ]
for color in colors:
print( color )
for i in range( len( colors ) ):
color = colors[ i ]
print( color )
Submission
- Call your program hw4x.py and submit it as follows:
rsubmit hw4 hw4x.py
A Note on Grading
- Each problem (1 and 2) will be graded separately, with a grade between A and E.
- Up to a point will be removed for missing or incomplete documentation. Look at the posted solutions for Homework 3 to see examples of good documentation.
- A program that crashes when it runs will be worth at most C.
- Up to 2/3 extra points will be given to imaginative designs (both for the graphics, and for the code).
- The two letter grades will be translated into their numeric equivalent, then averaged. A is 4, A- is 3.7, B+ is 3.3, etc... The closest letter grade to this average will be used as the homework grade.
- The extra credit, if done right, can bring the whole grade to a maximum of A+