1 / 10

Section 1.2: Drawing a UFO (continued)

Section 1.2: Drawing a UFO (continued). Writing a program to draw UFOs. Remember that the header includes: define program’s name (let’s call it UFO-draw) program’s variables (t, for the time) ( define (ufo-draw t) ... will fill in later …). Writing a program to draw UFOs.

courtright
Télécharger la présentation

Section 1.2: Drawing a UFO (continued)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Section 1.2: Drawing a UFO(continued)

  2. Writing a program to draw UFOs Remember that the header includes: • define • program’s name (let’s call it UFO-draw) • program’s variables (t, for the time) (define (ufo-draw t) ... will fill in later …)

  3. Writing a program to draw UFOs (define (ufo-draw t) (and (draw-solid-disk0 ... ...) (draw-solid-rect0 ... ...))) • Since we drew a disk and a rectangle to draw the UFO, the function says that to draw a UFO first draw a disk and then a rectangle.

  4. Writing a program to draw UFOs (define (ufo-draw t) (and (draw-solid-disk0 ... ... 10 'green) (draw-solid-rect0 ... ... 40 3 'green))) • To keep the program relatively simple, we will assume that all UFO’s are green and exactly the same size.

  5. Writing a program to draw UFOs • Of course, the (…) tells us that the function is still incomplete. • More precisely, the dots in draw-solid-disk0 (and draw-solid-rect0) say we don't know the X and Y coordinates of the UFO. • Recall that the UFO’s location is expressed by X and Y. So if we know t, we can use the X and Y programs that we wrote in Section 1.1: (define (X t) (+ (* 10 t) 20)) [We used the + and * functions (define (Y t) to write X (and Y).] (+ (* 20 t) 60))

  6. Writing a program to draw UFOs For draw-solid-disk0, the location is the center: (X, Y). (define (ufo-draw t) (and (draw-solid-disk0 (X t) (Y t) 10 'green) (draw-solid-rect0 … 40 3 'green)))

  7. Writing a program to draw UFOs However, for draw-solid-rect0, the location is the top-left corner: (X – 20, Y). (define (ufo-draw t) (and (draw-solid-disk0 (X t) (Y t) 10 'green) (draw-solid-rect0 (- (X t) 20) (Y t) 40 3 'green))) [We used the draw-solid-disk0, draw-solid-rect0, X, and Y functions to write ufo-draw.]

  8. Writing a program to erase UFOs • Notice that the screen is (almost) white. • To make it appear that we are erasing the UFO, we simply draw a white one. • Let’s call this function erase-UFO. • All we need to do is make 2 small changes to draw-UFO: -change the program’s name -change each of the colors to white

  9. Writing a program to erase UFOs (define (ufo-draw t) (and (draw-solid-disk0 (X t) (Y t) 10 'green) (draw-solid-rect0 (- (X t) 20) (Y t) 40 3 'green))) BECOMES (define (ufo-erase t) (and (draw-solid-disk0 (X t) (Y t) 10 ‘white) (draw-solid-rect0 (- (X t) 20) (Y t) 40 3 ‘white)))

  10. In summary… • Using figure 1, we can now find a UFO’s location (using the X and Y functions. • And, we can now draw and erase the UFO on the screen (using the UFO-draw and UFO-erase functions). Next time… • In programming, one should not have two functions that both have similar code. • We will see another way to write UFO-draw and UFO-erase. • Then, finally on to the ANIMATION!

More Related