Difference between revisions of "CSC111 Homework 6 2011"

From dftwiki3
Jump to: navigation, search
(Demo Program)
Line 6: Line 6:
 
| <bluebox>
 
| <bluebox>
  
This homework assignment is due on April Fool's Day, 4/1/10, at midnight.
+
This homework assignment is due on Nov. 1st, in the evening, at midnight.
  
 
You can work on Problem #1 in pairs.   
 
You can work on Problem #1 in pairs.   
 
Problem #2 is optional and extra-credit, and if you work on it, you must work on it on your own. 
 
  
 
</bluebox>
 
</bluebox>
Line 26: Line 24:
  
 
<source lang="python">
 
<source lang="python">
 +
# hw6a.py
 +
# D. Thiebaut
 
# This program shows two balls moving in the graphics window.
 
# This program shows two balls moving in the graphics window.
 
# One is yellow, one is magenta.  The magenta ball is not bound
 
# One is yellow, one is magenta.  The magenta ball is not bound
Line 40: Line 40:
 
   
 
   
 
def main():
 
def main():
     """Opens a graphics window 300x300 and draws a button on
+
     """Opens a graphics window 600x300 and draws two circles on it"""
    it.  Then wait for the user to click the mouse 10 times
 
    before exiting"""
 
 
     W = 600
 
     W = 600
 
     H = 300
 
     H = 300
Line 85: Line 83:
 
</source>
 
</source>
  
===Statement===
+
===Assignment===
Write a program called '''hw6a.py''' that moves two balls on the graphics screen.
+
# Modify the program hw6a.py so that the '''two''' balls bounce off the walls of the window, not just one.
# the two balls should bounce off the walls (border of the graphics window)
+
# Make the balls bounce when their side hits the walls, not their center.
# the two balls should bounce off each other.
+
# Make the two balls bounce off when they hit each other (the way pool-table balls would).  
# there should be three black boxes in the graphics window, and if any one of the two balls happens to move completely inside any one of the boxes, it should stop there.  
+
# There should be three black boxes in the graphics window, and if any one of the two balls happens to move '''completely inside''' any one of the boxes, it should stop there.  
# the program should stop and display ''click to End'' when the simulation has gone through 300 steps, or as soon as the two balls have been immobilized in boxes.
+
# the program should stop and display ''click me to quit'' when the simulation has gone through 1000 steps, '''or as soon as the two balls have been immobilized in boxes'''.
 
# there should be a fourth box, this one white, which should act as an obstacle.  Any time a ball hits the white box, it should bounce off the walls of the box, in a same way it bounces off the walls.
 
# there should be a fourth box, this one white, which should act as an obstacle.  Any time a ball hits the white box, it should bounce off the walls of the box, in a same way it bounces off the walls.
  
 
===Requirements===
 
===Requirements===
* Use boolean functions
+
* Use a list to hold the black boxes (defined by their coordinates).
* Use a list to hold the black boxes.
 
 
* You are free to select the size of the white and black boxes, but make them big enough to allow balls to hit them regularly, but not too big so as to make the balls "fall" in a black box before having time to hit a few walls or the white box.
 
* You are free to select the size of the white and black boxes, but make them big enough to allow balls to hit them regularly, but not too big so as to make the balls "fall" in a black box before having time to hit a few walls or the white box.
 
* Include your account name(s) or your first name(s) in the title of the graphics window to make it easier to identify printed screen captures of your running program:
 
* Include your account name(s) or your first name(s) in the title of the graphics window to make it easier to identify printed screen captures of your running program:
  
       win = GraphWin( W, H, "111c-xx 111c-yy" )
+
       win = GraphWin( W, H, "111a-xx's billiard program" )
  
 
===Realism===
 
===Realism===
* It is not easy nor possible with our current knowledge of Python to make the bouncing of the balls off each other realistic.  When a ball hits a wall, the rule is very nice and clear: we change the sign of either dx or dy, depending on which is perpendicular to the wall.  When the balls hit each other, both dx and dy would normally change.  The real equation for how dx and dy change is too complicated for us.  Invent your own laws of physics and  implement them in python!
+
* It is not easy nor possible with our current knowledge of Python to make the balls bounce off each other in a  realistic way.  When a ball hits a wall, the rule is very nice and clear: we change the sign of either dirX or dirY, depending on which is perpendicular to the wall.  When the balls hit each other, both dirX and dirY would normally change.  The real equation for  
 +
how dirX and dirY change is too complicated for us.  Invent your own laws of physics and  implement them in python!
  
 
===Submission===
 
===Submission===
  
 
* Make sure the header of your program contains your names and account numbers, if working in pairs, or just your name and account number if working individually.
 
* Make sure the header of your program contains your names and account numbers, if working in pairs, or just your name and account number if working individually.
 +
 
* Submit your program as follows:
 
