Question: Lab 5 Chapter 4 (1) You will complete this lab with Python 3.x.x. (2) You will create two .pyw files and run/test them successfully. (In
Lab 5 Chapter 4
(1) You will complete this lab with Python 3.x.x.
(2) You will create two .pyw files and run/test them successfully. (In order to run them successfully, they need to be saved within the new folder you created for this Chapter 4, which is named as CS114_Graphics_Files, because all .pyw file we wrote for THIS CHAPTER 4 will import the graphics.py).
(3) You will submit a .txt file with 2 QUESTION, and 2 SOURCE CODE. (No RUNNING RESULTs portion for this lab).
(4) Instead of the regular running results, you will screenshot two graphics and submit them as ATTACHMENTS.
(5) Screen shot tool on PC is called Snipping Tool; screen shot method on Mac is: press Command + Shift + 4 and drag a box around the screen portion you wish to capture.
PORTION 1: An archery target consists of a central circle of yellow surrounded by concentric rings of red, blue, black and white. Each ring has the same width, which is the same as the radius of the yellow circle. Write a program that draws such a target. Objects drawn later will appear on top of objects drawn earlier.
Radius of yellow = 50 pixels Width of red, blue, black and white = 50 pixels each
1. Give a name for your file, archery.pyw, within program comments 2. In comments, add your name and your CS 114 section, and the purpose of the program, An archery target 3. We need to import the graphics library so type import graphics 4. Then you need to tell it to use the graphics library so type from graphics import * 5. Create a main function definition: def main(): 6. Create a window: win = GraphWin(Archery Target, 600, 600) #This is creating a window that is 600 pixels tall and 600 pixels wide win.setBackground(gray) #Sets the background color of the window to gray center= Point(300,300) #establishing the center point for all of the circles 7. Create the first outer circle (white) c1 = Circle(center, 250) # This creates a circle starting at the center with a radius of 250 c1.setFill(white) #For the c1 circle that we have created, fill this circle with white. c1.draw(win) # Draw c1 onto the window 8. Create the remaining circles, with each circle having a radius one smaller than the next (e.g. black = 200, blue = 150, etc.) using the same method as in 7. 9. Have the program hold until you click the mouse win.getMouse() 10. Close the window win.close() 11. Dont forget to call the main function main()
PORTION 2:
Write a program (save as draw_line.pyw) that allows the user to draw a line segment and then displays some graphical and textual information about the line segment. It should include the following: Input: The two endpoints of the line segment Output: Draw the midpoint of the line segment in red. Draw the line. Print the length and the slope of the line.
1. Add the appropriate comments to the top of the page 2. Import math and graphics 3. In the main function: a. Create a window that is 400 pixels tall and 400 pixels wide b. Set the coordinates of the window to -10, -10, 10, 10 win.setCoords(-10, -10, 10, 10) c. Display a message asking the user to click on two points. msg = Text(Point(0, -9.5), Click on endpoints of a line segment.) msg.draw(win) d. Get point one p1 = win.getMouse() e. Get point two in the same manner f. Draw a line from those two points given line = Line(p1,p2) line.draw(win) g. Create a circle which marks the center of the line mark = Circle(line.getCenter(), 0.15) mark.setFill(red) mark.draw(win) h. Now we need to calculate the length and slope of the line dx = p2.getX() - p1.getX() dy = p2.getY() - p1.getY() slope = float(dy)/dx length = math.sqrt(dx*dx + dy*dy) i. Output the results of length and slope msg.setText("Length: %0.2f Slope: %0.2f" % (length, slope)) j. Have the program hold until you click the mouse and then close the window. win.getMouse() win.close() k. Remember to call the main function
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
