110 likes | 238 Views
CSC 160 Computer Programming for Non-Majors Section 1.2: Drawing a UFO. Prof. Adam M. Wittenstein adamwittenstein@adelphi.edu http://www.adelphi.edu/~wittensa/csc160/. So far …. We can (write a program to): - compute the UFO’s location But we cannot (write a program to):
E N D
CSC 160Computer Programmingfor Non-MajorsSection 1.2: Drawing a UFO Prof. Adam M. Wittenstein adamwittenstein@adelphi.edu http://www.adelphi.edu/~wittensa/csc160/
So far … • We can (write a program to): - compute the UFO’s location • But we cannot (write a program to): - create a canvas or - draw the UFO on it
Why use teachpacks? • Since this is a non-majors course, we will leave out some of the details and nitty-gritty of programming. • For example, instead of writing a program to create a canvas and a picture, we will use the program someone else wrote. • The authors of the textbook wrote this program, and many others, for students to use in the teachpacks. • They provide tools you don’t know how to write yet, but you can use them • Analogy: you can drive a car without knowing how to build one
Teachpacks • To use the programs, you need to install the file the author saved it on. That file is called a “teachpack”. (often called “libraries”) • This teachpack can be found on the web at: www.ccs.neu.edu/home/matthias/HtDP/Extended/simple-draw.ss • Install to the folder C:\ProgramFiles\PLT\Teachpacks • To install a teachpack to your DrScheme file: Language -> Add Teachpack -> find it • In the real world, the work of a computer programmer is used by other programmers. • In this course, the teachpacks will simulate the work of the “other computer programmer”.
Our first teachpack: simple-draw.ss This file includes four programs: • (start width height) creates a canvas of width × height pixels; • (draw-solid-disk0 x y r c), which draws a colored disk at position (x,y), with radius r and color c; • (draw-solid-rect0 x y w h c), which draws a colored rectangle at position (x,y), of w × h pixels, and color c; • (draw-solid-line0 x0 y0 x1 y1 c), which draws a colored line of color c between the two positions (x0,y0) and (x1,y1).
Using the programs • Notice that I have not given you the actual computer code for the program. • This means I have not shown you how this programs are defined, and I don’t plan to. • But we will still be able to use the programs, because the code is in the file that we will install. • Most of the variables will be replaced with numbers. We saw how to do this last time.
Replacing variables with colors • To replace a variable with a number, we change (X t) to (X 20). That is, we replace the variable t with the number 20. • How would we replace a variable with a color? • We could assign numbers to each color, but this will get messy in a hurry. • Luckily, the teachpack also includes the names of some common colors like: 'white, 'yellow, 'red, 'blue, 'green, and 'black. • Suppose the program X used colors instead of numbers. Then (X t) would become (X ‘white).
Exercise: Using the teachpack • Install the simple-draw.ss teachpack. • Type in an example for each of the four programs the teachpack gives. • See if it works the way you expect. If not, try and figure out why it doesn’t. - Either you expected the wrong thing - Or you did something wrong when typing it in.
Now we are ready to draw UFOs • Step 1: Open the canvas. (start 100 200) Don’t worry about it printing out true. This is an error by the teachpack writer. • Step 2: Draw a solid circle. (draw-solid-disk0 50 50 10 'green) • Step 3: Draw a line through the middle. (draw-solid-rect0 30 50 40 3 'green)
How do we draw many UFOs? • We can keep doing all 3 steps every time we want to draw a UFO. This will quickly get boring. • A better way is to write a program that will draw any UFO. • This program will use the simple-draw programs. • Defining this function is similar to defining the X or Y function for determining the UFO’s location.
In summary… • Today we saw how to use predefined functions for drawing. • Next time, we will use this to write a program to draw UFOs. Next time…