Unformatted text preview: 10/26/2007 Midterm, Assignment ECS 10
10/26 More questions on first midterm material will appear on next midterm, final. Current assignment is not really easy. Plan on getting help if you need it. Get to lab hours it EARLY next week. Graphics
Not a standard part of Python Need to include a module to get graphics functions. Ours is in O i i ecs10graphics07.py 10 hi 07 Importing
New feature of import command: import ecs10graphics07 as gr import ecs10graphics07 as gr Graphics functions are gr.line() rather than gr.line() ecs10graphics07.line(). Why do we need the prefix at all? Importing lots of modules, if two of them have functions named line() we need to be able to tell the difference. The Window
Begin any graphics program with: gr.begin_graphics() This creates the program that will display the graphics window. We can call the function with arguments to tailor the window.
gr.begin_graphics(400,600,gr.Color.pink,"Picture") More on Arguments
Not every language lets you call functions with or without arguments. Only some Python functions allow it. Sometimes you can specify arguments by name S i if b instead of order. Order of the arguments tells Python which is which. 1 10/26/2007 How do you find out...
What the arguments to a function are? What functions are available in a module? Look for documentation. Documentation for ecs10graphics07 is on the Web page, h i 10 hi 07 i h W b hanging off the page for the assignment. After importing the module, you can use dir() to get a list of what's in it: dir(gr) How do you find out...
Find out about specific functions with help() help(gr.begin_graphics) 1. 2. The display/animation loop
alive = True while alive: alive = gr.sleep(.05) Our Python program is running in one window. It needs to stop and let the window with the graphics in it run (so it can display itself, respond to the user killing it, whatever). This gr.sleep() command does this. gr.sleep() gr.sleep()
gr.sleep() gr.sleep() pauses this program to allow graphics window to be moved, killed, etc normal Windows behavior. Argument is number of seconds to pause Here, pause. Here 5/100. This window and the graphics window are taking turns running. If window is killed while the program is asleep, sleep returns False. Otherwise returns True. A big X
Let's draw something gr.set_Color(gr.Color.purple) Picks up the purple crayon gr.line(200,25,200,575,lineWidth=6) Draws the line from point (200,25) to point (200,575). Could specify (x,y) with floats as well. (x,y)
500 The window 0 0 500 2 10/26/2007 Points
A point in the window is a place in the window described by its x and y. (25,25) is in the lower left, (575,575) is the upper right. right Since a point is an ordered list of two integers (x and y), it makes sense to represent a point with a list: point = [25,25] Points on a circle
To make a star, you need to produce points on a circle (the points of the star). How do you produce such points? Geometry of a circle: G f i l Imagine a stick (the radius) sweeping out the circle as it rotates around the center. One point on circle for each angle.
radius angle center Points on a circle
(x,y) is a point on the x,y) circle. (cx,cy) is the center. cx,cy) a i the angle (0-2 Pi) is h l (0Trigonometry! Why did you sleep through that class? x = cx + radius * cos(a) cos(a) y = cy + radius * sin(a) sin(a) How to get sin() and cos()? cos()?
The math module! import math Then you can call: math.sin(a) math.cos(a) a should be a float, between 0 and 2*math.pi radius angle center Draw some points on a circle Take project in small steps
Draw points of a 5 pointed star as little circles. Store the points in a list, then draw them as circles. Draw lines connecting each pair of consecutive D li i h i f i points in the list (make a pentagon). Change how you make the points so that the list is in the order you want for the star. Get user input and handle other numbers of points. 3 ...
View Full Document
- Fall '07
- graphics window, radius angle center