* Submit your program as follows:
  
     submit hw8 hw8a.py
+
     rsubmit hw6 hw6a.py
  
 
===Additional Information===
 
===Additional Information===
Line 121: Line 120:
 
       return sqrt( pow( P1.getX() - P2.getX(), 2 ) + pow( P1.getY() - P2.getY(), 2 ) )
 
       return sqrt( pow( P1.getX() - P2.getX(), 2 ) + pow( P1.getY() - P2.getY(), 2 ) )
  
==Problem #2: Optional and Extra Credits==
+
* Here is a way it could be used:
  
<bluebox>
+
  P1 = Point( x1, y1 )
This part is extra credit and optional.  You cannot work on it in pairs, but you must work on it on your own.  If you submit a program that is
+
  P2 = Point( x2, y2 )
* well documented, and that
+
 
* runs flawlessly when tested,
+
  ...
you will get an additional 1/3 point on your midterm gradeSo, if you had gotten B+, this extra-credit part will bring you to A- total. 
+
   
</bluebox>
+
  if distance( P1, P2 ) > 30:
 
+
      # do something if the points are more than 30 pixels away from each other
<!--center>[[File:UnderConstruction.jpg | 150px | Under Construction]]<br />Under Construction...</center-->
+
  else
 
+
      # do something else if the points are closer than 30 pixels
* Your assignment is to write a program that will read a long string of characters which represents the results of a computer experiment.
+
   
:In this experiment, many different computers (88) are asked to compute a number and return this result to a main computer. The main computer outputs all the different answers in a long listing.  It is this listing that your program has to read, analyze, and process.
 
  
* To see what the list looks like, use getcopy to get two programs into your account:
 
 
getcopy hw8b.py
 
getcopy montecarlo.pyc
 
 
:montecarlo.pyc is a module that is used by hw8b.py.
 
 
* Run the hw8b.py program and observe its output:
 
 
'''python hw8b.py'''
 
<code><pre>
 
------------------------------
 
N= 10000000
 
pi=3.16159265359
 
Job 1 stopped: Execution time:3.073 seconds
 
------------------------------
 
N= 10000000
 
pi=3.15159265359
 
Job 2 stopped: Execution time:3.023 seconds
 
------------------------------
 
N= 10000000
 
pi=3.15159265359
 
Job 3 stopped: Execution time:3.029 seconds
 
------------------------------
 
N= 10000000
 
pi=3.14159265359
 
Job 4 stopped: Execution time:3.015 seconds
 
...
 
...
 
...
 
------------------------------
 
N= 10000000
 
pi=3.14159265359
 
Job 88 stopped: Execution time:3.021 seconds
 
</pre></code>
 
 
* 88 different results are computed and reported in 88 groups of 4 lines.  Each group starts with a line of dashes.  The next line in a group, '''N= 10000000''', is not important for us.  It just indicates that 10,000,000 iterations were performed to estimate each value of Pi.  The third line of a group,  '''pi=3.14159265359''', shows the result of  computing Pi on one of the computers.  This result is an ''estimation'' and is not exactly Pi.  The last line of each group, '''Job XX stopped: Execution time:n.nnn seconds''' indicates that the program ran on Computer #XX, and took ''n.nnn'' seconds to run.
 
 
* Your assignment is to modify hw8b.py so that it will process this collection of lines (stored in the variable '''output''' in the program hw8b.py), extract all the values of '''pi''' listed,  average them out (i.e. sum them all, then divide by the number of values), and print the average. 
 
 
* Your program should also keep track of the smallest and largest execution times reported.
 
 
* The output of your program should be something like this:
 
 
  88 valid values read
 
  average value of pi: 3.14159257298131
 
  minimum execution time recorded: 3.000 seconds
 
  maximum execution time recorded: 3.311 seconds
 
 
===Complications===
 
 
* The complications come from the fact that every so often one of the computers will crash, or will not return data.  If you run the hw8b.py program several times, you will observe these two cases from time to time.
 
 
* In the first case, when computers crash, we will get results that will be messed up, as shown below:
 
 
------------------------------
 
N= 10000000
 
pi= Job 82 crashed
 
NA seconds
 
 
:Note that the number of seconds is not available any more, and that the string '''NA''' is reported instead.
 
 
* In the second case, when computers do not return results, we simply get fewer than 88 results.
 
 
===Requirements ===
 
 
* Make your program work regardless of the errors that might happen.  Your program should not crash when the list contains corrupted results as shown above, or when the number of results is less than 88.
 
 
* Use the '''try/catch''' construct.
 
 
===Submission===
 
 
* Submit your program as follows:
 
  
  submit hw8 hw8b.py
 
  
:(You do not need to submit montecarlo.pyc)
 
 
<br />
 
<br />
 
<br /><br /><br /><br /><br /><br /><br /><br /><br />
 
