CM151: Lab 4 -- Graphics Fun!


Lab Goal:

Learn to work with the graphics tools provided with Python. Oh, and have fun!

You are encouraged to work in groups of two (one submission per pair of students).

You should make use of your knowledge of functions: in both lab programs, you'll want to have a main function and at least one helper function.


graphics.py:

To have access to this module, you need to down load the file and place it in your lab4 directory.

  1. You can get the file from here
  2. Place this file in the same location as your program you are developing.
  3. Run the following sequence to make sure you can access it:
    				from graphics import *
    				win = GraphWin("My Test Window")
    				circ = Circle(Point(30,30), 20)
    				circ.draw(win)
    			

idle3 wants you to save your program in the same directory as graphics.py stored. For example, you could save both the graphics.py and your lab .py files to a directory under cm151 called lab4.

Possibly Useful: The graphics.py file contains a method test. You can run test() to get an idea of what types of graphics you can easily do. Also, you can open the graphics.py file and look at the test method (at the bottom of the file). This will give you a bunch of examples showing you how to use the graphics package. In addition to the book (in particular, pages 108-114), test( ) may provide hints about how to accomplish this lab.


Assignment

  1. Circle Intersection

    Write a program that computes the intersection of a circle with a horizontal line and displays the information textually and graphically.

    Input: Radius of the circle and the y-intercept of the line.

    Output: Create a window with a height and width of 600, ranging from (-10, -10) to (10,10). Draw a circle centered at (0, 0) with the given radius. Draw a horizontal line across the window with the given y-intercept. Draw the points of intersection in red. Print out the x values of the points of intersection.

    You can assume that an intersection exists for the input provided. (If an intersection doesn't exist, it's OK if your program crashes.) You should end your program by asking for user input before closing the window you created.

    Note: you may find the str function useful when displaying the intersection text. It takes in a number as a parameter, and returns a string that represents the same number:
    >>> str(5)
    '5'
    >>>
    
    Formula:


  2. Line Segment Information

    This program allows the user to draw a line segment and then displays some graphical and textual information about the line segment.

    Input: Two mouse clicks for the end points of the line segment

    Output: Draw the midpoint of the segment in cyan. Draw the line. Print the length and the slope of the line (either as a Text object or to the terminal).

    Formula:

  3. Drawing

    Optional problem (nothing to hand in).

    Input: The user enters the number of points s/he will use via terminal and enters a color (represented as a string). The user then clicks screen that number of times.

    Output: Lines between these points, as the user clicks, in the specified color.


Submission

Once you have finished your two programs, save them to the courses Moodle site.