Due by 11:59pm Tuesday, September 26
Using IDLE, create a new blank file named assign3.py and put all of your program definitions for this assignment in the file, including your definitions from lab. You are free to use any of the code that we discussed in class as a basis or guide for writing your programs.
Finish the following programs from Lab 3 and cut and paste their code definitions into your assign3.py file:
The path() program below uses the setCoords method to specify a new coordinate range from -10.0 to 10.0 for both the x- and y-axes. This causes the y-axis to start from -10.0 at the bottom of the window and increase going up. This means the origin (0, 0) is now located at the center of the window, rather than the upper left corner, since each axis goes from -10.0 to 10.0.
def path(): howMany = eval(input("How many line segments? ")) win = GraphWin("Path", 300, 300) win.setCoords(-10, -10, 10, 10) # xmin, ymin, xmax, ymax # draw a big blue point in the center of the window center = Circle(Point(0, 0), 0.3) center.setFill("blue") center.draw(win) # more code goes here
Finish the program so that each time
the user clicks on a point, a new line segment is drawn from the previous
point to the new point (starting from the origin in the case of the first
point clicked). Each time a new line segment is added to the path, the
program should report the total length of the path so far, rounded to
three decimal places, using the distance formula (shown below) to calculate
the length of each new line segment.
For example, after clicking on five different points, the output of the program might look something like this:
>>> path() How many line segments? 5 Total length of the path is now 7.535 Total length of the path is now 16.442 Total length of the path is now 23.917 Total length of the path is now 32.947 Total length of the path is now 41.474
Write a program called drawgrid() to draw a grid in a 500 × 500 pixel square graphics window. The program should ask the user how many grid cells to display along a side. It should then use a for-loop to draw the vertical grid lines, and another for-loop to draw the horizontal grid lines. Some examples are shown below:
>>> drawgrid() How many grid cells on a side? 3 |
>>> drawgrid() How many grid cells on a side? 5 |
>>> drawgrid() How many grid cells on a side? 30 |
Write a program called face() that draws some sort of face. Be creative! To make drawing the features of your face easier, you may want to use the setCoords method to adjust the coordinates of the window. Another useful method is clone, which makes an identical copy of a shape (initially in an undrawn state). You can clone an existing shape, move it to a different location, and then draw it in the window. This can simplify drawing symmetrical features, such as eyes or ears (see pages 94-96 in the textbook for more details). For inspiration, here are a few faces from students who took the course in previous years.
You should finish the other problems first before working on these.
Write a program called parabola() that draws a smooth graph of the parabola y = x2 in a square graphics window, as shown below. Hint: divide the x-axis into 100 equally spaced intervals, and use a loop to compute the x and y values at each interval. To draw the line segments, keep track of both the previous point and the current point as you go through the loop. This will be easier if you use the setCoords method to define your own coordinate axis ranges.
Write a program called waves() that draws a smooth graph of the functions sin(x) and cos(x), where x ranges from -2π to 2π, and y ranges from -1 to +1, as shown below. Make your sine curve red and your cosine curve blue.
Improve parabola() or waves() by having them draw tic-marks along the x-axis and y-axis, labeled appropriately.
Write a program called animation() that draws some type of animated scene or object(s). Be creative! Remember that you can use the time.sleep(seconds) function available in the time module to temporarily pause program execution, in order to more precisely control the timing of events.
Save all of your program definitions in a single Python file called assign3.py. Make sure to include your name and the assignment number in a comment at the top of your file. Submit your file electronically using the Homework Upload Site. Please DO NOT email your file to me.
If you have questions about anything, don't hesitate to ask!