110 likes | 122 Views
CSC 160 Computer Programming for Non-Majors Lecture #5c: Functions with Images. Prof. Adam M. Wittenstein Wittenstein@adelphi.edu http://www.adelphi.edu/~wittensa/csc160/. A preview…. Today we will define functions involving images (as well as numbers, words, sentences).
E N D
CSC 160Computer Programmingfor Non-MajorsLecture #5c: Functions with Images Prof. Adam M. Wittenstein Wittenstein@adelphi.edu http://www.adelphi.edu/~wittensa/csc160/
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.
Example 1: mirror-horiz ; Purpose: To place any picture next to its horizontal mirror image. ; Contract: image -> image
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))
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))
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.
Example 2: counterchange Define a function called counterchange that takes in two images and returns a 2x2 arrangement of them as shown here.
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”
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?
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.
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.