; CS 111 - Week 3 Lecture 1 - 2016-09-06 ; more course style standards: ; * remember: named constants will be written ; in all-uppercase ; * we'll write function names in all-lowercase ; * we'll write parameter names in all-lowercase ; * and always ; start the expression within a function body ; on its own line, and indent it by at least 3 spaces ; * for a compound expression written on more ; than one line, line up its arguments for ; readability ;------- ; A NEW TYPE! (built-in) (require 2htdp/image) (require 2htdp/universe) ; scene - ; a special kind of image supported by the ; 2htdp/image and 2htdp/universe teachpacks ; * you can use scene type expressions with ; animations and other things... ; * a scene is a special rectangle that can ; have images placed at specified locations ; within it ;====== ; you can create an empty scene! ; signature: empty-scene: number number -> scene ; purpose: expects the desired width and height ; of a scene in pixels, and returns a scene ; instance with that width and height (empty-scene 400 100) (empty-scene 10 76) ; let's say I am going to be using many scene ; instances, all of width 300 and height 200 ; pixels. ; first: I might like named constants for ; that width and height (define SCENE-WIDTH 300) (define SCENE-HEIGHT 200) (empty-scene SCENE-WIDTH SCENE-HEIGHT) ; if a set, unchanging value is used more than once in ; a program -- especially if someone reading it ; later might not be sure what it means -- ; it is good practice to make that a named constant ; ; (more readable, easier to modify when you or your ; client changes their mind, etc.) ; what if I may be using an empty scene more than once? ; or ANY static scene more than once? (define BACKDROP (empty-scene SCENE-WIDTH SCENE-HEIGHT)) ; how can I add something to a scene? ; place-image ; signature: place-image: image number number scene -> scene ; purpose: expects an image, the desired x and y coordinates, ; and a scene, and returns a new scene with the given image ; placed into the given scene centered at the given ; (x, y) position within the given scene ;=========== ; IMPORTANT NOTE!!! ; in the scene type, ; (and in many graphics packages) ; * (0,0) is the TOP LEFT of the scene, ; * and larger values of y are placed ; lower in the scene (place-image (circle 30 "solid" "blue") 50 50 BACKDROP) (place-image (square 30 "outline" "green") 300 25 BACKDROP) ; make sure you can get yourself to believe ; the following will give you a quarter ; of a circle in the lower right corner ; of a scene with SCENE-WIDTH and SCENE-HEIGHT: (place-image (circle 30 "solid" "purple") SCENE-WIDTH SCENE-HEIGHT BACKDROP) (place-image (circle 30 "solid" "red") 0 SCENE-HEIGHT BACKDROP) (place-image (rectangle SCENE-WIDTH SCENE-HEIGHT "solid" "red") (/ SCENE-WIDTH 2) (/ SCENE-HEIGHT 2) BACKDROP) (define RED-BACKING (place-image (rectangle SCENE-WIDTH SCENE-HEIGHT "solid" "red") (/ SCENE-WIDTH 2) (/ SCENE-HEIGHT 2) BACKDROP) ) ; hey, I want a function that expects ; a width, height, and color, ; and gives me a scene of that color ; signature: custom-color-scene: number number color -> scene ; purpose: expects a desired scene's width and height ; in pixels, and its desired background color, ; and returns a scene with those specifications ; COURSE STYLE: do not use BSL Racket built-in type ; names as parameter names ; (you can always put a- or an- in front of them...) ;(define (custom-color-scene width height a-color) ; ... ;) (check-expect (custom-color-scene 60 80 "burgundy") (place-image (rectangle 60 80 "solid" "burgundy") 30 40 (empty-scene 60 80)) ) (check-expect (custom-color-scene 80 100 "gold") (place-image (rectangle 80 100 "solid" "gold") 40 50 (empty-scene 80 100)) ) (define (custom-color-scene width height a-color) (place-image (rectangle width height "solid" a-color) (/ width 2) (/ height 2) (empty-scene width height)) ) (custom-color-scene 60 80 "burgundy")