<br /><br /><br /><br /><br /><br /><br /><br /><br />

Revision as of 12:38, 25 October 2011

--D. Thiebaut 13:21, 25 October 2011 (EDT)


This homework assignment is due on Nov. 1st, in the evening, at midnight.

You can work on Problem #1 in pairs.



Problem #1

Demo Program

  • Create the following graphics program, either on beowulf, or on your own computer. Make sure the graphics.py library is in the same folder as your program.
# hw6a.py
# D. Thiebaut
# This program shows two balls moving in the graphics window.
# One is yellow, one is magenta.  The magenta ball is not bound
# by the dimensions of the window, while the yellow one is and
# bounces off the edges as it moves.
# The initial location and speed of the balls are random, but such
# that the balls appear in the window, and are visible.  It is possible
# for a ball to have a speed of 0, in which case it does not move.
 
from graphics import *
import random
 
 
 
def main():
    """Opens a graphics window 600x300 and draws two circles on it"""
    W = 600
    H = 300
    win = GraphWin( "Two Moving Balls", W, H )

    # draw a yellow ball, at a random place, with a random velocity
    c1 = Circle( Point( random.randrange( 100, W-100 ),  # random X
                        random.randrange( 100, H-100 ) ),# random Y
                 random.randrange( 10, 20 ) )            # random radius
    c1.setFill( "yellow" )
    dirX1 = random.randrange( -5, 5 )  # random horizontal displacement
    dirY1 = random.randrange( -5, 5 )  # random vertical displacement
    c1.draw( win )

    # draw a magenta ball, at a random place, with a random velocity
    c2 = Circle( Point( random.randrange( 100, W-100 ),  # random X
                        random.randrange( 100, H-100 ) ),# random Y
                 random.randrange( 10, 20 ) )            # random radius
    c2.setFill( "magenta" )
    dirX2 = random.randrange( -5, 5 )  # random horizontal displacement
    dirY2 = random.randrange( -5, 5 )  # random vertical displacement
    c2.draw( win )
    
    for i in range( 1000 ):

        c1.move( dirX1, dirY1 )
        c2.move( dirX2, dirY2 )

        if c1.getCenter().getX() < 0 or c1.getCenter().getX() > W:
            dirX1 = -dirX1
        if c1.getCenter().getY() < 0 or c1.getCenter().getY() > H:
            dirY1 = -dirY1

 
    #--- wait for  one more click and close up window---
    Text( Point( W//2, H//2 ), "Click me to quit" ).draw( win )
    win.getMouse()
    win.close()
 
main()

Assignment

  1. Modify the program hw6a.py so that the two balls bounce off the walls of the window, not just one.
  2. Make the balls bounce when their side hits the walls, not their center.
  3. Make the two balls bounce off when they hit each other (the way pool-table balls would).
  4. There should be three black boxes in the graphics window, and if any one of the two balls happens to move completely inside any one of the boxes, it should stop there.
  5. the program should stop and display click me to quit when the simulation has gone through 1000 steps, or as soon as the two balls have been immobilized in boxes.
  6. there should be a fourth box, this one white, which should act as an obstacle. Any time a ball hits the white box, it should bounce off the walls of the box, in a same way it bounces off the walls.

Requirements

  • Use a list to hold the black boxes (defined by their coordinates).
  • You are free to select the size of the white and black boxes, but make them big enough to allow balls to hit them regularly, but not too big so as to make the balls "fall" in a black box before having time to hit a few walls or the white box.
  • Include your account name(s) or your first name(s) in the title of the graphics window to make it easier to identify printed screen captures of your running program:
      win = GraphWin( W, H, "111a-xx's billiard program" )

Realism

  • It is not easy nor possible with our current knowledge of Python to make the balls bounce off each other in a realistic way. When a ball hits a wall, the rule is very nice and clear: we change the sign of either dirX or dirY, depending on which is perpendicular to the wall. When the balls hit each other, both dirX and dirY would normally change. The real equation for

how dirX and dirY change is too complicated for us. Invent your own laws of physics and implement them in python!

Submission

  • Make sure the header of your program contains your names and account numbers, if working in pairs, or just your name and account number if working individually.
  • Submit your program as follows:
   rsubmit hw6 hw6a.py

Additional Information

  • You may find the following function useful for computing the distance between two graphics points:
from math import *   # this should be at the beginning of the program

def distance( P1, P2 ):
     """Computes the distance between Point P1 and Point P2.  The returned value is a float"""
     return sqrt( pow( P1.getX() - P2.getX(), 2 ) + pow( P1.getY() - P2.getY(), 2 ) )
  • Here is a way it could be used:
 P1 = Point( x1, y1 )
 P2 = Point( x2, y2 )
 
 ...

 if distance( P1, P2 ) > 30:
      # do something if the points are more than 30 pixels away from each other
 else
      # do something else if the points are closer than 30 pixels