Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 160 Computer Programming for Non-Majors Lecture #5c: Functions with Images Prof. Adam M. Wittenstein

Similar presentations


Presentation on theme: "CSC 160 Computer Programming for Non-Majors Lecture #5c: Functions with Images Prof. Adam M. Wittenstein"— Presentation transcript:

1 CSC 160 Computer Programming for Non-Majors Lecture #5c: Functions with Images Prof. Adam M. Wittenstein Wittenstein@adelphi.eduhttp://www.adelphi.edu/~wittensa/csc160/

2 A preview… Today we will define functions involving images (as well as numbers, words, sentences). We follow the same rule for defining Scheme functions: (define (function-name param-name[s]) (expression)) We will practice using the Design Recipe when writing these functions.

3 Example 1: mirror-horiz ; Purpose: To place any picture next to its horizontal mirror image. ; Contract: image -> image

4 Example 1: mirror-horiz ; Purpose: To place any picture next to its horizontal mirror image. ; Contract: image -> image “Examples of mirror-horiz:” (define ADAM ) (mirror-horiz ADAM) “should be” (image-beside Adam (reflect-horiz ADAM))

5 Example 1: mirror-horiz ; Purpose: To place any picture next to its horizontal mirror image. ; Contract: image -> image (define (mirror-horiz pic) … pic …) “Examples of mirror-horiz:” (define ADAM ) (mirror-horiz ADAM) “should be” (image-beside Adam (reflect-horiz ADAM))

6 Example 1: mirror-horiz ; Purpose: To place any picture next to its horizontal mirror image. ; Contract: image -> image (define (mirror-horiz pic) (image-beside pic (reflect-horiz pic))) “Examples of mirror-horiz:” (mirror-horiz ADAM) “should be” (image-beside ADAM (reflect-horiz ADAM)) *Note that this function “uses” image-beside and reflect-horiz.

7 Example 2: counterchange Define a function called counterchange that takes in two images and returns a 2x2 arrangement of them as shown here.

8 Example 2: counterchange ; Purpose: To create a 2x2 arrangement of any two images. ; Contract: image image -> image ; Examples: ;(counterchange (rectangle 50 30 ‘solid ‘blue) (rectangle 20 ;20 ‘solid ‘red)) “should be” “a blue rectangle next to a red ;square with a red square next to a blue rectangle below it” ;(counterchange (circle 30 ‘solid ‘green) HANDS) “should be” ;“a green circle next to the HANDS with the HANDS next to a ;green circle below it”

9 Example 2: counterchange ; Skeleton: ; (define (counterchange pic1 pic2) ; … pic1 … pic2 …) ;Informal Thinking ;We need to put two images next to each other. How do we do this? ;How do we put an image directly above another?

10 Example 2: counterchange ; Actual Function: (define (counterchange pic1 pic2) (image-above (image-beside pic1 pic2) (image-beside pic2 pic1) ) *Note that this function “uses” image-above once and image-beside twice.

11 In summary… We have now defined functions involving several different data types. Next class, we will discuss error messages (including how to prevent them and how to interpret them). Please read Section 2.4 of How to Design Programs before next class.


Download ppt "CSC 160 Computer Programming for Non-Majors Lecture #5c: Functions with Images Prof. Adam M. Wittenstein"

Similar presentations


Ads by Google